Results 1 to 3 of 3
  1. #1
    fernando is offline Member
    Join Date
    Jul 2007
    Rep Power

    Default Help with drag from panel

    Hi, I like this code for "DragPictureDemo", Drag and Drop and Data Transfer: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Drag and Drop and Data Transfer)

    Web Demo: DragPictureDemo

    My question is:
    I have two panels, 1 on top, 1 at the bottom,
    I need to drag and drop the images from the top to the bottom panel, do not allow to drag images from bottom to top, But the main image on the top panel is not removed (cannot cut), so all actions are "COPY" to bottom panel, no need to hold the CTL button.
    I cannot get it to work.

  2. #2
    baltimore is offline Member
    Join Date
    Jul 2007
    Rep Power


    Well actually, I found a solution to this already..

    I make a Jlabel and add the mouse motion and drop listeners, then it the drop listener is fired and is found to be in a target (in this case another panel), I create a jlabel at runtime and place it in the other panel..

    The only thing left is the ability for it to 'snap' into the the desired grid on the panel.. but I think i can manage that..

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    Changes in PictureTransferHandler:
    Java Code:
        public int getSourceActions(JComponent c) {
            return COPY;
    Changes in DTPicture class:
    Java Code:
        public void mouseDragged(MouseEvent e) {
            //Don't bother to drag if the component displays no image.
            if (image == null) return;
            if (firstMouseEvent != null) {
                int action = TransferHandler.COPY;
                int dx = Math.abs(e.getX() - firstMouseEvent.getX());
                int dy = Math.abs(e.getY() - firstMouseEvent.getY());
                //Arbitrarily define a 5-pixel shift as the
                //official beginning of a drag.
                if (dx > 5 || dy > 5) {
                    //This is a drag, not a click.
                    JComponent c = (JComponent)e.getSource();
                    TransferHandler handler = c.getTransferHandler();
                    //Tell the transfer handler to initiate the drag.
                    handler.exportAsDrag(c, firstMouseEvent, action);
                    firstMouseEvent = null;

Similar Threads

  1. Java Control Panel not saving changes when I press OK
    By RebelScum in forum New To Java
    Replies: 0
    Last Post: 03-21-2008, 05:22 PM
  2. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 03:10 PM
  3. Why the panel text changed?
    By ottawalyli in forum AWT / Swing
    Replies: 1
    Last Post: 12-17-2007, 06:56 AM
  4. Why the panel text changed?
    By ottawalyli in forum SWT / JFace
    Replies: 0
    Last Post: 12-16-2007, 05:16 PM
  5. How to place panel into frame
    By vivek_9912 in forum AWT / Swing
    Replies: 2
    Last Post: 11-20-2007, 12:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts