This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am a new programmer and not quite sure what to do to register a button click, i know about action listeners but they dont make much sense the way some the oracle website explains it. Below is my code.
Note: I have tried to do action listeners but havent been able to get it, I sense I'm missing something quite simple.
When writing your own listeners, there are couple of steps you should follow (which you did to some point). In your case your listener is implementing ActionListener interface which declares one method:
To register the listener you defined to component that should handle the event using it, you write:
Whenever user clicks myButton, that object creates ActionEvent object and calls actionPerformed() method of listener registered to it to handle the event.
Now, to review your code. What I first noticed in your code is that your listener class (DataEvent) is abstract, which means you can't instantiate it hence you can't register it to your button. If you change the class signature not to be abstract, you still didn't register your listener to the button in your gui, so the action you implemented will never get executed.
As for the approach you have chosen, you have reference to DataApplication in your listener. But you didn't initialize it, nor you provided the constructor to do that, so it's always null. Hence, if you change the code as I stated above, the click on button will be handled, but you'll get NullPointerException.
The quieter you are, the more you are able to hear.
Ok now I'm confused now about the different classes. You want me to have one for the GUI, listeners, and events.
Could somebody clarify this, the book I'm learning from did this a different way.
And if somebody could please provide me with the right code for DataEvent the COMPLETE code.
Joined: Oct 13, 2005
If a book uses addActionListener(this) frequently, then it belongs in the recycling.
You should change the name of the class, and get rid of the if bit:Note all classes should have a constructor. You would have had trouble with your empty constructor; that would have caused null values and Exceptions. Also it ought not to be abstract.
You should have the done button in the data application given private access, and a get method to provide access in other classes. That is unnecessary if you have it as a private inner class (i.e. inside the data application class), when you can reduce it to this:You can add it like thisI think you should go through your code carefully, looking at all the names of the classes and fields, because I think their names could be improved.
Joined: Oct 13, 2005
I seem to fulminate about addActionListener(this) every now and again, usually referring back to my old threads. Try herehere and here for more information. You may end up with the same old thread referenced several times.
Constructors are used to enable instantiation of the class, and to restrict instantiation of the class. If it is a private inner class, you want to restrict knowledge about it to inside the class (that means inside the outer class), so I thought it ought to have a private constructor. That restricts instantiation (at least I think it does) to inside the class which means inside the outer class. A private constructor will achieve that and you can still access the button with private access, because they are inside the same class (or even the same method). By inside the same class, I mean inside the outer class.
If you don’t supply a constructor, then the compiler will add one for itself, which might not necessarily do what you intended. That is called a default constructor. It is one of those things which the compiler will allow, but which is not regarded as good style.
I think it would work if you gave the constructor other access in the inner class. If you have a standalone public class, then the compiler would usually have public access.
If the button is pushed it goes to this class and to actionPerformed and does whatever it says, right?
If the button is pushed (an event has occurred) the listener registered to that button reacts on that event. In case of your classes, if you register your listener to doneButton:
then the code inside actionPerformed(...) method of DataEventListener class will be executed.
The line that you're asking about is getting the object on which the event has occurred (read the API). So with getSource() you get an Object instance which is actually an object that "triggered" the event. Since you know it's JButton in your case, you cast it so you can invoke setEnable(false) on it. So basically, with that line you are getting a reference to JButton that was clicked.
Ok thanks a lot, that solved my problem. But i want to know whats considered better, having the event handled immediately inside the same class or going to different class to handle the event. So like I have it right now where it goes to a different class, or like this one.
That depends on some properties of the listener. For example, when the listener is that simple that it contains one line of code, I do it by creating an anonymous inner class:
You may also want to put the action code in a separate method (even if it's just a couple of lines of code), especially when you use the same listener for more than one component, but you also want to have access to some other fields of the class where you define it:
In other cases, like when you have a lot of components in your GUI, many of them have common listeners, or when you insist following some design principles then it would appropriate to have a listener defined in a separate class.