• Post Reply Bookmark Topic Watch Topic
  • New Topic

JList

 
Perk J
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JList with multiple selection mode. But I could not get all the selected values in a String[]. I tried to use - getSelectedValues() and then converts it toString(); I tried also with getSelectedIndices() and few others but could not make it work. Any suggestions ??
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly happens when you use getSelectedValues(). That method returns an Object[]. Then you used toString(). What happened when you did that?
 
Perk J
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compiled, it did not complain. But when I run it and get the action to print out those selected items, it gave exceptions.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably need to post a code snippet and the exact error you are getting.
 
Perk J
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, at this moment I am not working with the same machine where is that program runs. But so far I remember it was kinda:
String[] values = (String[])list.getSelectedValues();
Then print the values in a textarea.
Another one I tried:
Object[] selected = list.getSelectedValues();
String[] values = new String[selected.length];
for (int i = 0; i < selected.length; i++)
values[i] = selected[i].toString();
And I dont remember the exceptions.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you do if nothing is selected? Are you getting a NullPointerException?
 
Perk J
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I did not tried without selection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!