Results 1 to 3 of 3
- 10-07-2010, 10:05 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 24
- Rep Power
- 0
adding a actionListener but not using inner class
Hi ,
I just wonder if I can add ActionListener to a JButton without having to implement it using an inner class. My Code is something like:
Java Code:purchaseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("PURCHASE" )&& user!= null && stationFrom != null && stationTo != null && startTime != null && endTime != null) { text.setText("PURCHASE SELECTED"); } else { text.setText("Some space are in blank PURCHASE SELECTED"); } } });
and I really need to implement this not as a inner class because I need to invoke another methods on other classes and everytime when I do this using the inner class basically gives me an errors and ask to define any variable inside the inner class a final variable. I haven't been able to find a example of adding the actionListener without using inner class. Can this is be done. Thanks.....
- 10-07-2010, 10:35 AM #2
Anything you can do with an anonymous inner class you can do with a named, top level / nested / inner class. In this case, all that you need to ensure is that the argument to addActionListener(...) is-a ActionListener: it is an instance of a class that implements ActionListener.
Do realize that you anyhow can't access variables declared inside a method except from inside that method.
All I can say to that is you haven't looked very hard.I haven't been able to find a example of adding the actionListener without using inner class.
How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
db
- 10-08-2010, 07:24 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
one inner class(an interface (ActionListener))) for multiple actions
By bigj in forum New To JavaReplies: 6Last Post: 02-01-2010, 09:30 AM -
MouseListener / ActionListener in another class
By newmanity in forum New To JavaReplies: 9Last Post: 12-07-2009, 03:57 AM -
Adding 2 Implements to the class
By jboy in forum New To JavaReplies: 2Last Post: 10-23-2009, 05:19 AM -
exception error in a ActionListener class
By aserothbw in forum AWT / SwingReplies: 3Last Post: 09-22-2009, 12:59 PM -
exception error in a ActionListener class
By aserothbw in forum AWT / SwingReplies: 6Last Post: 09-04-2009, 03:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks