从JTable拖放到JLayeredPane [英] Drag and Drop from JTable to JLayeredPane

查看:108
本文介绍了从JTable拖放到JLayeredPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 JTable JLayeredPane 之间创建拖放.

How to create Drag and drop between JTable and JLayeredPane.

有没有人知道如何实现这一目标?

Does anyone have any ideas how to implement this?

我需要通过拖放在JTable和JLayeredPane之间传输对象.

推荐答案

您必须至少做几件事才能使它起作用:

You have to do a few things at minimum to get this to work:

  • 在您的JTable实例上调用setDragEnabled(true)
  • 设置JTableJLayeredPane
  • Call setDragEnabled(true) on your JTable instance
  • Set the JTables and JLayeredPanes DropTarget
  • Create a draggable Component to add to the JLayeredPane

要设置Component s DropTarget,请调用setDropTarget(DropTarget d)方法并传递一个匿名内部类作为参数.删除 Transferable s到Component的数据应该位于drop(DropTargetDropEvent dtde)方法中.

To set a Components DropTarget, call the setDropTarget(DropTarget d) method and pass an anonymous inner class as the argument. Code that drops the Transferables data into the Component should be located in the drop(DropTargetDropEvent dtde) method.

下面的代码片段显示了如何接收Transferable并将其插入到发起放置的JTable行中:

The below snippet shows how to receive a Transferable and insert it into the row of a JTable where the drop was initiated:

table.setDropTarget(new DropTarget() {

    @Override
    public synchronized void drop(DropTargetDropEvent dtde) {
        try {
            // get row to put new item in
            int row = table.rowAtPoint(dtde.getLocation());
            // inserting row:
            ((DefaultTableModel) table.getModel()).insertRow(
                    //if the row wasn't found, add it to the end of the JTable
                    row == -1 ? table.getRowCount() : row,
                    // pass string flavor of transferable data as row data parameter
                    new Object[] {dtde.getTransferable()
                                .getTransferData(DataFlavor.stringFlavor)});
        } catch(UnsupportedFlavorException | IOException e) {
            e.printStackTrace();
        }
    }
});

下面的代码片段再次接收到Transferable,但是将其添加到JLayeredPane的末尾.请注意,因为我为JLayeredPane分配了BoxLayout(SwingConstants.VERTICAL),所以组件被添加到了JLayeredPane的末尾.

This below snippet again receives a Transferable but instead adds it to the end of a JLayeredPane. Note that components are added to the end of the JLayeredPane because I assign a BoxLayout(SwingConstants.VERTICAL) to it.

layeredPane.setDropTarget(new DropTarget() {

    @Override
    public synchronized void drop(DropTargetDropEvent dtde) {
        try {
            // create draggable label to add to layered pane 
            // creating this class will be explained next
            final DraggableLabel label = new DraggableLabel(
                    (String) dtde.getTransferable()
                    .getTransferData(DataFlavor.stringFlavor));

            // add label to layered pane
            layeredPane.add(label, JLayeredPane.DEFAULT_LAYER);
            layeredPane.revalidate();
            layeredPane.repaint();
        } catch(UnsupportedFlavorException | IOException e) {
            e.printStackTrace();
        }
    }

});


要创建可拖动的Component(在本示例中为JLabel),您必须:


To create a draggable Component (In this example a JLabel), you must:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆