开发者

Dragging node from JTree and dropping onto JLabel to perform an action

开发者 https://www.devze.com 2023-04-11 15:16 出处:网络
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

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开发者_如何学Python 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);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号