I'm trying to change a JList inside a JScrollPane dynamically, using
myList.setListData(someArray);
After this I would expect the JScrollPane to show Scrollbars as needed, but this doesn't happen. Googling the issue was not very helpful. I tried various combinations of the following methods, with little success (basically poking through the 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 scrollbar appears, but it doesn't get activated by the model change. I also hooked into the PropertyChangeEvent and validated that the JList fires and event when the model changed. What am I missing here? What method do I have to use to make this work, or even better what property would make this work out of the box?
A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.
JList doesn't support scrolling directly. To create a scrolling list you make the JList the viewport view of a JScrollPane. For example: JScrollPane scrollPane = new JScrollPane(dataList); // Or in two steps: JScrollPane scrollPane = new JScrollPane(); scrollPane.
if the preferredSize is set on JList, even if (0, 0), then JScrollPane doesn't work - ensure that it is unset, setPreferredSize(null) if necessary
see mrtextminer as follows
The solution to this problem is to not setting the preferredSize of the JList (removing the setting statement). In Netbeans IDE 5.5.1, unsetting the preferredSize of the JList can be achieved by right clicking on the preferredSize property of the JList and then selecting “restoring default value”.
In conclusion, set preferredSize of the JScrollPane only. Do not set the preferredSize of the JList.
just wrote a little sample app to test this behaviour, and it works out of the box. when you modify the list data, vertical and horizontal scrollbars are added to the scrollpane as needed by default.
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();
}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With