从JTree拖动节点并放到JLabel上以执行操作

时间:2011-10-07 08:50:58

标签: java swing drag-and-drop applet jtree

我想通过从JTree拖动节点并将其放到JLabel上来在我的系统上实现DnD。

JLabel是一个具有某些机器属性的Icon,通过将信息从JTree节点拖到JLabel上,我希望它能够向在该机器上侦听的客户端发送消息。

非常感谢任何帮助!

标签方法示例:

private void makeLabel(String html, final String version) {
    // Create a button to link to the DR environment
    //JButton button = new JButton(html);
    JLabel machineLabel = new JLabel();
    machineLabel.setTransferHandler(new TransferHandler("text"));
    MouseListener listener = new DragMouseAdapter();
    machineLabel.addMouseListener(listener);
    machineLabel.setIcon(onlineIcon);
    machineLabel.setToolTipText("IP: " + html);
    //Add the button to the panel and make sure it appears
    machineLabel.setSize(25, 10);
    vecIcons.addElement(machineLabel);
    buttonPanel.add(machineLabel);
    buttonPanel.setVisible(true);
    buttonPanel.validate();
    dynaScrollPane.validate();
    buttonPanel.repaint();
    dynaScrollPane.repaint();
  }

DragMouseAdapter方法:

    private class DragMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
  JComponent c = (JComponent) e.getSource();
  TransferHandler handler = c.getTransferHandler();
  handler.exportAsDrag(c, e, TransferHandler.LINK);
  }
}

然后为了使我的树可以拖动我只有:

exampleTree.setDragEnabled(true);

1 个答案:

答案 0 :(得分:3)

不确定我是否理解您的设置:假设您希望标签成为放置目标,只需实现一个自定义Transferhandler,它接受树导出的dataflavour并使用它执行某些操作

修改

要掌握TreePath,还要在树上使用自定义Transferhandler:覆盖其createTransferable,它返回TreePath:

    final DataFlavor flavor =new DataFlavor(TreePath.class, "treePath");
    TransferHandler treeHandler = new TransferHandler() {
        DataFlavor[] pathFlavour = new DataFlavor[] 
                  {flavor};

        /** 
         * @inherited <p>
         */
        @Override
        protected Transferable createTransferable(JComponent c) {
            JTree tree = (JTree) c;
            final TreePath path = tree.getSelectionPath();
            Transferable transferable = new Transferable() {

                @Override
                public DataFlavor[] getTransferDataFlavors() {
                    return pathFlavour;
                }

                @Override
                public boolean isDataFlavorSupported(DataFlavor flavor) {
                    return pathFlavour[0].equals(flavor);
                }

                @Override
                public Object getTransferData(DataFlavor flavor)
                        throws UnsupportedFlavorException, IOException {
                    return path;
                }

            };
            return transferable;
        }

        public int getSourceActions(JComponent c) {
            return COPY;
        }

    };
    tree.setTransferHandler(treeHandler);
    JLabel labelTarget = new JLabel("I'm a drop target!");
    TransferHandler labelHandler = new TransferHandler() {

        /** 
         * @inherited <p>
         */
        @Override
        public boolean importData(JComponent comp, Transferable t) {
            try {
                LOG.info("import from: " +  t.getTransferData(flavor));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }

        /** 
         * @inherited <p>
         */
        @Override
        public boolean canImport(JComponent comp,
                DataFlavor[] transferFlavors) {
            return true;
        }

    };
    labelTarget.setTransferHandler(labelHandler);

无需额外的mouseListener