Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Help: JComboBoxes and Case statements

Posted on 2003-02-25
Medium Priority
Last Modified: 2010-03-31
I was wondering if anyone could give me any help with this.I have a JComboBox with say 7 items. These are all added to the JComboBox and the ComboBox also has an itemlistener. I then have an itemstatechanged method and i get the instance of the JComboBox using

if(ie.getSource() instanceof JComboBox)
     //userChoice = track.getSelectedItem();

Say, the items in the JComboBox are called ab, cd, ef, gh, ij, kl and mn. I want to have a case statement so if ab is selected i go off and do something else like work out some calculation. Is this possible?
Thanks for your time
Question by:johnome

Accepted Solution

yongsing earned 400 total points
ID: 8015319
if (ie.getSource() instanceof JComboBox) {
  JComboBox comboBox = (JComboBox)ie.getSource();
  String selectedItem = comboBox.getSelectedItem().toString();
  if (selectedItem.equals("ab")) {
    // do something
  } else if (selectedItem.equals("cd")) {
    // do something
  } else if (selectedItem.equals("ef")) {
    // do something
  } else if (selectedItem.equals("gh")) {
    // do something
  } else if (selectedItem.equals("ij")) {
    // do something
  } else if (selectedItem.equals("kl")) {
    // do something
  } else if (selectedItem.equals("mn")) {
    // do something

LVL 35

Expert Comment

ID: 8015370
if (ie.getSource() instanceof JComboBox)
  JComboBox comboBox = (JComboBox)ie.getSource();
  switch( comboBox.getSelectedIndex() )
    case 0 :
        // ab
        break ;
    case 1 :
        // cd
        break ;
    case 2 :
        // ef
        break ;
    case 3 :
        // gh
        break ;
    case 4 :
        // ij
        break ;
    case 5 :
        // kl
        break ;
    case 6 :
        // mn
        break ;
    default :
        // Nothing selected?


Author Comment

ID: 8015456
thank you very much

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

572 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