Translucent Drag-and-Drop #69
Chapter 9, Drag-and-Drop
The only job of the paintComponent( ) will be to draw the dragged picture.
Therefore, there is no need to call
super.paintComponent( ), as it will per-
form unnecessary operations.
Do not forget to set the composite in this method, or you
might draw the picture fully opaque.
The center of the picture is painted at the location specified in setLocation( ).
This means assuming the location given is the location of the mouse cursor.
Now, you’ve got a fully functional glass pane, and you just need to add it to
a Swing frame:
glassPane = new GhostGlassPane( );
Also, do not forget that a glass pane is not visible by default: you will have to
setVisible(true) when a drag-and-drop operation is initiated. The final
step is the activation of drag-and-drop on components requiring it. You can
use a picture as drag-and-drop feedback:
JLabel label = new JLabel("New Sale");
pictureAdapter = new GhostPictureAdapter(glassPane,
You can also use a ghost feedback:
JButton button = new JButton("Ghost Feedback"));
componentAdapter = new GhostComponentAdapter(glassPane, "button_pushed");
Whatever choice you make, you need two adapters to perform drag-and-
drop. The first one is an adapter for the
MouseListener interface. It can be
GhostPictureAdapter, to handle pictures, or a GhostComponentAdapter,
to handle a component ghost. The role of this adapter is to handle the begin-
ning and the end of a drag-and-drop gesture. When a drop action is per-
formed, an event is fired to every
GhostDropListener registered by the
adapter. The code in Example 9-8 shows how
The code of
GhostPictureAdapter is almost the same, and it’s left to you to
check out (all the code for this book is online; visit http://www.oreilly.com/