Results 1 to 4 of 4
  1. #1
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default ItemListener problem with JComboBox

    I cant get to the bottom of this problem. I have an ItemListener on a JComboBox, the problem is whenever I change the item in the list the listener handler class produces two outputs instead of one. It's like it is running the event twice :confused:

    The code in red is printed out twice. Thats the problem basically.

    Any help apprecitated

    Java Code:
    import java.awt.GridBagLayout;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.*;
    
    public class ItemListenerTrouble {
    
    	private JFrame frame;
    	private JPanel panel;
    	private JComboBox comboBox;
    	private String[] words={"hello","goodbye","yes","no"};
    	public ItemListenerTrouble()
    	{
    		frame=new JFrame("Create Your Team");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(600,400);
    		
    		panel=new JPanel();
    		panel.setLayout(new GridBagLayout());
    		
    		comboBox=new JComboBox(words);
    		ItemHandler ihandle=new ItemHandler();
    		comboBox.addItemListener(ihandle);
    		
    		
    		panel.add(comboBox);
    		
    		frame.getContentPane().add(panel);
    		frame.setVisible(true);
    	}
    	
    	private class ItemHandler implements ItemListener
    	{
    
    		public void itemStateChanged(ItemEvent event) 
    		{
    			if(event.getSource().equals(comboBox))
    			{
    				[COLOR="Red"]System.out.println("hello");[/COLOR]
    			}
    		}
    		
    	}
    	
    	public static void main(String args[])
    	{
    		new ItemListenerTrouble();
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no pro at this, but what if you change your listener like so:
    Java Code:
      private class ItemHandler implements ItemListener {
        public void itemStateChanged(ItemEvent event) {
          if (event.getSource().equals(comboBox)) {
            if (event.getStateChange() == ItemEvent.SELECTED) {
              System.out.println("hello.  Item Selected: " + comboBox.getSelectedItem());
            }
          }
        }
      }

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Brilliant as always Fubarable!! The funny thing is I always used to use .getSelectedItem() when I wanted the a comboBox selection after a button was pressed. First time using the ItemListener. Right back to the project :D

    Thanks Fubarable

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Serious Problem with JComboBox
    By Plissken in forum New To Java
    Replies: 3
    Last Post: 02-02-2010, 11:48 AM
  2. Accessibility of JComboBox problem
    By abedules78 in forum AWT / Swing
    Replies: 0
    Last Post: 12-26-2008, 02:11 PM
  3. Problem with JComboBox and Jlist
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 05-07-2008, 09:58 PM
  4. Demonstrating the ItemListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 09:25 PM
  5. How to use ItemListener for CheckBox class
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 09:18 PM

Posting Permissions

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