简体   繁体   English

如何将数组字符串添加到Netbeans的列表框中

[英]How to add a Array String to a list box in Netbeans

我想将字符串数组添加到Netbeans的列表框中。

String[] arr = {"one", "two", "three"};
listbox.setListData(arr);    //listbox is your JList object

See: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JList.html 参见: http : //java.sun.com/j2se/1.4.2/docs/api/javax/swing/JList.html

I prefer to do it like this: 我更喜欢这样:

DefaultListModel list = new DefaultListModel();
for (int i = 0; i < data.length; i++) {
    list.addElement(data[i]);
}      
jList1.setModel(list);

If you want to add the items through the GUI builder tool, do as follows: 如果要通过GUI构建器工具添加项目,请执行以下操作:

Drag and Drop the JList Component onto your parent container 将JList组件拖放到父容器中

Select the List component you have just dragged by clicking on it and on the right hand side, chose Properties. 通过单击选择刚刚拖动的列表组件,然后在右侧选择“属性”。

From the list of items at the top (like Background, Border, Font, etc) click on Model (by default it has the following items in it: Item 1, Item 2, etc.). 从顶部的项目列表(如背景,边框,字体等)中,单击“模型”(默认情况下,其中包含以下项目:项目1,项目2等)。 Click on the button that has three ellipses (it is on the same row) and a window should open. 单击具有三个椭圆的按钮(在同一行上),然后将打开一个窗口。 It should have the following items: 它应该包含以下各项:

  • Item 1 项目1
  • Item 2 项目2
  • Item 3, etc. 项目3等

Delete those and put in the items you want to add and press ok. 删除它们,然后放入要添加的项目,然后按确定。

That is how it is done through the Netbeans GUI builder. 这就是通过Netbeans GUI构建器完成的。 If you want to do it programmatically, do as genessis suggets. 如果要以编程方式进行操作,请作为创世记建议。

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

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