简体   繁体   中英

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.

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 a arrayList here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM