Results 1 to 9 of 9
Thread: two different Listeners
- 01-21-2011, 02:36 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
two different Listeners
I am trying to add a toolbar with one button on it and already have another button on the window.
andJava Code:tool1 = new JToolBar(); tool1.add(button1); button1.addActionListener(new Listener()); add(tool1);
so my problem is that i want one to do a separate action than the other but they both use the same listener. So my question is where and what do i need to put in to have two separate listeners.Java Code:JButton button = new JButton("Calculate"); button.addActionListener(new Listener()); add(button);
thanks.
- 01-21-2011, 02:44 AM #2
You can create 2 different Listener classes and attach the appropriate one to each of the buttons. Or you can use the one Listener and in the actionPerformed method use an if statement to determine which button got pressed. Or you can use anonymous classes.
Java Code:button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // add code for this buttons action doStuffForButton1(); } }); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // add code for this buttons action doStuffForButton2(); } });
-
Use a distinct ActionListener class for each of them, not the same one.
- 01-21-2011, 02:51 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
thanks you junky but i am liking fubarable's solution better. I was trying to achieve this but how do i tell actionPerformed which is different?
- 01-21-2011, 02:53 AM #5
I'm still feeling in a good mood.
Java Code:if(event.getSource() == button1) { } else if(event.getSource() == button2) { }
- 01-21-2011, 02:55 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
o ok, thank you. didnt mean to put you in a bad one.
- 01-21-2011, 03:00 AM #7
No, it's not that.
I just don't usually provide code because I think people learn better by going away with a hint provided and try and write the code themselves. Then if they come back they can post their code and we can provide further hints as to how to fix it.
- 01-21-2011, 03:07 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
o, ok. well thanks anyway. I dont usually get it when beating around the bush so thanks. I guess last thing would be is their a way to make it so that you have two seperate
with something else in the ActionEvent e slot that will tell it weather to use that for the button pushed or not. something likeJava Code:public void actionPerformed(ActionEvent e) { }is that at all possible? and thanks for the helpJava Code:public void actionPerformed(button1) { stuff for this button } public void actionPerformed(button2) { stuff for this button }
- 01-21-2011, 03:20 AM #9
No you cannot do that. I repeat what I said in my first post with a bit of clarification.
Option 1: Two classes.
Option 2: One listenerJava Code:class ListenerOne implements ActionListener { public void actionPerformed(ActionEvent event) { .... } } class ListenerTwo implements ActionListener { public void actionPerformed(ActionEvent event) { .... } } buttonOne.addActionListener(new ListenerOne()); buttonTwo.addActionListener(new ListenerTwo());
Option 3: Anonymous classes as per my code in my first replyJava Code:class Listener implements ActionListener { public void actionPerformed(ActionEvent event) { if(event.getSource() == button1) { .... } else if(event.getSource() == button2) { .... } } } Listener listen = new Listener(); buttonOne.addActionListener(listener); buttonTwo.addActionListener(listener);
Similar Threads
-
Listeners and events
By cglacet in forum New To JavaReplies: 3Last Post: 01-20-2011, 02:21 PM -
key listeners
By zjames in forum New To JavaReplies: 22Last Post: 11-24-2010, 05:58 AM -
Can't implement listeners
By xael in forum New To JavaReplies: 3Last Post: 09-02-2010, 06:33 PM -
Help with Listeners
By Psyclone in forum AWT / SwingReplies: 8Last Post: 02-09-2010, 07:21 PM -
Seriously need help on my listeners!!
By themburu in forum Java AppletsReplies: 4Last Post: 05-26-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks