I use this part of the code to get the select items from a jlist into a string list. I get the following result:
[[String1, String2, String3,...]]
How can I avoid the double []? Thanks
static List<String> strlist = new ArrayList<String>();
public class tog {
List<String> strlisttemp = new ArrayList<String>();
final JList list = new JList(strlisttemp.toArray());
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
final ListSelectionListener listSelectionListener = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
String lstr = list.getSelectedValuesList().toString();
System.out.println(lstr);
strlist = Arrays.asList(lstr.split("\\s*,\\s*"));
}
};
list.addListSelectionListener(listSelectionListener);
JOptionPane.showMessageDialog(null, list, "Select", JOptionPane.QUESTION_MESSAGE);
System.out.println(strlist);
}
This is the part that has problem: When I print lstr it works properly [...]. When I use this:
strlist = Arrays.asList(lstr.split("\\s*,\\s*"));
Then System.out.println(strlist); prints double brackets
list.getSelectedValuesList().toString(). You are not using the generic interface forJListand you are parsing theStringwhen you can get aList<E>fromgetSelectedValuesList().