Search code examples
javaswingjlistgridbaglayout

JList sudden change


In the picture below, the bottom JComponent is a JList (list) within a JScrollPane. By default the number of visible rows was 8 or 10, I don't know. But suddenly, while I was making more changes to the project it changed to this, and now I can only see one item at a time (fsgisfg). How can I change the number of rows to be displayed at a time?

list.setVisibleRowCount(8) doesn't work.

The list uses an extension of AbstractListModel as a model.

The main menu JFrame uses GridBagLayout.

I don't know what could possibly make this happen because I even tried to undo all the changes made in the project, and the result is still the same.

private JList<String> list;
[...]

list = new JList<String>(); 
list.setVisibleRowCount(8); //doesn't change anything
[...]

JScrollPane scroll2 = new JScrollPane(list);
[...]

list.setModel(new BookListModel(library));
list.repaint(); //the model gets the data for the list, and refresh is needed

[...]

GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
frame.add(scroll2, c);

image http://dl.dropbox.com/u/71389667/problem.jpg


Solution

  • Emm... it is quite hard now to get what causes the problem so first I do advise you to read this tutorial before you keep looking the bug...

    As for this moment, I just may assume that the problem is actually may come from Layout Managers or something in this direction... Try to play around the JList containing panel (or what component you using...) layout manager. It seems like right now you are using some kind of absolute one so change it to lets say FlowLayout for example.

    As another tip, check was the ListModel added null element(s)?

    For more suggestions, there would be good to see more code...


    Report if it helped