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

发布于 2024-12-08 09:52:47 字数 1360 浏览 0 评论 0原文

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

JLabel 是一个具有有关计算机的某些属性的图标,通过将信息从 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);

I want to implement DnD on my system by dragging a node from a JTree and dropping it onto a JLabel.

The JLabel is an Icon with certain properties about a machine, and by dragging the information from the JTree node onto the JLabel I want it to be able to send a message to a client listening on that machine.

Any help is much appreciated!

Example of label method:

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 method:

    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);
  }
}

Then in order to make my tree draggable I just have:

exampleTree.setDragEnabled(true);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨轻弹 2024-12-15 09:52:47

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

编辑

以获取TreePath 在树上也使用自定义 Transferhandler:覆盖其返回 TreePath 的 createTransferable:

    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

not sure I understand your setup: assuming you want the label to be a drop target, simply implement a custom Transferhandler which accepts the dataflavour as exported by the tree and do something with it

EDIT

To get hold of the TreePath use a custom Transferhandler on the tree as well: override its createTransferable which returns the 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);

no need for an additional mouseListener

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文