javaswingjbuttonjlist

How Can I Use Selected Values from List?


I've currently created a multiple interval selection JList, and I want to be able to DO something with the selections. Currently, my code only is able to create a JList where I can "highlight" selected objects; however, what I want to do is put the selected objects into an Array or ArrayList so I can perform some work with those objects. I've searched online and found out about getSelectedValueList(), but I don't really know how to use and access it for later.

Another question I have is that I'm thinking about creating a JButton called "Submit" but I'm wondering what I should do with the window with the JList, also. Should I make it exit after the button is pressed?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.util.List;

    public class ClassSelection extends JFrame {
         private JList classList;
         private String[] classNames = {"Programming Foundations I", 
         "Programming Foundations II",
         "Digital Design", "Computer Organization", "Programming 
         Paradigms", 
         "Honors Programming Paradigms", "Cluster Computing", 
         "Software Engineering"};

     public ClassSelection() {
         super("Classes Taken:");

         Container container = getContentPane();
         container.setLayout(new FlowLayout());

         classList = new JList(classNames);
         classList.setVisibleRowCount(5);
         classList.setFixedCellHeight(45);
         classList.setFixedCellWidth(450);

classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTIO N);

         container.add(new JScrollPane(classList));
         setSize(600, 600);
         setVisible(true);

    }

    public static void main(String[] args) {
         ClassSelection application = new ClassSelection();
    }
}

Solution

  • Here are 2 ways you can do this. I personally think "Approach 1" is enough. But if you really want to collect the selected items before user presses the button, then use "Approach 2".

    Approach 1 - Call getSelectedValuesList() in button's action listener

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.List;
    
    public class ClassSelection extends JFrame {
      private JList classList;
      private String[] classNames = {"Programming Foundations I",
          "Programming Foundations II",
          "Digital Design", "Computer Organization",
          "Programming Paradigms",
          "Honors Programming Paradigms", "Cluster Computing",
          "Software Engineering"};
    
      public ClassSelection() {
        super("Classes Taken:");
    
        Container container = getContentPane();
        container.setLayout(new FlowLayout());
    
        classList = new JList(classNames);
        classList.setVisibleRowCount(5);
        classList.setFixedCellHeight(45);
        classList.setFixedCellWidth(450);
    
        classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    
        container.add(new JScrollPane(classList));
    
        JButton button = new JButton("Print selected items");
        button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            List selectedItems = classList.getSelectedValuesList();
            System.out.println(selectedItems);
          }
        });
        container.add(button, BorderLayout.SOUTH);
    
        setSize(600, 600);
        setVisible(true);
    
      }
    
      public static void main(String[] args) {
        ClassSelection application = new ClassSelection();
      }
    }
    

    Approach 2 - Use a ListSelectionListener

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import java.util.*;
    import java.util.List;
    
    public class ClassSelection2 extends JFrame {
      private JList classList;
      private List<String> selectedItemsList = new ArrayList<>();
    
      private String[] classNames = {"Programming Foundations I",
          "Programming Foundations II",
          "Digital Design", "Computer Organization",
          "Programming Paradigms",
          "Honors Programming Paradigms", "Cluster Computing",
          "Software Engineering"};
    
      public ClassSelection2() {
        super("Classes Taken:");
    
        Container container = getContentPane();
        container.setLayout(new FlowLayout());
    
        classList = new JList(classNames);
        classList.setVisibleRowCount(5);
        classList.setFixedCellHeight(45);
        classList.setFixedCellWidth(450);
    
        classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        classList.addListSelectionListener(new ListSelectionListener() {
          @Override
          public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
              selectedItemsList.clear();
              selectedItemsList.addAll(classList.getSelectedValuesList());
            }
          }
        });
    
        container.add(new JScrollPane(classList));
    
        JButton button = new JButton("Print selected items");
        button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            System.out.println(selectedItemsList);
          }
        });
        container.add(button, BorderLayout.SOUTH);
    
        setSize(600, 600);
        setVisible(true);
    
      }
    
      public static void main(String[] args) {
        ClassSelection2 application = new ClassSelection2();
      }
    }