drag n drop overwriting JLabel

Hi,

can anyone shed some light on this please.....

I have successfully set up drag and drop, dragging text from JLabel into JTextArea which works fine. If you drag the text over another JLabel it overwrites the JLAbel text - how do I stop this?

I have tried using JTextArea's instead and .setEditable(false) but it doesn't make a difference.

The JLabels have .setTransferHandler(new TransferHandler("text")) is this the problem and if so how do I solve it?

I have searched through the Swing tutorial and found no answers.

All advice would be REALLY appreciated.

Cheers.

[620 byte] By [smoom78a] at [2007-11-27 6:09:54]
# 1

import java.awt.*;

import java.awt.datatransfer.*;

import java.awt.event.*;

import javax.swing.*;

public class DragNoDrop {

private JPanel getContent() {

JLabel dropEnabled = new JLabel("drag and drop enabled");

dropEnabled.setBorder(BorderFactory.createEtchedBorder());

dropEnabled.setTransferHandler(new TransferHandler("text"));

dropEnabled.addMouseListener(ml);

JLabel noDrop = new JLabel("drop not enabled");

noDrop.setBorder(BorderFactory.createEtchedBorder());

noDrop.setTransferHandler(new TransferHandler("text") {

public boolean canImport(JComponent comp, DataFlavor[] transferFlaors) {

return false;

}

});

noDrop.addMouseListener(ml);

JTextField textField = new JTextField("hello world", 12);

textField.setDragEnabled(true);

dropEnabled.setPreferredSize(textField.getPreferredSize());

noDrop.setPreferredSize(textField.getPreferredSize());

JPanel panel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.weighty = 1.0;

gbc.gridwidth = GridBagConstraints.REMAINDER;

panel.add(dropEnabled, gbc);

panel.add(noDrop, gbc);

panel.add(textField, gbc);

return panel;

}

private MouseListener ml = new MouseAdapter() {

public void mousePressed(MouseEvent e) {

JComponent c = (JComponent)e.getSource();

TransferHandler handler = c.getTransferHandler();

handler.exportAsDrag(c, e, TransferHandler.COPY);

}

};

public static void main(String[] args) {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new DragNoDrop().getContent());

f.setSize(300,160);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-12 17:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks!!! That has sorted it right out!!:o)
smoom78a at 2007-7-12 17:14:30 > top of Java-index,Desktop,Core GUI APIs...