Custom Drag and Drop Images in Java 6

Hi, I have a set of applications over at www.xfactory-librarians.co.uk which make use of some code I found a while back over at Java world for drawing a ghosted image of objects being dragged in a drag operation in addition to the drag cursor. This code has worked well under all Java versions since 1.3, but under 1.6 the image is now drawn under the drop selection being made in a JTree and JTable, whereas in earlier versions it is drawn on top.

Does anybody know what I need to do get the ghost image drawn over the drop selection in 1.6 I've tried looking, but cannot see anything obvious. The solution also needs to remain compatible with Java 1.5. I've found a way to detect 1.5 or 1.6, so I don't mind having some if/else statements for the different Java versions.

The code for my custom drawing is below (_raGhost is where to draw the image, and _imgGhost is the custom ghosted image to draw.

privatestatic Rectangle2D _raGhost =new Rectangle2D.Float();

// The 'drag image'

privatestatic BufferedImage _imgGhost;

/**

* Paints the graphic object in a Container

*

* @param parentThe parent tree or table component

* @param location The location to draw at

* @param dropRect The bounding rectangle for the drop location

* @param cueLinePositionThe position of the cue line if it's to be drawn

* @param validDrop true if the drop is valid at the current location

* @param paintCueLinetrue if a cue line (instead of a box) should be pained

*

*/

privatestaticvoid paintDragImage(JComponent parent, Point location, Rectangle dropRect,

int cueLinePosition,boolean validDrop,boolean paintCueLine){

Graphics2D g2 = (Graphics2D) parent.getGraphics();

// If a drag image is not supported by the platform, then draw our own drag image

if (DragSource.isDragImageSupported() ==false ){

// Rub out the last ghost image and cue line

parent.paintImmediately(_raGhost.getBounds());

if ( validDrop ==true ){

// Using the dropRect Y position causes the drag image to "snap" over the

// drop location rows.

_raGhost.setRect(location.x - _ptOffset.x, dropRect.getY(),

_imgGhost.getWidth(), _imgGhost.getHeight());

}else{

// Remember where we are about to draw the new ghost image

// If there is no valid drop location then the image does not "snap"

_raGhost.setRect(location.x - _ptOffset.x, location.y - _ptOffset.y,

_imgGhost.getWidth(), _imgGhost.getHeight());

}

// And draw the image

g2.drawImage(_imgGhost,

AffineTransform.getTranslateInstance(_raGhost.getX(), _raGhost.getY()),null);

}

[3784 byte] By [dacook3a] at [2007-11-27 4:13:39]
# 1
Bump! :-) Does anybody have any thoughts?
dacook3a at 2007-7-12 9:19:55 > top of Java-index,Desktop,Core GUI APIs...