Java JList 模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2893052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:10:22  来源:igfitidea点击:

Java JList model

javaswingjlist

提问by palAlaa

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

如何从 a 制作列表模型JList以便能够将项目插入其中。我想用这个方法:addElement(java.lang.Object item)

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

我在这里找到了一个解释,但问题是这ListModel是一个接口,即使我编写了一个实现并覆盖了它的方法,我也无法使用该addElement()方法

采纳答案by Michael Mrozek

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

Java 提供了ListModel已经的实现,例如DefaultListModel,您可以实例化和使用

For example:

例如:

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

final JList list = new JList(model);

回答by npinti

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

查看此Sun 教程。包含有关如何将列表添加到 JList 的示例