?
Solved

Help: JComboBoxes and Case statements

Posted on 2003-02-25
3
Medium Priority
?
175 Views
Last Modified: 2010-03-31
Hi,
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();
     repaint();
}

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
0
Comment
Question by:johnome
[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
3 Comments
 
LVL 9

Accepted Solution

by:
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
  }

}
0
 
LVL 35

Expert Comment

by:TimYates
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?
  }

}
0
 

Author Comment

by:johnome
ID: 8015456
thank you very much
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 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