刷新JFrame中的JList [英] Refresh JList in a JFrame

查看:212
本文介绍了刷新JFrame中的JList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示来自矢量的信息的JList。然后,用户可以添加和删除此向量中的信息。当从Vector中添加/删除项目时,是否可以刷新JFrame中的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);

但这不会将JList刷新为任何新内容。我检查,我的矢量内容等确实改变,但列表不刷新。为什么?我该如何解决?

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?

推荐答案

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

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

如果由于对Vector的更改而决定重新创建ListModel,那么您将更新通过执行以下操作:

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

list.setModel( theNewModel );

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

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 );

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

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.

这篇关于刷新JFrame中的JList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆