Adding a scrollbar to a JList

I am trying to add a scroll bar to a JList (which uses a custom data model). Adding a JScrollPane actually hides the JList instead of adding a scroll bar. When I run the code using the scrollbar, the JList is not displayed and I can not see the data.

playlistField = new JList(playlist); // playlist is a data model playlistField.setLocation(32, 220-36); playlistField.setSize(350,120); playlistField.setVisible(true); this.add(playlistField); listScrollPane = new JScrollPane(playlistField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

Commenting out the line listScrollPane = ... makes the JList visible again and works as expected.

What did I miss?

+6
scrollbar jlist
source share
1 answer

You need to add scrollpane to the container, not the list.

In your current example, adding a list to scrollpane, it removes the list from its original container, since the component can have only one parent.

+6
source share

All Articles