Results 1 to 8 of 8
Thread: combobox
- 05-04-2011, 06:36 PM #1
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
combobox
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,
dinaLast edited by dina; 05-07-2011 at 04:57 PM. Reason: added output data
- 05-04-2011, 07:18 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 05-04-2011, 08:46 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Post your Short, Self Contained, Correct Example that demonstrates the problem.
- 05-07-2011, 02:40 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
1) Use the "Code" tags when you post code so the code retains its formatting.
2) Seems to work fine for me. Nothing repeats 3 times.
- 05-09-2011, 11:00 PM #5
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
can you clarify my doubt about combobox which i posted earlier
hello camickr,Thankyou for your kind suggestion to improve my way of posting question,As this is the first time iam posting question like this to get help from other members,your suggestions really need to me.
I hope, now you understand my problem in my code, after adding output.
Can you please clarify my doubt,why is it happening so and what to do to get desired output.
Thankyou,
dina
- 05-10-2011, 02:41 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Those are not code tags. When you post a qestion you see all kinds of buttons like "B" for bold, "I" for Italic. Well, search for the one that displays a tooltip of "Wrap [code] tags around selected text" and use that button.
Don't add the ActionListener to the button in the itemStateChanged() method.
Instead add the ActionListener to the button when you create the button.
-
You don't want to add an ActionListener from within a SelectionListener, since each time you select something, you add another ActionListener, and this would explain why the ActionListener is firing more than once -- there are several listeners being added. Instead change the state of your program, say by setting a boolean field and then have the button's action listener use that field to decide what it wants to do.
edit: too slow!
- 05-11-2011, 05:31 PM #8
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
combobox
By leader in forum New To JavaReplies: 2Last Post: 02-12-2011, 09:30 PM -
Combobox Problem?
By waqasahmed_03235001393 in forum Advanced JavaReplies: 1Last Post: 07-27-2010, 10:56 AM -
Problem in combobox
By santhosh_el in forum AWT / SwingReplies: 4Last Post: 04-03-2009, 04:01 AM -
Need Help combobox
By kwink in forum AWT / SwingReplies: 3Last Post: 03-21-2009, 10:05 AM -
combobox
By chandu.v in forum New To JavaReplies: 2Last Post: 07-02-2008, 08:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks