简体   繁体   English

刷新JFrame中的JList

[英]Refresh JList in a JFrame

I've got a JList which displays information from a vector. 我有一个显示向量信息的JList。 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? 当从Vector中添加/删除项目时,是否可以刷新JFrame中的JList? 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. 但这并没有将JList刷新为任何新东西。 I've check and my vector contents etc. do change but the list isn't refreshing. 我检查,我的矢量内容等确实改变了,但列表没有刷新。 Why? 为什么? how can I fix it? 我该怎么解决?

You should not be updating the Vector. 您不应该更新Vector。 Changes should be made directly to the ListModel then the table will repaint itself automatically. 应该直接对ListModel进行更改,然后表将自动重新绘制。

If you decide to recreate the ListModel because of the changes to the Vector, then you update the list by doing: 如果由于对Vector的更改而决定重新创建ListModel,则通过执行以下操作来更新列表:

list.setModel( theNewModel );

Edit: Forget the Vector and load the data directly into the DefaultListModel: 编辑:忘记Vector并将数据直接加载到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. 现在,无论何时需要更改数据,都可以使用addElement(),removeElement()或set()方法直接更新模型。 The list will automatically be repainted. 该列表将自动重新绘制。

修改Vector后,在Jlist上调用updateUI

I think I found the solution for the Jlist's graphic 'refresh'. 我想我找到了Jlist图形'刷新'的解决方案。 Try calling this method after each add or remove element of the model that is held by the Jlist. 尝试在每次添加或删除Jlist所持有的模型元素后调用此方法。

Jlist_name.ensureIndexIsVisible(model_name.getSize()); Jlist_name.ensureIndexIsVisible(model_name.getSize());

每次向量更改时都可以使用list.setListData(vector)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM