I want to add a String Array to a list 开发者_如何学运维box in 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
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:
Drag and Drop the JList Component onto your parent container
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.). 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
- Item 2
- Item 3, etc.
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. If you want to do it programmatically, do as genessis suggets.
精彩评论