Page 1 of 1

Java Drag and Drop Tutorial: Part 1- Basics of Dragging Rate Topic: -----

#1 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Posted 14 January 2011 - 12:20 PM

*
POPULAR

Introduction to the DnD API
The Java Drag and Drop API allows us to write code where the user can drag JComponents and drop them (or their attributes) onto another JComponent. The basis of the Drag and Drop API is the TransferHandler class.

There are some existing JComponents that support Drag and Drop with little extra work. You simply need to invoke setDragEnabled(true) for the following JComponents to allow for dragging them:
  • JColorChooser
  • JEditorPane
  • JFileChooser
  • JFormattedTextField
  • JList
  • JTable
  • JTextArea
  • JTextField
  • JTextPane
  • JTree


The components that support dropping already include:
  • JEditorPane
  • JFormattedTextField
  • JPasswordField
  • JTextArea
  • JTextField
  • JTextPane
  • JColorChooser


Basics of Dragging
For those JComponents that do not support Drag-and-Drop by default, or JComponents you wish to customize their Drag-and-Drop behaviors, you need to pass them a new TransferHandler. The easiest way to create a TransferHandler is to instantiate one and pass it a beans property, or the name of a get/set method in the class (ie., text and icon are beans properties for JButton). We also need a MouseListener to export the drag.
JButton button = new JButton("This is the text");
TransferHandler transfer = new TransferHandler("text");

//the JButton can now be dragged and the text will be dropped
button.setTransferHandler(transfer);

//add a MouseListener to initiate the Drag on the appropriate
//MouseEvent
button.addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                JButton button = (JButton)e.getSource();
                TransferHandler handle = button.getTransferHandler();
                handle.exportAsDrag(button, e, TransferHandler.COPY);
            }
        });




Custom Dragging
In this section, we will explore custom dragging by creating a custom JButton we can actually drop, not just its text or icon, but the actual JButton. In addition to JButton and TransferHandler, we will be looking at the DragSource class, the Transferable interface, the DragSourceListener interface, and the DragGestureListener interface.

The way the Drag process works is that the JButton will listen for the DragGestureEvent. It will then use the DragSource to initiate the Drag. The TransferHandler will then export a Transferable, which wraps around the data you want to be transferred. In this case the DnDButton is a Transferable, exporting a new DnDButton with the same text on a drag.
/**
 * Our custom JButton class that is Draggable.
 * This JButton is Transferable (so it can be Dragged),
 * And listens for its own drags
 * */
public class DnDButton extends JButton implements Transferable,
DragSourceListener, DragGestureListener{

    //marks this JButton as the source of the Drag
    private DragSource source;

    private TransferHandler t;

    public DnDButton(){
        this("");
    }

    public DnDButton(String message){
        super(message);

        //The TransferHandler returns a new DnDButton
        //to be transferred in the Drag
        t = new TransferHandler(){

              public Transferable createTransferable(JComponent c){
                    return new DnDButton(getText());
              }
        };
        setTransferHandler(t);

        //The Drag will copy the DnDButton rather than moving it
        source = new DragSource();
        source.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
    }

     //The DataFlavor is a marker to let the DropTarget know how to
     //handle the Transferable
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[]{new DataFlavor(DnDButton.class, "JButton")};
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return true;
    }

    public Object getTransferData(DataFlavor flavor) {
        return this;
    }


    public void dragEnter(DragSourceDragEvent dsde) {}
    public void dragOver(DragSourceDragEvent dsde) {}
    public void dropActionchanged(DragSourceDragEvent dsde) {}
    public void dragExit(DragSourceEvent dse) {}

    //when the drag finishes, then repaint the DnDButton
    //so it doesn't look like it has still been pressed down
    public void dragDropEnd(DragSourceDropEvent dsde) {
        repaint();
    }

    //when a DragGesture is recognized, initiate the Drag
    public void dragGestureRecognized(DragGestureEvent dge) {
         source.startDrag(dge, DragSource.DefaultMoveDrop, new DnDButton("Text"), this);        
    }

}//end outer class



Is This A Good Question/Topic? 11
  • +

Replies To: Java Drag and Drop Tutorial: Part 1- Basics of Dragging

#2 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Posted 24 January 2011 - 08:39 AM

Cool, it's a bit over my head as I've never worked with it, but I like the concept. I take it the program at CC used this?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Posted 24 January 2011 - 08:44 AM

We'll do more with Swing in Computer Club. But my program for open house used the Drag-and-Drop API. :)
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,462
  • Joined: 24-September 08

Posted 24 January 2011 - 02:38 PM

How's about some screen shots?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Posted 25 January 2011 - 08:18 AM

Compile and run my DnDButton class from this tutorial, and my DnDTable class from my second tutorial. Running them in a test JFrame will give you a much better idea of how it works and looks than screen shots will. Besides, my computer doesn't take screen shots of my cursor, so it just looks like I'm clicking a JButton. :)
Was This Post Helpful? 0
  • +
  • -

#6 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Posted 23 May 2011 - 04:22 AM

Nice tutorial! Thanks :)
Was This Post Helpful? 0
  • +
  • -

#7 noorai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-July 12

Posted 05 August 2012 - 02:36 PM

please i want to know how implements these code in your tutorials about drag and drop part1 and part2 i need it so much to see how drag & drop a button(to imagine it). i try to run these code but there are more problems.
Please reply to my question as soon as possible.
thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1