Ibrar Sa
Ibrar Sa

Reputation: 41

Save selected items from jlist to string variables

I have a Jlist with 10 items (booktitles). The user will be allowed to select 3 items out of the 10. I want to save the selected items to 3 different string variables (example book0, book1, book2, etc).

I am not sure how to do this.

I have the Jlist created with 10 titles showing, and I have tried so many things but no luck.

Upvotes: 3

Views: 4205

Answers (1)

Sanyam Goel
Sanyam Goel

Reputation: 2180

say for example you have already defined a JList object list.

This should do it for you

Object[] selected = list.getSelectedValues();

String[] selectedItems = new String[selected.length];

for(int i=0; i<selected.length;i++){

selectedItems[i] = selected[i].toString();

}

the array of String selectedItem is all you needed, you may also use an arrayList here.

Upvotes: 5

Related Questions