将节点从 JTree 拖放到 JLabel 上以执行操作
我想通过从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我是否理解您的设置:假设您希望标签成为放置目标,只需实现一个自定义 Transferhandler,它接受树导出的数据风味并对其执行某些操作
编辑
以获取TreePath 在树上也使用自定义 Transferhandler:覆盖其返回 TreePath 的 createTransferable:
不需要额外的 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:
no need for an additional mouseListener