Hi all, I've been trying to get this done but really don't know how to.
I have a button action listener implementation inside a combobox itemlistener implementation.
My problem is whenever i select a combobox event my button listener is activating.I want my button listener to be activated only once when i hit my button.
when I select my editable combobox item for third time and i was hitting my button only this time but it is activating three times,where it supposed to be activate only once,which is not the case i needed.
can anyone help me why this is happening and what to do to avoid this.
Here is the code from my program.
/**code**/
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Test extends JPanel implements ItemListener{
JComboBox jc = new JComboBox();
JButton button = new JButton("submit");
public Test() {
jc.addItem("France");
jc.addItem("Germany");
jc.addItem("Italy");
jc.addItem("Japan");
jc.addItemListener(this);
add(jc);
add(button);
}
public void itemStateChanged(ItemEvent ievent) {
if (ievent.getStateChange() == ItemEvent.SELECTED) {
button.setEnabled(true);
String str = (String) jc.getSelectedItem();
System.out.println("ComboBox "+str);
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("submit")) {
System.out.println("Sequences after pressing button");
}
}
});//end button action listener
}
}//end Combobox itemlistener
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new Test());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
/**code**/
Case1:
when I select France from combobox,
This is the output Iam getting:
France
whenI press the submit button
output is:
Sequences after pressing button
case 2:
next time when I select Italy from combobox,
output is:
Italy
and after pressing submit button
output is:
Sequences after pressing button
Sequences after pressing button
case 3:
If i select another item from combobox example Germany,
output is:
Germany
and after pressing submit button
output is:
Sequences after pressing button
Sequences after pressing button
Sequences after pressing button
conclusion:
so in case 2 and case 3 submit button is activating twice and thrice respectively,which is not the case needed for me.
I want the button should activate only once, since iam pressing the button only once every time.
Thankyou,
dina

