Make JScrollPane scrollbar when changing JList inside

I am trying to change a JList inside a JScrollPane dynamically using

myList.setListData(someArray);

After that, I expected JScrollPane to show scrollbars as needed, but this will not happen. The problem with googling was not very helpful. I tried various combinations of the following methods, with little success (mostly punching api docs):

myScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
myList.validate(); myScrollPane.validate();
myScrollPane.setPreferredSize(Dimension someDimension);
myScrollPane.setViewportView(moduleList);
myScrollPane.setLayout(...);

When using the first method, a scroll bar appears, but it does not activate when the model changes. I also connected to the PropertyChangeEvent and confirmed that the JList is triggered and the event when the model has changed. What am I missing here? What method should I use to make this work, or even better, which property will make this work out of the box?

+5
5

preferredSize JList, (0, 0), JScrollPane - , , setPreferredSize (null)

. mrtextminer

, JList ( ). NetBeans IDE 5.5.1 JList , preferredSize JList " ".

JScrollPane. JList.

+9

, , . scrollpane.

public class Frametest {

private JList list;

public Frametest() {
    JFrame f = new JFrame("Scrollable JList");
    f.setSize(200, 300);
    JScrollPane jsp = new JScrollPane();        
    list = new JList();
    jsp.getViewport().add(list);
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(jsp, BorderLayout.CENTER);
    JButton button = new JButton("update");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Object[] objects = new Object[20];
            for(int i=0; i<20; i++) {
                objects[i] = "foo foo foo foo foo foo foo foo foo foo foo foo "+i;
            }
            list.setListData(objects);
        }
    });
    f.getContentPane().add(button, BorderLayout.SOUTH);
    f.setVisible(true);     
}

public static void main(String[] args) {
    new Frametest();
}}
+3

validate revalidate JList ?

myList.setListData(someArray);
myList.revalidate();

validate revalidate, , validate , , , revalidate , .. .

revalidate myList , , , .

+2

:

jsp.getViewport().add(list);

:

jsp.setViewportView(list);

new JScrollPane(list);

scrollpane. , scrollpane, .

+1

, , setpreferredsize , , .

resultList.setPreferredSize(resultList.getPreferredSize());

+1

All Articles