Results 1 to 3 of 3
  1. #1
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default 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.....

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default

    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.

    I haven't been able to find a example of adding the actionListener without using inner class.
    All I can say to that is you haven't looked very hard.
    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

  3. #3
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    thanks Darryl again. Maybe all the issue resides on my design. Basically what I want to do is invoke a method which is from a different class but I'll have a look to the URL's you send me and hopefully find the anwer. thanks again!!!.

Similar Threads

  1. Replies: 6
    Last Post: 02-01-2010, 09:30 AM
  2. MouseListener / ActionListener in another class
    By newmanity in forum New To Java
    Replies: 9
    Last Post: 12-07-2009, 03:57 AM
  3. Adding 2 Implements to the class
    By jboy in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 05:19 AM
  4. exception error in a ActionListener class
    By aserothbw in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2009, 12:59 PM
  5. exception error in a ActionListener class
    By aserothbw in forum AWT / Swing
    Replies: 6
    Last Post: 09-04-2009, 03:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •