Since the TransferHandler
property transfers only the 'text' property of JLabel
. How would I go about it to do an image file? Would I have to make a custom TransferHandler
made?
Asked
Active
Viewed 422 times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433

Benn
- 1
-
Something like [this](http://stackoverflow.com/questions/11460704/dragging-a-jlabel-with-a-transferhandler-drag-and-drop) or [this](http://stackoverflow.com/questions/11201734/java-how-to-drag-and-drop-jpanel-with-its-components/11443501#11443501) – MadProgrammer Sep 12 '15 at 21:42
1 Answers
2
How would I go about it to do an image file?
Not sure if you are just trying to transfer the Icon or both the Icon and Text at the same time.
If you just want the Icon, you can use the default TransferHandler:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DragIcon extends JPanel
{
public DragIcon()
{
TransferHandler iconHandler = new TransferHandler( "icon" );
MouseListener dragListener = new DragMouseAdapter();
JLabel label1 = new JLabel("Label1");
label1.setTransferHandler( iconHandler );
label1.addMouseListener(dragListener);
label1.setIcon( new ImageIcon("copy16.gif") );
JLabel label2 = new JLabel("Label2");
label2.setTransferHandler( iconHandler );
label2.addMouseListener(dragListener);
add( label1 );
add( label2 );
}
private class DragMouseAdapter extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
JComponent c = (JComponent)e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
// handler.exportAsDrag(c, e, TransferHandler.MOVE);
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Drag Icon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DragIcon());
frame.setLocationByPlatform( true );
frame.setSize(200, 100);
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

camickr
- 321,443
- 19
- 166
- 288