I have a weird problem that I can't seem to solve. I've got a JList
which has to be able to scroll if there are more items in it then can be displayed. However if I put the JList
in a JScrollPane
, it doesn't utilise the full height of the EAST
part of the BorderLayout
it's in.
Example without JScrollPane
:
public UsersPanel(){
String[] userList = new String[]{"Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar"};
JList users = new JList(userList);
add(users);
}
Example with JScrollPane
:
public UsersPanel(){
String[] userList = new String[]{"Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar"};
JList users = new JList(userList);
JScrollPane sp = new JScrollPane(users);
add(sp);
}
I want a JList
that utilises the full available height of the EAST part of the BorderLayout
it's in. I've tried wrapping my JList
inside another JPanel
, but that doesn't solve my problem either.
use setVisibleRowCount() method to set number of visible rows in Jlist. users.setVisibleRowCount(10);
One hack that I've used successfully before is to add a ComponentListener to parent component of the JList / JScrollPane, and as it changes reset the preferred size of the JList and/or Jscrollpane via (pseudo-code) setPreferredSize(getPreferredSize().width, parentHeight)