0

Ok, so I'm trying to create two separate JScrollPanes that takes two lists; One list of users and one list of books and simply presents them. The problem is that right now, I have 19 items in my "bookData"-array and 15 in my "userData"-array, but both my JScrollPanes are as empty as my wallet.

When checking my DefaultListModels with getSize(), I see that they too are 15 and 19 in size. I feel like I'm missing something crucial here. The million dollar question is what?

!! - EDIT:

Just noticed the ODDEST behaviour!

My lists aren't empty as such - This is how the window looks on start;

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

BUT - and here's the twist! As soon as I change size on the window by dragging the corners - MY LISTS APPEAR! Thus, the error is not in the lists, but in the graphics, methinks. :3

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

This is how my method looks;

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);
}

My first thought was to simply use my String arrays and present them in the JScrollPanes, as such;

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

But that didn't work, obviously.

Jerri Kangasniemi
  • 633
  • 1
  • 9
  • 23
  • For reference, here's a complete, working [example](http://stackoverflow.com/a/15104660/230513) showing multiple scrolling lists. – trashgod Feb 10 '14 at 00:00
  • Not 100% sure, but you may want to try avoiding setting size and location at all. Use Layout Managers. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). – Paul Samsotha Feb 10 '14 at 13:01

1 Answers1

0

The solution was extremely complex. Added private DefaultListmodel's outside the method for each of the lists and added everything as elements to those models. And last, but not least I repainted the JPanel using;

newLoan.repaint();
Jerri Kangasniemi
  • 633
  • 1
  • 9
  • 23