I am trying to use a for loop inside a ListSelectionListener event to build a string to set to a Jlabel. It works kind of, it'll build the string and the JLabel will update but not of the user selected items. For example, I'd select "Iceland" in the program, but the program would select United States since it's the first index and then if i continue to select other ones, it goes onto index 1, 2, etc instead of the ones I chose. Here is the code.
public class guiMain {
private static void constructGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
GUI frame = new GUI();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
constructGUI();
}
});
}
}
class MyActionListener implements ActionListener {
GUI fr;
public MyActionListener(GUI frame) {
fr = frame;
}
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String selection = (String) cb.getItemAt(cb.getSelectedIndex());
System.out.println(selection);
if(selection.contentEquals("Multiple")) { //user choice of selecting "Single" or "Multiple" in JList
fr.ctryJList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
else {
fr.ctryJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
}
}
class GUI extends JFrame {
public JLabel userSelected;
public JList ctryJList;
public GUI() {
super("The Country Combo");
init();
}
private void init() {
//array for country choices
String[] countries = {"United States of America", "Italy", "England", "Iceland", "Netherlands",
"Sweden", "Switzerland", "Canada", "New Zealand"};
//array for choice of selecting one, or multiple countries
String[] selection = {"Single", "Multiple"};
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox comboBox = new JComboBox(selection); //creation of ComboBox
comboBox.addActionListener(new MyActionListener(this));
userSelected = new JLabel("Selected items will appear here");
ctryJList = new JList(countries); //creation of JList
ctryJList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e){
ListSelectionModel lst = (ListSelectionModel) e.getSource();
int[] selectedIndices = ctryJList.getSelectedIndices();
String output = "Selected Items: ";
for(int i = 0; i < selectedIndices.length; i++) {
output = output + (String)ctryJList.getModel().getElementAt(i) + " ";
userSelected.setText(output);
}
System.out.println("Your countries are: " + output);
}
});
this.pack();
JComponent panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(new JLabel("Choose Selection Mode:"));
panel.add(comboBox);
this.getContentPane().add(panel,BorderLayout.NORTH);
this.getContentPane().add(ctryJList,BorderLayout.WEST);
this.getContentPane().add(userSelected, BorderLayout.SOUTH);
this.pack();
//TODO: Create JLabel for selection
}
}
output = output + (String)ctryJList.getModel().getElementAt(i) ...
You are getting the values at index 0, 1, 2... since "i" always starts at 0.
You want the index of the selected item so you need to get the index from your array:
output = output + (String)ctryJList.getModel().getElementAt(selectedindices[i]) ...
Also, don't use string concatenation to build your output string. Instead use a StringBuffer
or StringBuilder
and use the append(...)
method. Or maybe use the StringJoiner
.