刷新 JFrame 中的 JList

发布于 2024-10-04 02:04:25 字数 280 浏览 1 评论 0原文

我有一个 JList,它显示来自向量的信息。然后,用户可以向该向量添加或删除信息。当从 Vector 中添加/删除项目时,是否可以刷新 JFrame 内的 JList?目前我正在做..

 list = new JList(names);
 jframe.add(new JScrollPane(list), BorderLayout.CENTER);

但这不会将 JList 刷新为任何新内容。我已经检查过,我的矢量内容等确实发生了变化,但列表并未刷新。为什么?我该如何修复它?

I've got a JList which displays information from a vector. The user can then add and remove information from this vector. Is it possible to refresh the JList inside my JFrame when items are added / removed from the Vector? Currently I'm doing..

 list = new JList(names);
 jframe.add(new JScrollPane(list), BorderLayout.CENTER);

but this doesn't refresh the JList to anything new. I've check and my vector contents etc. do change but the list isn't refreshing. Why? how can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

明天过后 2024-10-11 02:04:26

您不应该更新向量。应直接对 ListModel 进行更改,然后表格将自动重新绘制。

如果由于 Vector 的更改而决定重新创建 ListModel,则可以通过执行以下操作来更新列表:

list.setModel( theNewModel );

编辑:忘记 Vector 并将数据直接加载到 DefaultListModel 中:

DefaultListModel model = new DefaultListModel();
model.addElement( "one" );
model.addElement( "two" );
JList list = new JList( model );

现在,每当您需要更改数据时,都可以直接更新模型使用 addElement()、removeElement() 或 set() 方法。该列表将自动重新绘制。

You should not be updating the Vector. Changes should be made directly to the ListModel then the table will repaint itself automatically.

If you decide to recreate the ListModel because of the changes to the Vector, then you update the list by doing:

list.setModel( theNewModel );

Edit: Forget the Vector and load the data directly into the DefaultListModel:

DefaultListModel model = new DefaultListModel();
model.addElement( "one" );
model.addElement( "two" );
JList list = new JList( model );

Now whenever you need to change the data you update the model directly using the addElement(), removeElement() or set() methods. The list will automatically be repainted.

别忘他 2024-10-11 02:04:26

修改 Vector 后,在 Jlist 上调用 updateUI

Call updateUI on the Jlist after modifying your Vector.

电影里的梦 2024-10-11 02:04:26

我想我找到了 Jlist 图形“刷新”的解决方案。尝试在每次添加或删除 Jlist 保存的模型元素后调用此方法。

Jlist_name.ensureIndexIsVisible(model_name.getSize());

I think I found the solution for the Jlist's graphic 'refresh'. Try calling this method after each add or remove element of the model that is held by the Jlist.

Jlist_name.ensureIndexIsVisible(model_name.getSize());

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