This string still contains qualifiers, so to strip them off
the qualifier Pattern object is used just as it was in
c09:ShowMethods.java. Feedback
At the end of init( ), an initial value is placed in name and the action
event is run, to provide a test with initial data.
This program is a convenient way to investigate the capabilities of a Swing
component. Once you know which events a particular component
supports, you don’t need to look anything up to react to that event. You
simply:
1.
Take the name of the event class and remove the word “Event.”
Add the word “Listener” to what remains. This is the listener
interface you must implement in your inner class. Feedback
2.
Implement the interface above and write out the methods for the
events you want to capture. For example, you might be looking for
mouse movements, so you write code for the mouseMoved( )
method of the MouseMotionListener interface. (You must
implement the other methods, of course, but there’s often a
shortcut for that which you’ll see soon.) Feedback
3.
Create an object of the listener class in Step 2. Register it with your
component with the method produced by prefixing “add” to your
814
Thinking in Java
www.BruceEckel.com
listener name. For example, addMouseMotionListener( ).
Feedback
Here are some of the listener interfaces:
Listener interface
Methods in interface
w/ adapter
ActionListener actionPerformed(ActionEvent)
AdjustmentListener adjustmentValueChanged(
AdjustmentEvent)
ComponentListener
componentHidden(ComponentEvent)
ComponentAdapter
componentShown(ComponentEvent)
componentMoved(ComponentEvent)
componentResized(ComponentEvent)
ContainerListener
componentAdded(ContainerEvent)
ContainerAdapter
componentRemoved(ContainerEvent)
FocusListener
focusGained(FocusEvent)
FocusAdapter
focusLost(FocusEvent)
KeyListener
keyPressed(KeyEvent)
KeyAdapter
keyReleased(KeyEvent)
keyTyped(KeyEvent)
MouseListener
mouseClicked(MouseEvent)
MouseAdapter
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
MouseMotionListener
mouseDragged(MouseEvent)
MouseMotionAdapter
mouseMoved(MouseEvent)
WindowListener
windowOpened(WindowEvent)
WindowAdapter
windowClosing(WindowEvent)
windowClosed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
ItemListener itemStateChanged(ItemEvent)
Chapter 14: Creating Windows & Applets
815
This is not an exhaustive listing, partly because the event model allows
you to create your own event types and associated listeners. Thus, you’ll
regularly come across libraries that have invented their own events, and
the knowledge gained in this chapter will allow you to figure out how to
use these events. Feedback
Using listener adapters for simplicity
In the table above, you can see that some listener interfaces have only one
method. These are trivial to implement since you’ll implement them only
when you want to write that particular method. However, the listener
interfaces that have multiple methods can be less pleasant to use. For
example, if you want to capture a mouse click (that isn’t already captured
for you, for example by a button), then you need to write a method for
mouseClicked( ). But since MouseListener is an interface, you must
implement all of the other methods even if they don’t do anything. This
can be annoying. Feedback
To solve the problem, some (but not all) of the listener interfaces that
have more than one method are provided with adapters, the names of
which you can see in the table above. Each adapter provides default empty
methods for each of the interface methods. Then all you need to do is
inherit from the adapter and override only the methods you need to
change. For example, the typical MouseListener you’ll use looks like
this:
class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
// Respond to mouse click...
}
}
The whole point of the adapters is to make the creation of listener classes
easy. Feedback
There is a downside to adapters, however, in the form of a pitfall. Suppose
you write a MouseAdapter like the one above:
class MyMouseListener extends MouseAdapter {
public void MouseClicked(MouseEvent e) {
// Respond to mouse click...
}
816
Thinking in Java
www.BruceEckel.com
}
This doesn’t work, but it will drive you crazy trying to figure out why,
since everything will compile and run fine—except that your method
won’t be called for a mouse click. Can you see the problem? It’s in the
name of the method: MouseClicked( ) instead of mouseClicked ( ). A
simple slip in capitalization results in the addition of a completely new
method. However, this is not the method that’s called when the window is
closing, so you don’t get the desired results. Despite the inconvenience, an
interface will guarantee that the methods are properly implemented.
Feedback
Tracking multiple events
|