?
Solved

Help: JComboBoxes and Event handling

Posted on 2003-02-24
7
Medium Priority
?
208 Views
Last Modified: 2010-03-31
Hi,
I'm trying to create a JApplet that has several JComboBoxes. I have it designed but don't know how to get event handling to work. Does anyone know the JApplet equivalant of the following as this works with normal applets

public void itemStateChanged(ItemEvent e)
{
     if(e.getSource() instanceof Choice)
     {
          userChoice = track.getSelectedItem();
          repaint();
     }
}

note that track is just the name of a JComboBox.
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
7 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8010083
what's the problem? something like this should work:

ItemListener il = new ItemListener() {
   public void itemStateChanged(ItemEvent e)
   {
        if(e.getSource() instanceof Choice)
        {
             userChoice = track.getSelectedItem();
             repaint();
        }
   }

};

JComboBox track = new JComboBox();
track.addItemListener(il);
0
 

Author Comment

by:johnome
ID: 8010166
That works for Choices on normal applets but not on JComboBoxes for JApplets. It;s the instanceof Choice that's the problem. Do i use casting of some type?
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 8010412
why not:
 if(e.getSource() instanceof JComboBox)
0
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.

 
LVL 86

Expert Comment

by:CEHJ
ID: 8010474
Not quite sure what the difficulty is here. The only thing you should watch is that deselection events occur as well.

public class A extends JApplet implements ItemListener {

     public void init() {
          Container contentPane =getContentPane();
          try {
          Vector items = new Vector(4);
          items.add("1");
          items.add("2");
          items.add("3");
          items.add("4");
          DefaultComboBoxModel model = new DefaultComboBoxModel(items);
          JComboBox combo = new JComboBox(model);
          contentPane.add(combo, BorderLayout.NORTH);
          combo.addItemListener(this);
          }
          catch(Exception e){
               e.printStackTrace();
          }
     }

     public void itemStateChanged(ItemEvent e) {
          System.out.println(e);
     }    
}


0
 

Author Comment

by:johnome
ID: 8014904
thanks very much bobbit
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8017432
> thanks very much bobbit
no problem, but why the 'B' grade?
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 8074417
Grade changed per request ...

** Mindphaser - Community Support Moderator **
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 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