JList in JScrollPane messes up JPanel height

Go To StackoverFlow.com

0

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 without JScrollPane

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

Example with JScrollPane

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.

2012-04-04 19:25
by hannesbelen


1

use setVisibleRowCount() method to set number of visible rows in Jlist. users.setVisibleRowCount(10);

2012-04-07 07:19
by gauravHI5


0

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)

2012-04-04 19:32
by ControlAltDel
Ads