Results 1 to 4 of 4
- 12-25-2011, 04:07 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Double ItemEvents sent by JComboBox
Hi.
Here is the code:
I have a TestClass that drives this one just fine, no problems there..Java Code:import java.awt.FlowLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class MyComboBoxClass extends JFrame { private JComboBox myJComboBox; private JLabel myJLabel; private static final String[] names = {"","Simpons", "Beavies & Butthead", "South Park", "Ninja Turtles", "Gone With The Wind", "Dilbert"}; public MyComboBoxClass() { super( "Which is not a cartoon? "); setLayout( new FlowLayout() ); myJLabel = new JLabel( "Which is not a cartoon? "); this.add(myJLabel); myJComboBox = new JComboBox(); myJComboBox.setMaximumRowCount(4); for(int i = 0; i<names.length;i++) { String tobeadded = (names[i]); myJComboBox.addItem(tobeadded); } myJComboBox.addItemListener(new Listener()); this.add(myJComboBox); }// end MyComboBoxClass constructor public class Listener implements ItemListener { public void itemStateChanged(ItemEvent e) { //System.out.println("Get Item: " + e.getItem()); //System.out.println("Get State Change: " + e.getStateChange()); if(e.getItem().equals("Gone With The Wind") && e.getStateChange() == 1) { System.out.println("Congrats!"); JOptionPane.showMessageDialog(null,"Congrats"); } else { System.out.println("Try Again!"); JOptionPane.showMessageDialog(null,"Try again!"); } }//end method itemStateChanged( ItemEvent e ) }// end class Listener implements ItemListener }//end class MyComboBoxClass extends JFrame
My question is:
Basically when I pick Simpons from the Combobox: Two JOptionPanes are seen saying Try again!
When I pick Gone With The Wind, I first get a "Try Again" JOptionPane, and then a "Congrats!" one.
So obviously the ItemEvents are sent to my listener, one for the option that is being UNSELECTED, and one for the one that is SELECTED.
But I do not care about the UNSELECTED one, I only care about the SELECTED one.
Any help?
- 12-25-2011, 04:11 PM #2
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Double ItemEvents sent by JComboBox
Hi Myself;Java Code:public class Listener implements ItemListener { public void itemStateChanged(ItemEvent e) { System.out.println("Get Item: " + e.getItem()); System.out.println("Get State Change: " + e.getStateChange()); if(e.getStateChange()!=1) { } else { if(e.getItem().equals("Gone With The Wind") && e.getStateChange() == 1) { //System.out.println("Congrats!"); JOptionPane.showMessageDialog(null,"Congrats"); } else { //System.out.println("Try Again!"); JOptionPane.showMessageDialog(null,"Try again!"); } } }//end method itemStateChanged( ItemEvent e ) }// end class Listener implements ItemListener }//end class MyComboBoxClass extends JFrame
You have just modified your code, and the problem is solved.
Congrats!
:)
( I came up with the solution after 4 seconds I asked it. )
Thank you, myself.
- 12-25-2011, 04:15 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Double ItemEvents sent by JComboBox
Use the symbolic version ItemEvent.SELECTED instead of the magic value '1'.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-25-2011, 04:16 PM #4
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
double a * double b = weird output
By GPB in forum New To JavaReplies: 3Last Post: 03-26-2010, 10:40 AM -
Activate JComboBox 1 when object is selected in JComboBox 2...
By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 01:58 PM -
Check if double is double
By marshalthrone in forum New To JavaReplies: 8Last Post: 09-30-2009, 02:51 PM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks