【问题标题】:JScrollPanes empty despite input arrays and DefaultListModels not being empty尽管输入数组和 DefaultListModels 不为空,但 JScrollPanes 为空
【发布时间】:2014-03-06 20:31:14
【问题描述】:

好的,所以我正在尝试创建两个单独的 JScrollPanes,它们需要两个列表;一个用户列表和一个书籍列表,并简单地呈现它们。问题是现在,我的“bookData”-array 中有 19 个项目,“userData”-array 中有 15 个项目,但是我的两个 JScrollPanes 和我的钱包一样空。

当使用 getSize() 检查我的 DefaultListModels 时,我发现它们的大小也是 15 和 19。我觉得我在这里遗漏了一些重要的东西。百万美元的问题是什么?

!! - 编辑:

刚刚注意到奇怪的行为!

我的列表不是空的 - 这就是窗口在启动时的样子;

http://i60.tinypic.com/2nr0kyu.png

但是 - 这就是转折点!只要我通过拖动角落更改窗口大小 - 我的列表就会出现!因此,我认为错误不在列表中,而在图形中。 :3

http://i61.tinypic.com/2ah7ac3.png

这就是我的方法的样子;

public void newLoan(){
    StringBuilder sb = new StringBuilder();
    StringBuilder string = new StringBuilder();
    JLabel userLabel;
    JLabel bookLabel;
    JButton btnRegister;
    // Build a string of all users, separated with a "–"
    for (int i = 1; i < Processes.userList.size()+1; i++) {
        sb.append(Processes.userList.get(i).getFirstname()+" "+Processes.
                userList.get(i).getSurname()+", "+Processes.userList.
                get(i).getPersonalID()+"–");
        System.out.println(Processes.userList.get(i).getFirstname()+" "+Processes.
                userList.get(i).getSurname()+", "+Processes.userList.
                get(i).getPersonalID());
    }
    // Build a string of all books, separated with a "–"
    for (int i = 1; i < Processes.bookList.size()+1; i++) {
        if (Processes.bookList.get(i).getAvailable()-
                Processes.bookList.get(i).getOnLoan()!=0) {
            string.append(Processes.bookList.get(i).getAvailable()-
                    Processes.bookList.get(i).getOnLoan()+" available - "+
                    Processes.bookList.get(i).getTitle()+" by "+Processes.
                    bookList.get(i).getAuthor_firstname()+" "+Processes.
                    bookList.get(i).getAuthor_surname()+" ("+Processes.
                    bookList.get(i).getIsbn()+")–");
            System.out.println(Processes.bookList.get(i).getAvailable()-
                    Processes.bookList.get(i).getOnLoan()+" available - "+
                    Processes.bookList.get(i).getTitle()+" by "+Processes.
                    bookList.get(i).getAuthor_firstname()+" "+Processes.
                    bookList.get(i).getAuthor_surname()+" ("+Processes.
                    bookList.get(i).getIsbn());
        }
    }
    // split sb at the "–"'s and fill an array.
    String[] userData = sb.toString().split("–");
    System.out.println(userData.length);
    // split string at the "–"'s and fill an array.
    String[] bookData = string.toString().split("–");
    System.out.println(bookData.length);
    /* Defining sizes and locations and initializing all variables. Also
     * defining text on buttons and labels.*/
    DefaultListModel userModel = new DefaultListModel();
    for (int i = 0; i < userData.length; i++) {
        userModel.addElement(userData[i]);
    }
    System.out.println(userModel.getSize());
    final JList userJList = new JList();
    userJList.setModel(userModel);
    JScrollPane userList = new JScrollPane();  //f*cking JScrollPane! Work!
    userList.setViewportView(userJList);

    DefaultListModel bookModel = new DefaultListModel();
    for (int i = 0; i < bookData.length; i++) {
        bookModel.addElement(bookData[i]);
    }
    System.out.println(bookModel.getSize());
    final JList bookJList = new JList();
    bookJList.setModel(bookModel);
    JScrollPane bookList = new JScrollPane();
    bookList.setViewportView(bookJList);

    userLabel = new JLabel("Select user to register loan onto:");
    userLabel.setSize(250,20);
    userLabel.setLocation(20, 50);
    userList.setSize(150, 200);
    userList.setLocation(70, 80);
    bookList.setSize(150, 200);
    bookList.setLocation(400, 80);
    btnRegister = new JButton("Register loan");
    btnRegister.setSize(150,50);
    btnRegister.setLocation(235, 300);

    // Adding functionality... Later

    // Adding the different components to the panel "newLoan".
    newLoan.add(userLabel);
    newLoan.add(userList);
    newLoan.add(bookList);
    newLoan.add(btnRegister);
}

我的第一个想法是简单地使用我的 String 数组并将它们呈现在 JScrollPanes 中;

final JList userJList = new JList(userData);
JScrollPane userList = new JScrollPane();
userList.setViewportView(userJList);

但这显然不起作用。

【问题讨论】:

标签: java swing jscrollpane jlist


【解决方案1】:

解决方案非常复杂。在每个列表的方法之外添加私有 DefaultListmodel,并将所有内容作为元素添加到这些模型中。最后,但并非最不重要的是,我使用以下方法重新绘制了 JPanel;

newLoan.repaint();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2010-12-17
    相关资源
    最近更新 更多