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.

January 27th, 2012, 12:04 PM

KevinWorkman

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.

January 27th, 2012, 12:35 PM

mjr

Re: ActionListener question.

Quote:

Originally Posted by KevinWorkman

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

January 27th, 2012, 12:54 PM

copeg

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.

January 27th, 2012, 01:05 PM

mjr

Re: ActionListener question.

Quote:

Originally Posted by copeg

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.

January 27th, 2012, 01:07 PM

KevinWorkman

Re: ActionListener question.

Quote:

Originally Posted by mjr

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.

January 27th, 2012, 01:47 PM

mjr

Re: ActionListener question.

Quote:

Originally Posted by KevinWorkman

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:

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.

January 27th, 2012, 02:32 PM

KevinWorkman

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.

January 27th, 2012, 02:44 PM

mjr

Re: ActionListener question.

Quote:

Originally Posted by KevinWorkman

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:

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:

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.

January 27th, 2012, 07:59 PM

KevinWorkman

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.