Link to home
Start Free TrialLog in
Avatar of glynco
glynco

asked on

How can I put a CASE SWITCH on a JList?

How can I put a case switch on JList selection entered?  

case one:
case two:
case three:
case four:
case five:
import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
 
    public class Lister {
      public static void main(String[] args) {
        JFrame frame = new JFrame("Lister v1.0");
 
 
        String [] items = { "one", "two", "three", "four", "five" };
 
 
 
        final JList list = new JList(items);
 
        // create a button; when it's pressed, print out
        // the selection in the list
        JButton button = new JButton("Per favore");
        button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            Object[] selection = list.getSelectedValues();
            System.out.println("-----");
            for ( Object o : selection )
              System.out.println( o );
          }
        });
 
        // put the controls the content pane
        Container c = frame.getContentPane();  // unnecessary  in 5.0+
        JPanel comboPanel = new JPanel();
        c.add(new JScrollPane(list), BorderLayout.CENTER);
        c.add(button, BorderLayout.SOUTH);
 
        frame.setSize(200, 200);
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible(true);
      }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MatthiasVance
MatthiasVance
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may use Enum instead of String if you want to use switch-case here.