I have created a project which contains jtextarea
and jlabel
, adding to jlabe
l the option of transferhandler
in order to drag the label and write it in the textarea. My problem is that when I drag this jlabel
over another in the same panel, the label is overwritten with the text I'm dragging.
I enclose my code.
Method that allows me to move the label when keeping the mouse click:
MouseListener ml = new MouseAdapter() {
//creamos el método para transferir
//datos al presionar con el ratón
public void mousePressed(MouseEvent e) {
JComponent jc = (JComponent) e.getSource();
TransferHandler th = jc.getTransferHandler();
System.out.println("tengo el label");
th.exportAsDrag(jc, e, TransferHandler.COPY);
}
};
Method that allows me to add jlabel
to jpanel
and gives them the property of mouselistener
along with transferhandler
:
if (RsNameKeyword2.next()) {
mencion = RsNameKeyword2.getString(1);
mencion = "\n" + mencion;
etiqueta[i] = new JLabel(mencion);
etiqueta[i].setTransferHandler(new TransferHandler("text"));
etiqueta[i].addMouseListener(ml);
etiqueta[i].setEnabled(true);
//etiqueta[i].setTransferHandler(new TransferHandler(programa));
JLabel labelFecha = new JLabel(fechaMencion);
drag.setLayout(layout);
c.gridx = 0;
c.gridy = indice;
c.fill = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1.0D;
c.weighty = 1.0D;
//etiqueta[i].setBounds(0, nuevoY, 30, 10);
//etiqueta[i].setBorder(new CompoundBorder(border, margin));
//etiqueta[i].setPreferredSize(new Dimension(300, 40));
//etiqueta[i].setHorizontalAlignment(JLabel.LEFT);
//etiqueta[i].setBackground(Color.CYAN);
drag.add(etiqueta[i], c);
c.gridx = 1;
c.gridy = indice;
c.fill = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1.0D;
c.weighty = 1.0D;
//etiqueta[i].setBounds(0, nuevoY, 30, 10);
//etiqueta[i].setBorder(new CompoundBorder(border, margin));
//etiqueta[i].setPreferredSize(new Dimension(300, 40));
//etiqueta[i].setHorizontalAlignment(JLabel.LEFT);
//etiqueta[i].setBackground(Color.CYAN);
drag.add(labelFecha, c);
}