I have this:
and want the image SD to be able to be draged and dropped into another JLabel (the JLabel now shows the picture SD), how would I do it?
ImageIcon SD = new ImageIcon("resources/terrains/StoneDungeon.jpg");
JLabel dns=new JLabel( SD);
frame.getContentPane().add(dns);
dns.setBounds(800,150,50,50);
dns.setFont(new Font("Courior", Font.BOLD, 25));
dns.setForeground(Color.red);
final String propertyName = "text";
dns.setTransferHandler(new TransferHandler(propertyName));
// Listen for mouse clicks
dns.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
JComponent comp = (JComponent)evt.getSource();
TransferHandler th = comp.getTransferHandler();
// Start the drag operation
th.exportAsDrag(comp, evt, TransferHandler.COPY);
Please don’t tell me to look at the tutorial, I already have.
I dont have enough rep to answer my question so here is the answer:
That was the stupidist mistake ever!!!!!!!!
code shoul’ve been:
ImageIcon SD = new ImageIcon("resources/terrains/StoneDungeon.jpg");
JLabel dns=new JLabel( SD);
frame.getContentPane().add(dns);
dns.setBounds(800,150,50,50);
dns.setFont(new Font("Courior", Font.BOLD, 25));
dns.setForeground(Color.red);
dns.setTransferHandler(new TransferHandler(SD));
MouseListener ml = new MouseAdapter(){
public void mousePressed(MouseEvent e){
JComponent jc = (JComponent)e.getSource();
TransferHandler th = jc.getTransferHandler();
th.exportAsDrag(jc, e, TransferHandler.COPY);
}
};
dns.addMouseListener(ml);
I had put some string variables in instead of a picture to be moved!!!!
but now the problem is, you cant drag drop icons (SD)
1 Answer