my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Event Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event Handling" Watch "Event Handling" New topic
Author

Event Handling

Milind Deodhar
Ranch Hand

Joined: Dec 06, 2000
Posts: 32
I am trying to do a small things.
I have a table like this
Type1 SubType1 Temp1 SubTemp1
Type1 SubType2 Temp2 SubTemp2

I have created a frame where there are 2 comboboxes and 2 textfield
COmboBox1 wil have field 1 and combobox will have field2.
Depending on the valuess chosen in field1 the combobox2 should get updated and depending on the 2 comboboxes the textfield should get field
I have writen ItemStatehanged event, When the item state of the first combo box , first thing I do it
Combobox2.removeAllItems();, then go ahead the fill the Items of combobox2 with new values depending on combobox1 selected.
But the problem I am having is when I change the item in First combo box , the removeAllItems(); fires the event on the second box also and then it creates a problem.
I tried everything, nothing works.
Ccan anybody read the code which I am attaching and give me the solution. or if anyone has a other solution to implement this please help me.
class SymItem implements java.awt.event.ItemListener
{
public void itemStateChanged(java.awt.event.ItemEvent
event)
{
Object object = event.getSource();
if (object == JComboBox1){
JComboBox1_itemStateChanged(event);}
else if (object == JComboBox2){
JComboBox2_itemStateChanged(event);}
}
}
void JComboBox1_itemStateChange (java.awt.event.ItemEvent event)
{
JComboBox2.removeAllItems();
fillCombo2(JComboBox1.getSelectedItem().toString());
fillOther(JComboBox1.getSelectedItem().toString(),JComboBox2.getSelectedItem().toString());

}
void JComboBox2_itemStateChanged(java.awt.event.ItemEvent event)
{

fillOther(JComboBox1.getSelectedItem().toString() ,JComboBox2.getSelectedItem().toString());
}

Here fillComboBox2 , reads the database and fills the second combobox and fillOther is suppose to fill the other 2 textfields.

}
}
[This message has been edited by Milind Deodhar (edited March 28, 2001).]
[This message has been edited by Milind Deodhar (edited March 28, 2001).]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I'm not sure if it's what you want, but here is something.
I too had a great deal of trouble with this code, it was constantly throwing exceptions and misbehaving, until I made the discovery that is included in a rather large comment.

[This message has been edited by Mike Curwen (edited March 28, 2001).]
Milind Deodhar
Ranch Hand

Joined: Dec 06, 2000
Posts: 32
Thanks a lot for your reply
N Mohammed
Greenhorn

Joined: Sep 10, 2003
Posts: 1
I'm using similar code with comboboxes. Every time an item is selected from a combobox, a new query is executed and it populates other comboboxes and the second combobox affects the third...
I am using ActionListeners instead of ItemListeners and things seem to be working alright. Thanks for that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Event Handling
 
Similar Threads
JTable selected row problem
Need help in Joining 3 combo boxes
regarding combobox in frame
Netbeans GUI Code Doubt
JTables