fix said:
Yes, and I tried and it works. Actually if I am comparing by getSource()
== timerName, it worked too.
Right, and that would make sense. On the other hand, I don't see an
explicit guarantee that this is the case, so I wouldn't rely on it.
Yeah, you're right. So to implement this, should I put a class in the
containing class by just typing in "class someClass" and put all the
code and method in it?
Sure, or it's less trouble to declare an anonymous class at the point
where you need it:
firstButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Your code here
}
});
secondButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Your code here
}
});
timer = new Timer(300, new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Your code here
}
});
I am a VB programmer, we have different "method"s for each components,
each type of events, for instance, Button1_Clicked, Button2_Clicked,
Text1_KeyDown, Text1_KeyUp, Timer1_Timer. But it does not really make
sense to me to have CLASSes to handle different events.
Okay. I don't have VB set up right now to check what I'm saying, and
it's been a couple years since I've worked with it, so please forgive me
if I don't remember correctly.
As I recall, VB does have classes, and they are used exclusively to
separate different pieces of code. As such, you expect to see a
completely different part of the application in a new class. The
corrolaries of this are that there's a reluctance to create new classes,
and that many parts of the language are designed to treat methods a
first-class elements.
In Java, it's not that way. A class is as big or as small as you need
it to be, and as such, there are "big" classes and then there are
classes that are easier to use and more local. Not that Java
applications are *not* broken up into classes; certainly they are... but
all classes don't represent major independent divisions of your code.
For example, the code I posted above is all a part of some big class,
but contains *three* complete smaller classes, which are so small that
they don't even get names. I don't have to go that far; I can also
declare a class that's named but may only be used within a single
method, or a class that's is only used within a single containing class,
or a class that's only used with a package, or a class that's visible to
the whole application. I pick whichever one meets my needs.
The point, then, is that you need an object that provides a certain kind
of behavior (that is, ActionListener), and you've got one. The specific
implementation class doesn't need a name, because all you need to know
about this object is that it provides this "ActionListener" sort of
behavior... the specific implementation is of very isolated importance.
Because you can do this kind of thing, and because defining a new class
is not nearly as much of a pain as it is in Visual Basic, it's generally
frowned upon to do some of the stuff that is done in Visual Basic to
*avoid* creating new classes. For example, your decision to reuse a
single object for listening to all kinds of events and then sorting them
out later is not a good one in Java.
Now, it may turn out that the behavior implemented by some
ActionListener instance becomes of such general importance that it's
worth sharing. In that case, it's trivial to move that local anonymous
class to a full top-level class with a name, and use it in other places
as well. However, you wouldn't do this unless the code that you've
written in the class really is widely applicable; otherwise, you're just
providing more top-level API than you need to provide, and making it
harder for others to approach your code.
Oh I meant excluding the event-handling method, it is "empty".
Then yes... although since *the* purpose of that class is to implement
the response to that event, it still contains enough code to justify its
existence. There's nothing wrong with writing a class to contain one
method, especially when that class doesn't present a top-level API
element, and is only used to obtain the object behavior you need.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation