I\'m trying to set up code so that the user can drag from a JTable to a JList, and customize the drag image by using the Java 7 function setDragImage in TransferHandler. I g
In order to have a TransferHandler display a custom image (when a drag occurs), you must create a subclass of TransferHandler with the following functions overridden by it:
public int getSourceActions(JComponent) - it must return the actions supported by the source (the JComponent from which the drag initiated), if the action requested by the drag when initiated (see 5) doesnt match, it will not work.public Transferable createTransferable(JComponent c) - the TransferHandler must return a Transferable object, if null is returned, the drag will not be initiated.public Image getDragImage() needs to reutn the wanted image, or setDragImage() should be called on an instance of the custom TransferHandlerTo start the drag, override/extend a MouseMotionListener with:
public void mouseDragged(MouseEvent e){
JComponent jc = (JComponent)e.getSource();
jc.getTransferHandler().exportAsDrag(jc, e,TransferHandler.COPY);
}
And also set the JComponent TransferHandler and MouseMotionListener with:
myJComponent.setTransferHandler(new myCustomTransferHandler());
myJComponent.addMouseMotionListener(new myCustomMouseMotionListener());
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.imageio.ImageIO;
public class DragTest{
Image dragImage;
public static void main(String[] args){
new DragTest();
}
public DragTest(){
try{
dragImage = javax.imageio.ImageIO.read( new java.net.URL(
"https://www.icon2s.com/wp-content/uploads/2013/07/black-white-metro-file-icon.png"));
//dragImage = ImageIO.read(new java.io.File("icon.png")); //can be used to read image from file
}catch(java.io.IOException e1){
e1.printStackTrace();
return;
}
dragImage = dragImage.getScaledInstance(64, 64, Image.SCALE_DEFAULT); //scaling the image
JFrame frame = new JFrame();
frame.setLayout(null);
frame.setSize(500,500); frame.setLocationRelativeTo(null); //size and location
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyJComponent myJC = new MyJComponent();
myJC.setBounds(50,50,100,100);
myJC.addMouseMotionListener(new MouseMotionListener(){
@Override
public void mouseDragged(MouseEvent e){
JComponent src = (JComponent)e.getSource();
src.getTransferHandler().exportAsDrag(src, e, TransferHandler.COPY); //try to export with the copy action - TransferHandler should support copy
}
@Override
public void mouseMoved(MouseEvent e){}
});
myJC.setTransferHandler(new TransferHandler(){
@Override public Image getDragImage(){ return(dragImage);}
@Override public int getSourceActions(JComponent c){
return(COPY_OR_MOVE); //supporting both copy and move
}
@Override public Transferable createTransferable(JComponent c){
return(new Transferable(){
@Override
public boolean isDataFlavorSupported(DataFlavor flavor){
return false;
}
@Override
public DataFlavor[] getTransferDataFlavors(){
return null;
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, java.io.IOException {
return null;
}
});
}
});
frame.add(myJC);
frame.setVisible(true);
}
private class MyJComponent extends JComponent{
@Override
public void paintComponent(Graphics g){
g.setColor(Color.RED);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}