Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: ActionListener question.

  1. #1
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default ActionListener question.

    Is there any advantage in doing an ActionListener as an anonymous inner class, like this:


    jBtnSelection.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            selectionButtonPressed();
        }
    } );

    versus creating a separate class that implements ActionListener, like this:

    public class GeneratePasswordButtonListener implements ActionListener {
     
        GeneratePasswordButtonListener(){}
     
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub   
        }
    }

    And calling it thusly:

    btn.addActionListener(new GeneratePasswordButtonListener());

    The biggest problem I see is getting a return value from GeneratePasswordButtonListener()

    I'm fairly new to Java, and new to the forum, so please forgive me if this has been asked already.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ActionListener question.

    The advantage is that is saves time and space. Usually things like ActionListeners don't do all that much, so making them their own named class doesn't make a ton of sense. Do whichever fits into your brain the best and don't worry too much about it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mjr (January 27th, 2012)

  4. #3
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: ActionListener question.

    Quote Originally Posted by KevinWorkman View Post
    The advantage is that is saves time and space. Usually things like ActionListeners don't do all that much, so making them their own named class doesn't make a ton of sense. Do whichever fits into your brain the best and don't worry too much about it.
    Thanks! That makes a lot of sense.

    My initial thought was to do it where each ActionListener was a class that implemented ActionListener (like the second fragment in my first post).

    So if I had four buttons (for instance), I'd have four separate ActionListener classes.

    The problem that I ran into was putting data into a label (in a JFrame) from the ActionListener when the ActionListener is in a separate class. For instance, in the second code fragment, the actionPerformed() method might return an int, but since I'm new to Java, I don't know of any way to get the int returned to the label in the JFrame.

    That's probably not the best way to go about it, though...

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ActionListener question.

    Making ActionListener's as a separate class can make them more reusable (as opposed to the inner class syntax). Sometimes this isn't a big deal...other times it can be. Consider a simple scenario in which you have a JButton with an associated ActionListener...now consider a requirement in which you need to have a JMenuItem do the same thing - writing a separate class allows that code to be readily reusable and you can plug and play with the ActionListener to whatever (and how many) component you wish. In smaller projects this is less of an issue - in larger projects this can become important and complex.

  6. The Following User Says Thank You to copeg For This Useful Post:

    mjr (January 27th, 2012)

  7. #5
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: ActionListener question.

    Quote Originally Posted by copeg View Post
    Making ActionListener's as a separate class can make them more reusable (as opposed to the inner class syntax). Sometimes this isn't a big deal...other times it can be. Consider a simple scenario in which you have a JButton with an associated ActionListener...now consider a requirement in which you need to have a JMenuItem do the same thing - writing a separate class allows that code to be readily reusable and you can plug and play with the ActionListener to whatever (and how many) component you wish. In smaller projects this is less of an issue - in larger projects this can become important and complex.
    Well, doing it to practice reusability was my main purpose. I just couldn't figure out how to return a value from the method within the ActionListener to the label in the JFrame.

  8. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ActionListener question.

    Quote Originally Posted by mjr View Post
    The problem that I ran into was putting data into a label (in a JFrame) from the ActionListener when the ActionListener is in a separate class. For instance, in the second code fragment, the actionPerformed() method might return an int, but since I'm new to Java, I don't know of any way to get the int returned to the label in the JFrame.

    The actionPerformed() method is void, so it can't return an int or anything else, whether it's in an anonymous class or its own public class in its own file. Instead, you're going to have to make sure the JLabel is in scope in your actionPerformed() method (this would suggest a preference for an inner class, whether anonymous or not doesn't really matter). You could also call a method from the ActionListener that updates the JLabel appropriately, but the principle is the same.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mjr (January 27th, 2012)

  10. #7
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: ActionListener question.

    Quote Originally Posted by KevinWorkman View Post
    The actionPerformed() method is void, so it can't return an int or anything else, whether it's in an anonymous class or its own public class in its own file. Instead, you're going to have to make sure the JLabel is in scope in your actionPerformed() method (this would suggest a preference for an inner class, whether anonymous or not doesn't really matter). You could also call a method from the ActionListener that updates the JLabel appropriately, but the principle is the same.
    I think I follow that. And it makes a ton of sense.

    I'm curious, though, let's say that I did set up an external class, but inside of actionPerformed() is a method call, like this:

    public class GeneratePasswordButtonListener implements ActionListener {
     
        GeneratePasswordButtonListener(){}
     
        @Override
        public void actionPerformed(ActionEvent e) {
           selectionButtonPressed();
        }
    }

    Note selectionButtonPressed();

    Now, my rudimentary understanding (since I'm new to this) is that selectionButtonPressed() would have to be within the class GeneratePasswordButtonListener. Is that correct?

    And if so, if selectionButtonPressed() returns a value (for sake of argument, let's say an int), can I return it to the JLabel within the JFrame where the button resides?

    In other words, the JFrame has a button. When the button is clicked, it triggers the action listener that would call selectionButtonPressed(), which would return a value back to a JLabel in the JFrame.

    May not be possible, I'm just curious if it is. Might not even be good coding practice.

  11. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ActionListener question.

    What happened when you tried?

    But it really depends on what you mean by "return it to the JLabel". Can you set the text of a JLabel to a value returned from some method? Sure, as long as you have access to both in whatever code you're talking about.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #9
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: ActionListener question.

    Quote Originally Posted by KevinWorkman View Post
    What happened when you tried?
    Well, I couldn't figure out exactly what I needed to do.

    If I have the button that calls the external listener, like this:

    btn.addActionListener(new GeneratePasswordButtonListener());

    I don't know if there's some type of "pressed" event or something, nor do I know if I can just do this:

    myLabel.text = btn.addActionListener(new GeneratePasswordButtonListener());

    Something tells me that just won't work.

    Although I may try it later tonight and get back with you...

  13. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ActionListener question.

    Like I said, what happened when you tried?

    But I can tell you right now that that won't work. First of all, that's not how you set the text of a JLabel. Secondly, the addActionListener() method is void, so it can't return anything. You're going to need to do something like this:

    //inside your ActionListener, wherever that is
    public void actionPerformed(ActionEvent e){
       myLabel.setText("whatever");
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mjr (January 27th, 2012)

  15. #11
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: ActionListener question.

    Gotcha.

    I'm just used to how Microsoft does things.

    In C#, if I have a label, I can do myLabel.Text = "Some Text".

    And the buttons don't require ActionListeners to be defined the way Java does.

    So in C# (which I know isn't Java) I could set up a class, say MyClass, and have a method that is called calculateNumbers(), and in my button click do something like this:

    MyClass cls = new MyClass();
    int theResult;

    theResult = cls.calculateNumbers();

    And then on the form, I could do this:

    myLabel.Text = theResult;

    Thanks for being so patient with me. Java is still fairly new to me, and I'm excited to be learning it. I'm also on my way to earning a BSCS, and one of the classes is a Java class.

    This particular question isn't assignment or homework, in this case, I'm simply curious.

  16. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ActionListener question.

    Well, assuming some things (I don't know much about C#), that's not too different than how Java can work. You can set public class variables like that, but JLabel's text variable is not public. This is presumably so that JLabel can use the setText() method to also call repaint() and other such methods.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Actionlistener and actionperformed question
    By ms_ceng in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2011, 11:59 AM
  2. Help with ActionListener please
    By knightmetal in forum AWT / Java Swing
    Replies: 3
    Last Post: August 23rd, 2011, 05:41 PM
  3. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 12:25 PM
  4. New Java user ActionListener question
    By VBGuy in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 20th, 2010, 09:46 PM
  5. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 11:00 AM