Results 1 to 11 of 11
- 12-16-2009, 01:09 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 6
- Rep Power
- 0
-
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.
- 12-16-2009, 02:25 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 6
- Rep Power
- 0
I've studied some of them, do u know which ones will be useful for me?
- 12-19-2009, 08:53 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 6
- Rep Power
- 0
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
- 12-19-2009, 10:50 PM #5
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
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);
- 12-19-2009, 10:56 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 6
- Rep Power
- 0
- 12-19-2009, 11:05 PM #7
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
Doh. My bad. I failed to read carefully.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
Copying is harder because two copies must be made of the underlying object.
Ideally, it would be something like
but this will probably fail; neither JComponent nor Component is Cloneable.Java Code:container.add(component.clone())
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.
- 12-19-2009, 11:13 PM #8
Member
- Join Date
- Dec 2009
- Posts
- 6
- Rep Power
- 0
yeah it didnt work..
so, I should change the whole thing?
is there another way to do it?
- 12-20-2009, 04:01 AM #9
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
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.
- 12-20-2009, 06:45 AM #10
Member
- Join Date
- Dec 2009
- Posts
- 6
- Rep Power
- 0
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
- 12-20-2009, 07:59 PM #11
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
I would suggest making them before you insert them into the target container.make the duplicates where?
Doing it after is ill-defined :rolleyes:
So if (component instanceof JLabel), cast the component to a JLabelthe draggable components are JLabel with icons
and use its data to construct a new JLabel.
Similar Threads
-
drag and drop - disable intra component drop
By tomba in forum AWT / SwingReplies: 4Last Post: 12-09-2009, 01:01 PM -
Drag and Drop
By carderne in forum New To JavaReplies: 0Last Post: 08-31-2009, 09:18 AM -
Drag and Drop Issue
By kirly in forum Advanced JavaReplies: 1Last Post: 04-16-2009, 05:04 AM -
Drag and drop
By thayalan in forum AWT / SwingReplies: 1Last Post: 02-16-2009, 03:04 PM -
Drag and drop
By abhivenugopal in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-30-2008, 02:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks