2014-02-09 37 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(); 
相關問題