Results 1 to 2 of 2
  1. #1
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default multiple combo boxes

    hi i have a gui which has 2 JcomboBox's, and i pass them in a control class which either implements ActionListener or ItemListener

    ie

    Java Code:
        public class Control implements ActionListener,ItemListener{
    
              DefaultComboBoxModel clientNumberModel = new DefaultComboBoxModel ();
              DefaultComboBoxModel itemNumberModel = new DefaultComboBoxModel ();
    
        public Control()
        {
          ......
          // add the data to the models 
          setupComboModels();
    
        }
    
         public ActionPerformed(actionEvent e)
         {
               if(e.getActionCommand.equalsIgnoreCade("clientNumber))
               {
                     processClientSelection();
    
                }
              else
              if(e.getActionCommand.equalsIgnoreCade("itemNumber))
               {
                     processItemSelection();
    
                }
          }
        }
    
       public class gui extends JFrame()
       {     
            public gui(Control control)
            {
            .....
            setUpCombos();
            }
    
            public void setUpCombo's()
            {
                  .....
                  JcomboBox clientNumbersCombo = new Jcombobox(control.getclientNumbersModel());
                   clientNumbersCombo .addActionListener(control);
                   clientNumbersCombo .addItemListener(control);
    
                  JcomboBox itemNumbersCombo = new Jcombobox(control.getitemNumbersModel());
    
                  itemNumbersCombo .addActionListener(control);
                  itemNumbersCombo .addItemListener(control);
             }
       }

    my problem is, that when a user selects from one combo, both combo's fire an event, so if i use the above code you cant differentiate the source, and the above if else statement won't work ?

    also if i use the item listener, there is no tag like actionCommand() which you can differentiate the source of the event ?

    my solution has been to seperate out into different control objects but that kinda seems the long way round ?

    is there a way to easily identify the source of the JcomboBox ?

    thanks in advance :)

  2. #2
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    is it weird to answer your own posts ??

    to late....

    this lets your work out the source of the combo event...

    Java Code:
    public void itemStateChanged(ItemEvent ev) {
    		System.out.println(" item changed state ");
    		
    		Object item = ev.getItem(); 
    		JComboBox box = (JComboBox) ev.getSource();
    		
    		System.out.println(" event action command   " + box.getActionCommand());
    		
    		
    		
    		if (ev.getStateChange() == ItemEvent.SELECTED) 
    		{ 
    			System.out.println(" the item selected " + item.toString());
    			String selectedItem =  item.toString();
    			
    			if(box.getActionCommand().equalsIgnoreCase("clientNumbersSelection"))
    			{
    				
    				System.out.println("clientNumbersSelection only" );
    				
    			}
    			else if(box.getActionCommand().equalsIgnoreCase("attritionRate"))
    			{
    				System.out.println("attritionRate only" );
    			}
    			
    		}
    		
    	}

Similar Threads

  1. dialog boxes
    By gedas in forum Java 2D
    Replies: 1
    Last Post: 02-19-2010, 12:23 PM
  2. List Boxes
    By zzpprk in forum AWT / Swing
    Replies: 2
    Last Post: 12-15-2009, 10:43 PM
  3. Replies: 6
    Last Post: 07-11-2009, 04:13 AM
  4. [SOLVED] Combo Boxes Automation
    By dbashby in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 01:39 AM
  5. creation of one combo box form another combo box
    By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-09-2008, 04:14 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
  •