Java JList model

半城伤御伤魂 提交于 2019-11-30 17:25:16

问题


How can I make a list model from a JList in order to be able to insert an item into it. I want to use this method: addElement(java.lang.Object item)

I found an explanation here, but the problem is that ListModel is an interface and even if I write an implementation and override its method, I can't use the addElement() method


回答1:


Java provides implementations of ListModel already, like DefaultListModel, that you can instantiate and use

For example:

final DefaultListModel model = new DefaultListModel();
model.addElement("one");
model.addElement("two");
model.addElement("three");

final JList list = new JList(model);



回答2:


Check out this Sun Tutorial. Contains example on how to add lists to a JList



来源:https://stackoverflow.com/questions/2893052/java-jlist-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!