Chin
Chin

Reputation: 20705

Swing equivalent of the .NET ListBox

What is the Swing equivalent of the .NET ListBox that has the ability to add new items?

Upvotes: 2

Views: 2213

Answers (1)

Joel Christophel
Joel Christophel

Reputation: 2663

JList inside of a JScrollPane.

To add and remove items dynamically, you will want to make use of a DefaultListModel as follows:

DefaultListModel<String> listModel = new DefaultListModel<String>();
JList<String> list = new JList<String>(listModel);
JScrollPane scroll = new JScrollPane(list);
someComponent.add(scroll);

listModel.addElement("new");
listModel.remove(0);

Upvotes: 9

Related Questions