How To Get Listeners to Work

The Basics

    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, click here.

Mouse Events

    There are five Mouse Events that components are capable of registering.

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.

Keyboard Events

    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: