How can I put a CASE SWITCH on a JList?

Posted on 2008-10-21
Last Modified: 2012-05-05
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();


            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 );




Open in new window

Question by:glynco

Accepted Solution

MatthiasVance earned 500 total points
ID: 22765527
First of all you should change the selection mode of your control, then you can use list.getSelectedValue(). Note that you cannot use a string in a switch, so you have to think of something else to do (maybe a private class that holds the string and the numerical value of it, and then switch() the numerical value).

Kind regards,

Matthias Vance
// Private Value class

private class Value {

    		public String text;

    		public int num;

    		Value(String text, int num) {

    			this.text = text;

    			this.num = num;


    		public String toString(){

				return text;



// Change selection mode


// Get selected value

Value selection = (Value)list.getSelectedValue();

            switch(selection.num) {

            	case 1:

            		System.out.println("You selected one!");



Open in new window


Expert Comment

ID: 22765826
You may use Enum instead of String if you want to use switch-case here.

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now