将滚动条添加到JList [英] Adding Scrollbar to JList
问题描述
我正在尝试向JList添加滚动条(它使用自定义数据模型).添加JScrollPane实际上是隐藏 JList,而不是添加滚动条.当我使用滚动条运行代码时,JList不可见,并且我也看不到数据.
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);
注释listScrollPane = ...行使JList再次可见,并且按预期方式工作.
Commenting out the listScrollPane = ... line makes the JList visible again and it works as expected.
我想念什么?
推荐答案
您需要将滚动窗格添加到容器中,而不是列表中.
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.
这篇关于将滚动条添加到JList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!