简体   繁体   English

将选定的项目从jlist保存到字符串变量

[英]Save selected items from jlist to string variables

I have a Jlist with 10 items (booktitles). 我有一个包含10个项目(书名)的Jlist。 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). 允许用户从10个中选择3个。我想将所选项目保存到3个不同的字符串变量(例如book0,book1,book2等)。

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. 我创建了显示有10个标题的Jlist,并且尝试了很多事情,但是没有运气。

say for example you have already defined a JList object list . 例如,您已经定义了JList对象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. 您只需要String selectedItem的数组,您也可以在此处使用arrayList。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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