Bobby
Bobby

Reputation: 18315

How can I get the text from a component in a JList?

I have a JList and I am wanting to get the text of an entry of that list at a specific index. Could someone inform me how to do this or should I restructure my code to getValues instead of getIndices?

Upvotes: 3

Views: 12534

Answers (4)

SandroMarques
SandroMarques

Reputation: 6554

String nick = jListNicknames.getModel().getElementAt(index).toString();
System.out.println(nick);

Upvotes: 0

Dave
Dave

Reputation: 11

DefaultListModel list = new DefaultListModel();
JList jl = new JList(list);

int i = 21;
Object = element;
String = yourElement;

element = jl.getModel().getElementAt(i);
yourElement = element.toString;

Upvotes: 1

Pierre
Pierre

Reputation: 35306

JList dataList=(...)

 for(int i = 0; i < dataList.getModel().getSize(); i++) {
     System.out.println(dataList.getModel().getElementAt(i));
 }

Upvotes: 8

Bobby
Bobby

Reputation: 18315

Object[] temp = jList1.getSelectedValues();
temp[i] = the object you want.

Upvotes: 2

Related Questions