Results 1 to 6 of 6
  1. #1
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default repopulate ComboBox

    I want to repopulate a combobox every time when a user selects an item from the combo box. In the addItemListener, itemStateChanged method, I do removeAllItems first, then do addItems. Unfortunately this doesn't work because it got me into an infinite loop.

    Can somebody tell me how to do this?

    Thanks!

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Remove the listener before you change the contents and add it after you're done.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    I initialize all the listeners on the start up of the program in the following way. How do I stop the listener and restart it? Can I stop the listener inside the statechange routine? Thanks!

    Java Code:
    protected void initListeners()
    {	 
    	// ComboBox listener 
    	filterNamesCombo.addItemListener( new ItemListener()
    	{
    		public void itemStateChanged( ItemEvent event )
    		{				
    			// Determine whether an item is selected
    			if (event.getStateChange() == ItemEvent.SELECTED)
    			{
    				// Stop listener
    				// remove all items from the combo box
    				// repopulate new items
    				// restart listener
    			}
    		}
    	});
    
    	// OK button listener
    	
    	// Cancel button listener
    	
    }

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Quote Originally Posted by new2java2009 View Post
    Can I stop the listener inside the statechange routine?
    No, you don't have a reference to the listener. You'd need something like this:
    Java Code:
    public class Clazz {
    	JComboBox filterNamesCombo = new JComboBox();
    	ItemListener comboListener = null;
    	protected void initListeners()
    	{	 
    		comboListener = new ItemListener()
    		{
    			public void itemStateChanged( ItemEvent event )
    			{				
    				// Determine whether an item is selected
    				if (event.getStateChange() == ItemEvent.SELECTED)
    				{
    					// Stop listener
    					filterNamesCombo.removeItemListener(comboListener);
    					// remove all items from the combo box
    					// repopulate new items
    					// restart listener
    					filterNamesCombo.addItemListener(comboListener);
    				}
    			}
    		};
    		// ComboBox listener 
    		filterNamesCombo.addItemListener(comboListener);
    	}
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Smile

    Thank you so much PhHein. It's working nicely. :)

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

Similar Threads

  1. Input box with combobox within
    By cowboy in forum New To Java
    Replies: 4
    Last Post: 10-23-2009, 03:07 PM
  2. problem in combobox
    By 435.mahesh in forum AWT / Swing
    Replies: 3
    Last Post: 05-04-2009, 06:12 AM
  3. Need Help combobox
    By kwink in forum AWT / Swing
    Replies: 3
    Last Post: 03-21-2009, 11:05 AM
  4. want default value in ComboBox from DB!!
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-04-2008, 01:25 AM
  5. combobox
    By chandu.v in forum New To Java
    Replies: 2
    Last Post: 07-02-2008, 09:36 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
  •