如何将元素添加到现有JList [英] How to add element to existing JList

查看:141
本文介绍了如何将元素添加到现有JList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的部分代码

 ArrayList<Item> i = g.getItems(); 
 Vector itemsVector = new Vector(i); 
 JList items = new JList(iemsVector); 

稍后在代码中我创建了我想要添加到JList的新对象。我该怎么做?

Later in the code I create new object which I want to add to JList. How can I do that?

推荐答案

使用DefaultListModel填充JList,而不是向量,并在类中显示模型。然后只需在列表模型上调用addElement即可向其中添加项目。

Populate the JList with a DefaultListModel, not a vector, and have the model visible in the class. Then simply call addElement on the list model to add items to it.

这篇关于如何将元素添加到现有JList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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