Java 如何在 Netbeans 中设置 JList 的 ListModel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2117867/
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
How to set the ListModel of a JList in Netbeans?
提问by Yatendra Goel
I have designed a Swing GUI with the help of Netbeans IDE and this GUI contains a JList.
我在 Netbeans IDE 的帮助下设计了一个 Swing GUI,这个 GUI 包含一个 JList。
Bydefault, it uses AbstractListModel to pass it as an argument in the JList contructor to create that JList.
默认情况下,它使用 AbstractListModel 将其作为 JList 构造函数中的参数传递以创建该 JList。
I want to specify somewhere in the Netbeans to pass DefaultListModel as the model to be passed in that JList so that later I can retrieve it to make changes in the listModel.
我想在 Netbeans 中的某处指定将 DefaultListModel 作为要在该 JList 中传递的模型,以便稍后我可以检索它以在 listModel 中进行更改。
How can I do that.
我怎样才能做到这一点。
回答by Jason Nichols
You have two ways of doing this:
你有两种方法可以做到这一点:
1) In your code manually call list.setModel() anywhere after initComponents() is called. 2) Do it through NetBeans - Right click the list, go to "Customize Code". The first code section is the list's constructor call. Change the dropdown from "Default Code" to "Custom Creation" and simply insert your ListModel in the constructor call. You can do this by setting it to new
1) 在您的代码中,在调用 initComponents() 之后的任何地方手动调用 list.setModel()。2) 通过 NetBeans 进行 - 右键单击列表,转到“自定义代码”。第一个代码部分是列表的构造函数调用。将下拉列表从“默认代码”更改为“自定义创建”,只需在构造函数调用中插入您的 ListModel。您可以通过将其设置为新来做到这一点
javax.swing.JList(new DefaultListModel())
or by instantiating your listmodel before the call to initComponents() in the code and then doing
或者通过在代码中调用 initComponents() 之前实例化您的列表模型,然后执行
javax.swing.JList(defaultModel);
回答by Tun
I usually do this way in Netbeans
1. Select the JList
2. In modelproperty, select Custom codeand insert the listModel name(declared in 3rd step)
3. declare DefaultListModel listModel = new DefaultListModel();
in code view
4. change listModel declaration to accept a List or similar
我通常在 Netbeans 中这样做
1. 选择JList
2. 在模型属性中,选择自定义代码并插入listModel 名称(在第 3 步中
声明)3.DefaultListModel listModel = new DefaultListModel();
在代码视图中
声明 4. 更改 listModel 声明以接受 List 或类似的