Results 1 to 2 of 2
  1. #1
    thechazm is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Move object during Drag and Drop

    Hello all this is my first time posting here and I appreciate any help offered/given to answering my questions.

    I am just trying to understand during a drag and drop how can I transfer the actual object instead of just creating a new one. I believe I am close but I cannot nail down where I am going wrong. Here is my code I am using to test this.

    First/Main:

    Java Code:
    package main;
    
    import javax.swing.SwingUtilities;
    
    public class Main {
    
    	public static void main(String[] args) {
    		
    		SwingUtilities.invokeLater(new Runnable(){
    
    			@Override
    			public void run() {
    				new MainPanel();
    				
    			}
    			
    		});
    	}
    
    }
    Second/Actual JFrame:

    Java Code:
    package main;
    
    import java.awt.FlowLayout;
    
    import javax.swing.JFrame;
    
    public class MainPanel extends JFrame{
    
    	private static final long serialVersionUID = -892554275836568837L;
    
    	public MainPanel(){
    		super("DnD Panels Test");
    		ScreenFunctions sf = new ScreenFunctions();
    		this.setSize(sf.getScreenRes());
    		this.setVisible(true);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLayout(new FlowLayout());
    		for (int i = 1; i < 3; i++ ){
    			this.add(new GangsPanel());
    		}
    	}
    }
    Third/JPanels that act as a drop object:

    Java Code:
    package main;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.Transferable;
    import java.awt.datatransfer.UnsupportedFlavorException;
    import java.awt.dnd.DropTarget;
    import java.awt.dnd.DropTargetDragEvent;
    import java.awt.dnd.DropTargetDropEvent;
    import java.awt.dnd.DropTargetEvent;
    import java.awt.dnd.DropTargetListener;
    import java.io.IOException;
    
    import javax.swing.JPanel;
    
    public class GangsPanel extends JPanel implements DropTargetListener{
    
    	private static final long serialVersionUID = -5962552107674142270L;
    	Dimension prefSize = new Dimension(300,300);
    	private DropTarget target;
    	
    	public GangsPanel(){
    		this.setBackground(Color.black);
    		this.setLayout(new FlowLayout());
    		
    		for (int i = 1; i < 3; i++){
    			Person p = new Person();
    			this.add(new Person());
    			p.setN(i);
    			System.out.println(p.getN());
    		}
    		target = new DropTarget(this,this);
    		
    	}
    	
    	public Dimension getPreferredSize() {
    		return prefSize;
    	}
    
    	@Override
    	public void dragEnter(DropTargetDragEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void dragExit(DropTargetEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void dragOver(DropTargetDragEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void drop(DropTargetDropEvent event) {
    		// TODO Auto-generated method stub
    		Transferable t = event.getTransferable();
    		DataFlavor[] d = t.getTransferDataFlavors();
    		try {
    			Person tempButton = (Person) t.getTransferData(d[0]);
    			System.out.println(tempButton.getN());
    			this.add(tempButton);
    			event.dropComplete(true);
    			validate();
    				/*if(getTransferHandler().canImport(this, d)){
    					
    					((DropTransferHandler)getTransferHandler()).importData(this, (Person)t.getTransferData(d[0]));
    					
    					repaint();
    				}*/
    
    		} catch (UnsupportedFlavorException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    
    	}
    
    	@Override
    	public void dropActionChanged(DropTargetDragEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    Fourth/The object I am trying to drag and drop:

    Java Code:
    package main;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.Transferable;
    import java.awt.dnd.DnDConstants;
    import java.awt.dnd.DragGestureEvent;
    import java.awt.dnd.DragGestureListener;
    import java.awt.dnd.DragSource;
    import java.awt.dnd.DragSourceDragEvent;
    import java.awt.dnd.DragSourceDropEvent;
    import java.awt.dnd.DragSourceEvent;
    import java.awt.dnd.DragSourceListener;
    import java.awt.dnd.InvalidDnDOperationException;
    import java.io.IOException;
    
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    import javax.swing.TransferHandler;
    
    public class Person extends JPanel implements Transferable, DragSourceListener, DragGestureListener{
    
    	private static final long serialVersionUID = 8404269502745974739L;
    	
    	Dimension prefSize = new Dimension(60,40);
    	private DragSource source;
    	private TransferHandler t;
    	private int n;
    	
    	public int getN() {
    		return n;
    	}
    
    	public void setN(int n) {
    		this.n = n;
    	}
    
    	public Person(){
    		this.setBackground(Color.white);
    		t = new TransferHandler(){
    
    			private static final long serialVersionUID = -1727180294103674625L;
    
    			public Transferable createTransferable(JComponent c){
    				return new Person();
    			}
    
    		};
    
    		this.setTransferHandler(t);
    		
    		source = new DragSource();
    		source.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_MOVE, this);
    
    	}
    	
    	public Dimension getPreferredSize() {
    		return prefSize;
    	}
    	
    	public DataFlavor[] getTransferDataFlavors() {
    
    		return new DataFlavor[]{new DataFlavor(Person.class, "JPanel")};
    		//return new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType; Person.class);
    
    	}
    
    	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) {
    		try {
    			source.startDrag(dge, null, ImageFunctions.componentToImage(this, null), new Point(0,0), new Person(), this);
    		} catch (InvalidDnDOperationException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	
    	}
    
    	@Override
    	public void dropActionChanged(DragSourceDragEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	
    	
    }

    When I drag the JPanels it just copies and makes a new one. I tested this by having a sysout send to the console a private int that I set during the intial creation of the object. Any help and guidance would be greatly appreciated.

    Have a good one,

  2. #2
    thechazm is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Re: Move object during Drag and Drop

    I am also including a copy of the entire project as it is very small since it is an example. Any help is greatly appreciated!

    DndJPanels.zip

Similar Threads

  1. how to move my card images after drop this drag image.
    By ganesh.gothi@gmail.com in forum Java Applets
    Replies: 4
    Last Post: 04-30-2012, 04:27 PM
  2. Drag and Drop object
    By Anel Murselji in forum Advanced Java
    Replies: 8
    Last Post: 06-29-2011, 03:16 PM
  3. drag and drop
    By Dean29126 in forum AWT / Swing
    Replies: 0
    Last Post: 01-18-2011, 08:24 AM
  4. drag and drop - disable intra component drop
    By tomba in forum AWT / Swing
    Replies: 4
    Last Post: 12-09-2009, 02:01 PM
  5. Drag and Drop
    By carderne in forum New To Java
    Replies: 0
    Last Post: 08-31-2009, 10:18 AM

Tags for this Thread

Posting Permissions

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