OmidTahouri
OmidTahouri

Reputation: 1352

Elements not adding to JList

For some reason, I can't add anything to my JList. The JList is visible but simply shows white - nothing can be selected.

List list;
DefaultListModel listModel;
//...
list = new JList();
list.setBounds(220,20,150,200);
listModel = new DefaultListModel();
listModel.addElement("ONE");
panel.add(list);

Am I missing something?

Upvotes: 0

Views: 1070

Answers (2)

coobird
coobird

Reputation: 160964

The JList is not using the listModel.

One way is to initialize the JList by specifying a ListModel to use:

DefaultListModel listModel = ...
JList list = new JList(listModel);

Then, performing changes to the listModel (such as calling addElement) will cause the changes to appear on the JList.

For more information on using JLists, the How to Use Lists lesson from The Java Tutorials is a good source.

Upvotes: 1

Jonathan Feinberg
Jonathan Feinberg

Reputation: 45324

You never set the list's model to the ListModel you've constructed.

Upvotes: 0

Related Questions