Solved

How can I put a CASE SWITCH on a JList?

Posted on 2008-10-21
2
274 Views
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();
            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

0
Comment
Question by:glynco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
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
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
// Get selected value
Value selection = (Value)list.getSelectedValue();
            switch(selection.num) {
            	case 1:
            		System.out.println("You selected one!");
            		break;
            }

Open in new window

0
 
LVL 5

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 107
Java exception bubble up 2 24
ejb wildfly example 2 72
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 75
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

749 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