将滚动条添加到 JList

发布于 2024-07-23 05:00:59 字数 547 浏览 7 评论 0原文

我正在尝试向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2024-07-30 05:00:59

您需要将滚动窗格添加到容器,而不是列表。

在当前的示例中,通过在滚动窗格中添加列表,它会从其原始容器中删除该列表,因为组件只能有一个父组件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文