Results 1 to 4 of 4
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    6

    Angry Double ItemEvents sent by JComboBox

    Hi.

    Here is the code:

    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
    I have a TestClass that drives this one just fine, no problems there..

    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?

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    6

    Default Re: Double ItemEvents sent by JComboBox

    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
    Hi Myself;

    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,372
    Blog Entries
    7
    Rep Power
    25

    Default Re: Double ItemEvents sent by JComboBox

    Use the symbolic version ItemEvent.SELECTED instead of the magic value '1'.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    6

    Default Re: Double ItemEvents sent by JComboBox

    Right, thanks.
    Useful tip.

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  2. Replies: 2
    Last Post: 12-10-2009, 02:58 PM
  3. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 02:51 PM
  4. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •