How to use DragManager

Mikle edited this page Jul 2, 2014 · 4 revisions

Available since: WebLaF v1.28 release
Required Java version: Java 6 update 30 or any later
Module: ui


What is it for?

Swing has a pretty descent support for Drag and Drop operations but it still lacking a convenient way to display visual representation of the dragged data. This is where DragManager saves the day.

It has rather simple but efficient way to provide dragged data representation separately from the drag & drop code - all you need is to provide custom DragViewHandler into DragManager which defines DataFlavor represented and returns the image representation of the dragged data.

After registering your custom DragViewHandler you will see the provided representation when any data with the specified DataFlavor is dragged within your application.


How to use it?

First of all you need to decide what kind of data is going to be dragged, what DataFlavor will you specify for your custom DragViewHandler and how you want that dragged data representation to look like. Then you can create the handler itself.

Here is a simple example that displays representation for DataFlavor.stringFlavor:

public class TextDragViewHandler implements DragViewHandler<String>
{
    @Override
    public DataFlavor getObjectFlavor ()
    {
        return DataFlavor.stringFlavor;
    }

    @Override
    public BufferedImage getView ( final String object )
    {
        final FontMetrics fm = SwingUtils.getDefaultLabelFontMetrics ();
        final int w = fm.stringWidth ( object );
        final int h = fm.getHeight ();
        final int y = LafUtils.getTextCenterShearY ( fm );

        final BufferedImage image = new BufferedImage ( w, h, Transparency.TRANSLUCENT );
        final Graphics2D g2d = image.createGraphics ();
        g2d.setFont ( fm.getFont () );
        GraphicsUtils.setupAlphaComposite ( g2d, 0.8f );
        GraphicsUtils.setupSystemTextHints ( g2d );
        g2d.setColor ( Color.BLACK );
        g2d.drawString ( object, 0, h/2+y );
        g2d.dispose ();
        return image;
    }

    @Override
    public Point getViewRelativeLocation ( final String object )
    {
        return new Point ( 20, 5 );
    }
}

To see it working you will need some code that has draggable strings:

public class TextDragViewHandlerExample
{
    public static void main ( final String[] args )
    {
        WebLookAndFeel.install ();
        DragManager.registerViewHandler ( new TextDragViewHandler () );

        final WebLabel dragMe = new WebLabel ( "Drag me!", WebLabel.CENTER );
        dragMe.setPreferredSize ( new Dimension ( 800, 600 ) );
        dragMe.setTransferHandler ( new TextDragHandler ( dragMe, dragMe.getText () ) );

        TestFrame.show ( dragMe );
    }
}

And don't forget to register your custom DragViewHandler!

If you run this example and drag the text you will see something like this: Text D&D view

That's all you need to know to start using this feature. I will update DragManager with more options in the future, so stay tuned!

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.