将滚动条添加到 JList
我正在尝试向 JList 添加滚动条(它使用自定义数据模型)。 添加 JScrollPane 实际上隐藏 JList,而不是添加滚动条。 当我使用滚动条运行代码时,JList 不可见,并且我看不到数据。
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 再次可见,并且它按预期工作。
我错过了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.