No such 'listener' was found in the API for 1.6 or 1.5Quote:
A listener object is an instance of a class that implements a special
interface called (naturally enough) a listener interface.
:confused:
Printable View
No such 'listener' was found in the API for 1.6 or 1.5Quote:
A listener object is an instance of a class that implements a special
interface called (naturally enough) a listener interface.
:confused:
What 'listener' are we talking about?
It's an object that you create to handle events (e.g. mouse clicks, keyboard buttons pressed).
Netbeans "autogenerated code" neatly adds something like this
Seems like I have answered the question myself, I wasn't aware before what the book was "talking about".Code:jButton1.addActionListener(new java.awt.event.ActionListener()
Yep, ActionListener is likely the most common "Listener" you'll use in Swing code. As your book suggests, it's an interface and you must create a class that implements it and then instantiate an object of that class before it can be used. NetBeans does this for you by creating what's called an anonymous inner class. So rather than creating a named class such as
and then creating and using an object of this class, it creates an unnamed class and an object of this class at the same time with:Code:Button1ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//... code goes here
}
}
Code:jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button1ActionPerformed(e);
}
});