How To Get Listeners to Work
The DefaultListener class is what takes care of all of the listeners. All
CanvasComponents contain a DefaultListener by default. If you want to use Mouse Listeners
or Keyboard Listeners you should subclass DefaultListener and override the methods you want to
use. If you want to use Drag and Drop on a Component, you can use its current DefaultListener. For Mouse and
Keyboard Listeners, after you have created your subclass, you have to associate a component with
your listener. All ShapeComponents and PictureComponents have a setListener method that takes
care of this. Pass in your subclass of DefaultListener as the parameter. After the listener
is associated, you have to tell the individual components which Events they should listen for.
There are three types of events that are possible.
Each of these event types has a separate listener associated with it. To
activate the Mouse Listener for a specific component, call that component's
addMouseListener( ) method. After this method is called, then any Mouse Events
on the component will call the appropriate methods on your default listener
subclass. If these methods are properly overridden, then you should be able to obtain
functionality from Mouse Events. To Allow a Component to listen for Mouse Drag
Events, you need to call addDragListener( ) on the component. For keyboard events, you
need to call addKeyboardListener( ). If you want to check out the Javadoc for DefaultListener,
There are five Mouse Events that components are capable of registering.
- Mouse Click Events
- Every time a Mouse Click occurs in the component, the method
mouseClickedAt(x,y) is called on the DefaultListener.
- the x and y parameters are the x and y coordinates of the click.
- Mouse Pressed Events
- Every time a mouse is Pressed in the component, the method
mousePressedAt(x,y)is called on the DefaultListener.
- Mouse Released Events
- Every time a Mouse Press is released, the method
mouseReleasedAt(x,y) is called on the DefaultListener.
- Mouse Moved Events
- Every time the Mouse is moved within the listening component,
mouseMovedTo(x,y) is called on the DefaultListener.
- Mouse Dragged Events
- Any time a mouse is dragged on a component, and the drag listener is not
enabled, mouseDraggedTo(x,y) is called on the DefaultListener.
Mouse Drag Events
When the Drag Listener is enabled, the components themselves take
care of all of the necessary Drag and drop functionality. Drag and drop actions
call certain methods within the components. Therefore, you do not need to
subclass DefaultListener for drag and drop. To get drag and drop to work, you
only need to call addDragListener( ) on the component. It is not necessary to call
setListener with a new DefaultListener. DropDragShapeComponents, PictureComponents, and
DrawingPanes can listen for drag and drop. There are certain methods in these components
that have to be overridden in subclasses of the of these files for drag and drop to work.
- This method is called on an object whenever another component is dropped on it.
This method will only be called on drop targets that are also listening for drag and drop.
The parameter "dropped" is a reference to the object that is being dropped. You should
override this method and make sure it returns 'true' to allow the drop.
- manufactureDroppable( )
- This method is called on the object that is being dropped. If it returns null,
which is the default, acceptDrop( ) will be called on the Drop Target with a null parameter.
If you want a Component to be able to register itself with the Drop Target, make
manufactureDroppable( ) return 'this.' You will want to override this method to return 'this'
in all components that can be dropped on another component.
- okToDrag( )
- This method is called at the moment a Drag Gesture is recognized. If you want
certain Components to listen only for drops, then override this method to return false
in those components.
When the Keyboard Listener is enabled for a specific component, that
component will be able to listen for keystrokes whenever that object has the
keyboard focus. Here are some important notes about that.
The keystrokes DefaultListeners can listen for are:
- Only one Component can listen for keystrokes
at a time -- the component that has Focus.
- It is fundamental to call
requestFocus() on your component before you expect it to listen for keystrokes.
- To have any effect, the call must happen when the component is visible on the
screen (ie, already added to a DrawingPane which is on a ViewFrame).
If this method call comes when the component is not visible on the screen, the
component cannot receive focus and cannot listen for keystrokes.
- Up Key
- Whenever the up key is pressed, and the component has focus,
upKeyPressed( ) is called on the DefaultListener.
- Down Key
- Whenever the down key is pressed, and the component has focus,
downKeyPressed( ) is called on the DefaultListener.
- Left Key
- Whenever the left key is pressed, and the component has focus,
leftKeyPressed( ) is called on the DefaultListener.
- Right Key
- Whenever the right key is pressed, and the component has focus,
rightKeyPressed( ) is called on the DefaultListener.
- Enter Key
- Whenever the enter key is pressed, and the component has focus,
enterKeyPressed( ) is called on the DefaultListener.
- Whenever the spacebar is pressed, and the component has focus,
spacebarPressed( ) is called on the DefaultListener.