Results 1 to 2 of 2
Thread: multiple combo boxes
- 07-22-2010, 06:06 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
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 :)
- 07-22-2010, 11:57 AM #2
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
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
-
dialog boxes
By gedas in forum Java 2DReplies: 1Last Post: 02-19-2010, 11:23 AM -
List Boxes
By zzpprk in forum AWT / SwingReplies: 2Last Post: 12-15-2009, 09:43 PM -
Attaching multiple combo box with each other on d basis of sql 2005.
By Sachin. in forum Java ServletReplies: 6Last Post: 07-11-2009, 03:13 AM -
[SOLVED] Combo Boxes Automation
By dbashby in forum New To JavaReplies: 3Last Post: 03-27-2009, 12:39 AM -
creation of one combo box form another combo box
By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-09-2008, 03:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks