codeblocks
Class JComponentDragHandler

java.lang.Object
  extended by codeblocks.JComponentDragHandler
All Implemented Interfaces:
java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.util.EventListener

public class JComponentDragHandler
extends java.lang.Object
implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener

This class can be used to add dragging capability to any JComponents. It contains the methods and data members needed to support automatic dragging, and contains methods to impliment both MouseListener, MouseMotionListener. In general, any existing JComponent can be made to be draggable simple by creating an instance of JComponentDragHandler (passing a reference to itself) and registering the JComponentDragHandler as the listener for all mouse events. Classes that need similar, but not identical, behavior, or that need to add functionality to the mouse methods here can create an inner class that extends this class. In this way the inner class can maintain the functionality of JComponentDragHandler while also having access to data members and methods of its enclosing class for the purposes of extension.


Field Summary
 int dragDX
           
 int dragDY
           
 int mCurrentX
           
 int mCurrentY
           
 int mPressedX
          These data members save the point at which the mouse was pressed relative to the (0,0) corner of the JComponent.
 int mPressedY
           
 java.awt.Point myLoc
          Stores location data (typically of this JComponent) as a Point for easy manipulation and to avoid re-creating a new object every time these manipulations are done.
 int oldLocX
           
 int oldLocY
           
 
Constructor Summary
JComponentDragHandler(javax.swing.JComponent jc)
          Creates a new instance of a JComponentDragHandler with a pointer to the given JComponent.
 
Method Summary
 java.awt.Cursor getDraggingCursor()
          Returns the Cursor instance that is used on mouse drags
 java.awt.Cursor getDragHintCursor()
          Returns the Cursor instance that is used when a mouse is over a draggable object
 java.awt.Point getMousePoint()
           
 void mouseClicked(java.awt.event.MouseEvent arg0)
           
 void mouseDragged(java.awt.event.MouseEvent e)
          This method is called when the mouse is dragged over the JComponent.
 void mouseEntered(java.awt.event.MouseEvent arg0)
           
 void mouseExited(java.awt.event.MouseEvent arg0)
           
 void mouseMoved(java.awt.event.MouseEvent e)
          update the current location of the mouse
 void mousePressed(java.awt.event.MouseEvent e)
          Called when the mouse is pressed over the JComponent.
 void mouseReleased(java.awt.event.MouseEvent e)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mPressedX

public int mPressedX
These data members save the point at which the mouse was pressed relative to the (0,0) corner of the JComponent.


mPressedY

public int mPressedY

mCurrentX

public int mCurrentX

mCurrentY

public int mCurrentY

dragDX

public int dragDX

dragDY

public int dragDY

oldLocX

public int oldLocX

oldLocY

public int oldLocY

myLoc

public java.awt.Point myLoc
Stores location data (typically of this JComponent) as a Point for easy manipulation and to avoid re-creating a new object every time these manipulations are done.

Constructor Detail

JComponentDragHandler

public JComponentDragHandler(javax.swing.JComponent jc)
Creates a new instance of a JComponentDragHandler with a pointer to the given JComponent. Remember to register this JComponentDragHandler as the listener for mouse events in the JComponent in order for this class to be allowed to handle those events.

Parameters:
jc - the JComponent whose mouse events will be handled by this JComponentDragHandler
Method Detail

getDragHintCursor

public java.awt.Cursor getDragHintCursor()
Returns the Cursor instance that is used when a mouse is over a draggable object

Returns:
the Cursor instance that is used when a mouse is over a draggable object

getDraggingCursor

public java.awt.Cursor getDraggingCursor()
Returns the Cursor instance that is used on mouse drags

Returns:
the Cursor instance that is used on mouse drags

getMousePoint

public java.awt.Point getMousePoint()
Returns:
the Point where the mouse is, in the JComponent's coordinate frame

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Called when the mouse is pressed over the JComponent. Saves the point (which is measured relative to the JComponent's corner) over which the mouse was pressed.

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
This method is called when the mouse is dragged over the JComponent. Moves the JComponent by the amount of the drag such that the point under which the mouse the pressed remains under the mouse cursor. In other words, "drags" the JComponent.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
update the current location of the mouse

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent arg0)
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent arg0)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent arg0)
Specified by:
mouseExited in interface java.awt.event.MouseListener