2014-02-09 31 views
0

好的,所以我试图创建两个单独的JScrollPanes,它带有两个列表;一个用户列表和一个书目列表,并简单地介绍它们。现在的问题是,我的“bookData”数组中有19个项目,我的“userData”数组中有15个项目,但是我的JScrollPanes和我的钱包一样都是空的。JScrollPanes尽管输入数组和DefaultListModels不为空也是空的

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

!! - 编辑:

只注意到ODDEST的行为!

我的清单并非如此 - 这是窗口在启动时的外观;

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

但是 - 这里的扭曲!只要我通过拖动角落更改窗口大小 - 我的列表出现!因此,错误不在列表中,而是在图形中,methinks。 :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); 
} 

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

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

但是,这显然没有奏效。

+0

作为参考,这里是一个完整的,工作[示例](http://stackoverflow.com/a/15104660/230513)显示多个滚动列表。 – trashgod

+0

不是100%确定,但你可能想尝试避免设置大小和位置。使用布局管理器。请参阅[我是否应避免使用Java Swing中的set(Preferred | Maximum | Minimum)Size方法?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-方法 - 在-java的SWI)。 –

回答

0

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

newLoan.repaint(); 
相关问题