 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
neilthechicken Guest
|
Posted: Mon Aug 30, 2004 11:49 am Post subject: Newbie question on "Implements" |
|
|
Forgive my ignorance but can someone tell me the signifigance of not
having "implements" at the start of a class deffinition if you are
using action Listeners within the class ?
Thanks
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Mon Aug 30, 2004 12:13 pm Post subject: Re: Newbie question on "Implements" |
|
|
On 30 Aug 2004 04:49:59 -0700, neilthechicken wrote:
| Quote: | Forgive my ignorance but can someone tell me the signifigance of not
having "implements" at the start of a class deffinition if you are
using action Listeners within the class ?
|
Pretty significant if you go to compile it.
[ Try it. ]
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
|
|
| Back to top |
|
 |
Matt Humphrey Guest
|
Posted: Mon Aug 30, 2004 12:49 pm Post subject: Re: Newbie question on "Implements" |
|
|
"neilthechicken" <neilthechicken (AT) yahoo (DOT) co.uk> wrote
| Quote: | Forgive my ignorance but can someone tell me the signifigance of not
having "implements" at the start of a class deffinition if you are
using action Listeners within the class ?
|
The "implements" tag indicates that the objects of the class can also be
considered objects of the types listed by the implements. Without the tag,
the class is not considered to be of that type. This is how java creates
multiple inheritance. Frequently in GUI code, a component (frame, panel,
etc) will also be an ActionListener for the buttons it contains (see, it's
like it's two kinds of object in one.) If you don't put ActionListener as
implements, any GUI code that says "addActionListener (this)" will fail
because this (the object of the current class) will not be an
ActionListener.
It is possible to use ActionListeners within a class without declaring the
entire class to be an Action Listener and the most common way of doing this
is to provide a unique action listener for each button via an anonymous
inner class that translates the action into the appropriate method of the
class. This technique is often preferred because it removes the need for
the actionPerformed method to figure out which object goes with the action.
It is used as follows:
myButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {
doMyButtonCode ();
}
};
What's happening here is that the ActionListener interface (which has no
code of its own) is being used to instantiate a new class that does nothing
but implement that interface, which is does by providing the actionPerformed
method. When the button is pressed, this action Performed code directly
invokes the doMyButtonCode, which is presumably a method on the original
component. This nesting is possible because inner classes are run in the
context of the object they are nested within.
Cheers,
Matt Humphrey [email]matth (AT) ivizNOSPAM (DOT) com[/email] http://www.iviz.com/
|
|
| Back to top |
|
 |
neilthechicken Guest
|
Posted: Wed Sep 01, 2004 2:45 pm Post subject: Re: Newbie question on "Implements" |
|
|
"Matt Humphrey" <matth (AT) ivizNOSPAM (DOT) com> wrote
| Quote: | "neilthechicken" <neilthechicken (AT) yahoo (DOT) co.uk> wrote in message
news:5513a3a8.0408300349.679295da (AT) posting (DOT) google.com...
Forgive my ignorance but can someone tell me the signifigance of not
having "implements" at the start of a class deffinition if you are
using action Listeners within the class ?
The "implements" tag indicates that the objects of the class can also be
considered objects of the types listed by the implements. Without the tag,
the class is not considered to be of that type. This is how java creates
multiple inheritance. Frequently in GUI code, a component (frame, panel,
etc) will also be an ActionListener for the buttons it contains (see, it's
like it's two kinds of object in one.) If you don't put ActionListener as
implements, any GUI code that says "addActionListener (this)" will fail
because this (the object of the current class) will not be an
ActionListener.
It is possible to use ActionListeners within a class without declaring the
entire class to be an Action Listener and the most common way of doing this
is to provide a unique action listener for each button via an anonymous
inner class that translates the action into the appropriate method of the
class. This technique is often preferred because it removes the need for
the actionPerformed method to figure out which object goes with the action.
It is used as follows:
myButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {
doMyButtonCode ();
}
};
What's happening here is that the ActionListener interface (which has no
code of its own) is being used to instantiate a new class that does nothing
but implement that interface, which is does by providing the actionPerformed
method. When the button is pressed, this action Performed code directly
invokes the doMyButtonCode, which is presumably a method on the original
component. This nesting is possible because inner classes are run in the
context of the object they are nested within.
Cheers,
Matt Humphrey [email]matth (AT) ivizNOSPAM (DOT) com[/email] http://www.iviz.com/
|
Thanks Matt you answered the question I was trying to ask
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|