[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Need help with JList

Basically I want a List with names
"name1"
"name2"
"name3"

I declare this list this way:

JList dataList = new JList();
JScrollPane scrollPane2 = new JScrollPane(dataList);

But I can't figure out how I can add/remove names in this list. Please help.
0
brightwood
Asked:
brightwood
  • 2
  • 2
1 Solution
 
StillUnAwareCommented:
dataList.add("name1");
...
0
 
StillUnAwareCommented:
Sorry, my mistake, it should be:

    String[] data = {"name1", "name2"};
    dataList.setListData(data);
0
 
objectsCommented:
you add the elements to the list model:

DefaultListModel model = new DefaultListModel();
model.addElement("name1");
model.addElement("name2");
model.addElement("name3");
JList dataList = new JList(model);
JScrollPane scrollPane2 = new JScrollPane(dataList);

// you can also add extra one after list created

model.addElement("name4");
model.addElement("name5");
0
 
brightwoodAuthor Commented:
I little other thing.

How can I clear (remove all text appended) a JTextArea ?
0
 
objectsCommented:
ta.setText("");
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now