Query by the MouseListener and MouseAdapter Java method

0

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);
}
    
asked by ignacio tapia 09.11.2018 в 20:46
source

1 answer

0

Investigating on the internet I managed to find the answer to my query in case anyone else who sees this question serves.

public void mousePressed(MouseEvent e) { for (int i = 0; i < drag.getComponentCount(); i++) { if (drag.getComponent(i) instanceof JLabel) { ((JLabel) drag.getComponent(i)).setTransferHandler(new TransferHandler("text")); } } JComponent jc = (JComponent) e.getSource(); TransferHandler th = jc.getTransferHandler(); th.exportAsDrag(jc, e, TransferHandler.COPY); for (int i = 0; i < drag.getComponentCount(); i++) { if (drag.getComponent(i) instanceof JLabel) { ((JLabel) drag.getComponent(i)).setTransferHandler(new TransferHandler("")); } } }

In the mousePressed event that allows us to know when we click and keep it in a component, I ask for the components within my jpanel "drag" counting them and traversing them one by one. Then I ask for the component number "i" located in the jpanel and if this instance of jlabel , I give the property to be able to move and be copied in another place.

Then I do the same step, but with the difference that I give them the property of transferhandler empty so that when you have the jlabel with the mouse you can not overwrite the others.

    
answered by 12.11.2018 в 16:33