摆动:将列表中的选定值存储到数组中. [英] Swings: storing the selected values from List into an array.

查看:44
本文介绍了摆动:将列表中的选定值存储到数组中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中创建了一个列表,如下所示:

I made a List in java as under:

 String names[] = {"abc@gmail.com", "def@gmail.com","ghi@gmail.com","jkl@gmail.com"};
 JList places = new JList(names) ;

然后为了访问我在valueChanged方法中编写的选定值:

Then in order to access the selected values I wrote in valueChanged method :

String[] emailID= places.getSelectedValuesList().toString[];

这是不正确的...请帮助我重写该行,以便将选定的值存储在数组中.

which is coming out to be incorrect ... Kindly help how should I rewrite this line so as the selected values get stored in array.

推荐答案

如果要将所有选定项作为数组,则可以执行以下操作:

If you want to have all selected Items as an Array you can do something like this:

public static void main(String[] args){

    String names[] = {"abc@gmail.com", "def@gmail.com","ghi@gmail.com","jkl@gmail.com"};
    JList<String> places = new JList<String>(names) ;

    places.setSelectedIndices(new int[]{0,1,2});

    String[] emailIDs = places.getSelectedValuesList().toArray(new String[]{});

    for(String s : emailIDs){
        System.out.println(s);
    }
}

注意:

我在列表中添加了<String>,因为我假设您始终希望将字符串作为值.这样,您可以获得带有通用输出的List .toArray()方法.否则,您需要获取Object[](对象数组)并强制转换值.

I added <String> to the List, because I assume you always want to have Strings as values. That way you can get the List .toArray() method with a generic output. Else you'd need to get an Object[] (Object Array) and cast the values.

这篇关于摆动:将列表中的选定值存储到数组中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆