jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonAddActionPerformed(evt);
}
});
[java] Please explain this Java code to me.
I am trying to learn Java right now and the only time I’ve ever used it was in a small intro course at a community college a few years ago. I am experimenting with NetBeans right now and it generated the following code:
What exactly is going on here? Is it looks like it is creating a temporary derived class and overriding the constructor and the actionPerformed function. Is that what is going on?
Also, is there an easy way I can run non applet Java programs without having to use the Command Prompt or the IDE?
Quote:
What exactly is going on here?
It is creating a new ActionListener object, and overriding the actionPerformed method, then passing the newly created object to the addActionListener method. The other option would be to simply create an entire new class extending the ActionListener object and overriding the actionPerformed method, then creating a new instance of the object to pass to addActionListener. However, if you used this method with several different buttons, you would need to create separate classes for each one, which would create more unnecessary code and clutter.
In case you were wondering what it's technically called, it's an anonymous class. Feel free to google the term for several more articles on it.
Quote:Original post by ManaStone
Also, is there an easy way I can run non applet Java programs without having to use the Command Prompt or the IDE?
Assuming you mean desktop applications:
1. Make a batch file (.bat or .sh) containing the command (either java <class> or java -jar <jarfile>
2. Make an executable jar, which at least on windows can be executed on double click (if you assigned 'java.exe' to the '.jar' extension).
The same is true for command line applications, but you wouldn't get the output. ;)
For other application types (JEE components, Midlets, Servlets, ...) there are other ways. ;)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement