Results 1 to 11 of 11
  1. #1
    zoz
    zoz is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default drag and drop for diagrams - help!

    hi,
    is it possible to drag and drop many components to one panel?
    I want to draw a diagram using drag and drop, lets say drag two squares and then connect them using an arrow
    any ideas? :confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes it is quite possible. If you haven't done so already, you'll want to study the Sun drawing in Swing and Graphics tutorials as well as the MouseListener tutorial.

  3. #3
    zoz
    zoz is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    I've studied some of them, do u know which ones will be useful for me?

  4. #4
    zoz
    zoz is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    hi again,,
    I managed to do the drag & drop but the problem is components are moved, I want them to be copied
    here's the code..

    Java Code:
    import javax.swing.*;
    import java.awt.dnd.DnDConstants; // drag and drop actions
    import java.awt.*;
    import java.awt.datatransfer.*; //for use with the clipboard
    import java.awt.dnd.*; //To develop the D&D metaphor
    import java.awt.dnd.DropTarget; // to associate destenation of data
    import java.awt.datatransfer.Transferable; /* encapsulates the data transferred
     between the DragSource and DropTarget objects. **/
    
    
    public class BtstwiGha9b extends JFrame implements DragGestureListener,
    DragSourceListener,
            DropTargetListener, Transferable{
    
        JButton b;
        JLabel l1;
        JPanel p,p2;
        JLabel x;
        int i = 1;
    
    
    
        static final DataFlavor[] supportedFlavors = {null};
        static {
            try {
                supportedFlavors[0] = new
                DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        Object object;
        // Transferable methods.
        public Object getTransferData(DataFlavor flavor) {
            if (flavor.isMimeTypeEqual
                (DataFlavor.javaJVMLocalObjectMimeType)) {
                return object;
            } else {
                return null;
            }
        }
    
        public DataFlavor[] getTransferDataFlavors() {
            return supportedFlavors;
        }
    
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return flavor.isMimeTypeEqual
            (DataFlavor.javaJVMLocalObjectMimeType);
        }
    
    
        // DragGestureListener method.
        public void dragGestureRecognized(DragGestureEvent ev) {
            ev.startDrag(null, this, this);
        }
        // DragSourceListener methods.
        public void dragDropEnd(DragSourceDropEvent ev) {
        }
    
        public void dragEnter(DragSourceDragEvent ev) {
        }
    
        public void dragExit(DragSourceEvent ev) {
        }
    
        public void dragOver(DragSourceDragEvent ev) {
            object = ev.getSource();
        }
    
        public void dropActionChanged(DragSourceDragEvent ev) {
        }
        // DropTargetListener methods.
        public void dragEnter(DropTargetDragEvent ev) {
        }
    
        public void dragExit(DropTargetEvent ev) {
        }
    
        public void dragOver(DropTargetDragEvent ev) {
            dropTargetDrag(ev);
        }
    
        public void dropActionChanged(DropTargetDragEvent ev) {
            dropTargetDrag(ev);
        }
    
        void dropTargetDrag(DropTargetDragEvent ev) {
            ev.acceptDrag(ev.getDropAction());
        }
    
        public void drop(DropTargetDropEvent ev) {
            ev.acceptDrop(ev.getDropAction());
            try {
                Object target = ev.getSource();
                Object source = ev.getTransferable().getTransferData
                                (supportedFlavors[0]);
                Component component = ((DragSourceContext)
                                        source).getComponent();
                Container oldContainer = component.getParent();
                Container container = (Container) ((DropTarget)
                                       target).getComponent();
                container.add(component);
                oldContainer.validate();
                oldContainer.repaint();
                container.validate();
                container.repaint();
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
            ev.dropComplete(true);
        }
    
    
    
        public BtstwiGha9b()
        {
            setLayout(new GridLayout(3,2,5,5));
            l1 = new JLabel("this is zooz");
    
    
            b = new JButton("try");
            p = new JPanel();
            p.setLayout(new GridLayout(3,2,5,5));
            p.setBorder(BorderFactory.createLineBorder(Color.black));
            p2 = new JPanel();
            p2.setLayout(new GridLayout(3,2,5,5));
            p2.setBorder(BorderFactory.createLineBorder(Color.black));
    
            add(b);
            add(p);
            add(p2);
            p.add(l1);
            x = new JLabel("hello");
            p2.add(x);
    
    
            DragSource dragSource = new DragSource();
            DropTarget dropTarget1 = new DropTarget(p,
            DnDConstants.ACTION_COPY_OR_MOVE, this);
            DropTarget dropTarget2 = new DropTarget(p2,
            DnDConstants.ACTION_COPY_OR_MOVE,
                    this);
            DragGestureRecognizer dragRecognizer1 = dragSource.
                    createDefaultDragGestureRecognizer(x,
                    DnDConstants.ACTION_COPY_OR_MOVE, this);
            DragGestureRecognizer dragRecognizer2 = dragSource.
                    createDefaultDragGestureRecognizer(l1,
                    DnDConstants.ACTION_COPY_OR_MOVE, this);
    
       
        }
    
    
    
    
       public static void main(String [] args)
       {
           BtstwiGha9b t = new BtstwiGha9b();
           t.setVisible(true);
           t.setSize(500, 500);
           t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
       }
    
    
    } // class ends here

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Yes, they are indeed copied and not moved. It says so in the code:
    Java Code:
                Object target = ev.getSource();
                Object source = ev.getTransferable().getTransferData
                                (supportedFlavors[0]);
                Component component = ((DragSourceContext)
                                        source).getComponent();
                Container oldContainer = component.getParent();
                Container container = (Container) ((DropTarget)
                                       target).getComponent();
                container.add(component);
                oldContainer.validate();
                oldContainer.repaint();
                container.validate();
                container.repaint();

    To move instead of copied, the component must be removed from the old container
    as well as being added to the new.
    It may be as simple as adding a line before oldContainer.validate(...):
    Java Code:
    oldContainer.remove(component);

  6. #6
    zoz
    zoz is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by zweibieren View Post
    Yes, they are indeed copied and not moved. It says so in the code:
    Java Code:
                Object target = ev.getSource();
                Object source = ev.getTransferable().getTransferData
                                (supportedFlavors[0]);
                Component component = ((DragSourceContext)
                                        source).getComponent();
                Container oldContainer = component.getParent();
                Container container = (Container) ((DropTarget)
                                       target).getComponent();
                container.add(component);
                oldContainer.validate();
                oldContainer.repaint();
                container.validate();
                container.repaint();

    To move instead of copied, the component must be removed from the old container
    as well as being added to the new.
    It may be as simple as adding a line before oldContainer.validate(...):
    Java Code:
    oldContainer.remove(component);
    yes it is copied, I dont want it be removed, I want to have a copy of it in its original container & then paste it in the new one

  7. #7
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    yes it is copied, I dont want it be removed, I want to have a copy of it in its original container & then paste it in the new one
    Doh. My bad. I failed to read carefully.

    Copying is harder because two copies must be made of the underlying object.
    Ideally, it would be something like
    Java Code:
    container.add(component.clone())
    but this will probably fail; neither JComponent nor Component is Cloneable.
    And anyway cloning is usally only one level deep.

    Here is where a model-view separation is of value.
    You want two components sharing the same model.
    Without details of the components, it is difficult to say more.

  8. #8
    zoz
    zoz is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    yeah it didnt work..
    so, I should change the whole thing?
    is there another way to do it?

  9. #9
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    It may be there is no need to change anything.
    You have not posted the details of the components, so we can't say for sure.
    Presumably each component is displaying data that comes from somewhere.
    Possibly the task is as easy as creating a duplicate component
    that views the same data.

  10. #10
    zoz
    zoz is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    make the duplicates where?
    the draggable components are JLabel with icons
    I want to have many duplicates in that case, because there will instances be as much as the user wants

  11. #11
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    make the duplicates where?
    I would suggest making them before you insert them into the target container.
    Doing it after is ill-defined :rolleyes:

    the draggable components are JLabel with icons
    So if (component instanceof JLabel), cast the component to a JLabel
    and use its data to construct a new JLabel.

Similar Threads

  1. drag and drop - disable intra component drop
    By tomba in forum AWT / Swing
    Replies: 4
    Last Post: 12-09-2009, 01:01 PM
  2. Drag and Drop
    By carderne in forum New To Java
    Replies: 0
    Last Post: 08-31-2009, 09:18 AM
  3. Drag and Drop Issue
    By kirly in forum Advanced Java
    Replies: 1
    Last Post: 04-16-2009, 05:04 AM
  4. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 03:04 PM
  5. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 02:10 PM

Posting Permissions

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