Adding Scrollbar to JList

和自甴很熟 提交于 2019-12-08 21:58:15

问题


I'm trying to add a scrollbar to a JList (which uses a custom data model). Adding a JScrollPane actually hides the JList, instead of adding a scrollbar. When I run the code with the scrollbar, the JList is not visible, and I cannot 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 listScrollPane = ... line makes the JList visible again and it works as expected.

What did I miss?


回答1:


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

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



来源:https://stackoverflow.com/questions/940838/adding-scrollbar-to-jlist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!