2012-04-04 99 views
0

我有一個奇怪的問題,我似乎無法解決。我有一個JList必須能夠滾動,如果有更多的項目,然後可以顯示。但是,如果我把JListJScrollPane,它不利用BorderLayout它在的EAST部分的整個高度JScrollPane中的JList打亂JPanel高度

例無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

例如用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

我想JList利用的BorderLayout它在東部區域的全部可用的高度。我已經試過我的包裹裏面JList另一個JPanel,但這並不無論是解決我的問題。

回答

0

我以前成功使用過的一個黑客手段是向JList/JScrollPane的父組件添加ComponentListener,並且隨着它改變,通過(僞代碼)setPreferredSize()重置JList和/或Jscrollpane的首選大小getPreferredSize()。width,parentHeight)

1

使用setVisibleRowCount()方法來設置Jlist中可見行的數量。 users.setVisibleRowCount(10);