使用transferHandler将文本从JLabel拖放到JTable

时间:2016-08-22 06:11:23

标签: java swing drag-and-drop jtable jlabel

我正在按照下面提到的教程。在本教程的主题Extending Default DnD Support下,有一个应用JTextAreaJListJTable

它完美无缺。另外我使用了JLabel。我将此标签的传输处理程序设置为:

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

当我将这个标签拖到桌子上时,它并不接受它。但它适用于文本区域的字符串。

如何将标签上的文字拖放到桌子上?

2 个答案:

答案 0 :(得分:2)

new TransferHandler("text");
  

https://docs.oracle.com/javase/8/docs/api/javax/swing/TransferHandler.html

     

实现此类是为了提供传递组件属性的默认行为,只需在构造函数中指定属性的名称即可。例如,要通过剪贴板或拖放操作将前景色从一个组件传输到另一个组件,可以使用字符串"前景"构建TransferHandler。内置支持将使用getForeground返回的颜色作为传输源,并使用setForeground作为传输目标。

JTable没有setText(String)方法。因此,您需要覆盖TransferHander#importData(...)方法以将数据导入JTable的单元格。

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class DnDTest {
  public JComponent makeUI() {
    JTable table = new JTable(5, 3);
    table.setAutoCreateRowSorter(true);
    table.setTransferHandler(new ToTransferHandler());

    JLabel label = new JLabel("aaaaaaaaaaaa");
    label.setTransferHandler(new TransferHandler("text"));
    label.addMouseListener(new MouseAdapter() {
      @Override public void mousePressed(MouseEvent e) {
        JComponent c = (JComponent) e.getSource();
        c.getTransferHandler().exportAsDrag(c, e, TransferHandler.COPY);
      }
    });

    JPanel p = new JPanel(new BorderLayout());
    p.add(new JScrollPane(table));
    p.add(label, BorderLayout.SOUTH);
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new DnDTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

//@see https://docs.oracle.com/javase/tutorial/uiswing/dnd/dropactiondemo.html
class ToTransferHandler extends TransferHandler {
  @Override public boolean canImport(TransferHandler.TransferSupport support) {
    return getPropertyDataFlavor(String.class, support.getDataFlavors()) != null;
  }
  @Override public boolean importData(TransferHandler.TransferSupport support) {
    // if we can't handle the import, say so
    if (!canImport(support)) {
      return false;
    }

    DataFlavor flavor = getPropertyDataFlavor(String.class, support.getDataFlavors());

    // fetch the data and bail if this fails
    String data;
    try {
      data = (String) support.getTransferable().getTransferData(flavor);
    } catch (UnsupportedFlavorException | java.io.IOException e) {
      return false;
    }

    JTable.DropLocation dl = (JTable.DropLocation) support.getDropLocation();
    JTable table = (JTable) support.getComponent();
    table.setValueAt(data, dl.getRow(), dl.getColumn());
    return true;
  }
  //@see TransferHandler.java private DataFlavor getPropertyDataFlavor(...) {
  private DataFlavor getPropertyDataFlavor(Class<?> k, DataFlavor[] flavors) {
    for (int i = 0; i < flavors.length; i++) {
      DataFlavor flavor = flavors[i];
      if ("application".equals(flavor.getPrimaryType()) &&
          "x-java-jvm-local-objectref".equals(flavor.getSubType()) &&
          k.isAssignableFrom(flavor.getRepresentationClass())) {
        return flavor;
      }
    }
    return null;
  }
}

答案 1 :(得分:1)

似乎您需要手动向标签添加鼠标事件处理程序来执行此操作:

jLabel1.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent evt) {
    JComponent comp = (JComponent) evt.getSource();
    TransferHandler th = comp.getTransferHandler();

    th.exportAsDrag(comp, evt, TransferHandler.COPY);
  }
});

来源:http://www.java2s.com/Tutorial/Java/0240__Swing/AddingDragandDropSupporttoaJLabelComponent.htm