各位,如果在JTree上实现右键拖放?本人试过如果用左键可以,但如果同时按下shift再左键拖放就失效了,本人想知如何用右键拖放一个jtree上的数据到一个jtable,或者用右键拖放Jtree上的数据到jtable?
解决方案 »
- private List<DownloaderTask> dwnTasks = new List<DownloaderTask>()这句话干什么用的?
- 求助递归方法的解决??
- 我是新手,请高手帮助~~~我想做个文本输入程序。。请高手多多指点~~~
- 寻找P2P JXTA的大虾们,你们在哪啊?……我哭啊!~
- 关于className及调用其方法的问题。有点晕了
- 我写的applet在本地用浏览器打开点击后回跳转到另一页。但是上传到网上然后打开就不能跳转到另一个页面了。
- 看过<大家看这么简单的句子错在那了?>的进来接分!
- 关于Socket请求和响应的问题,请socket高手赐教。
- 讨论:如何用java替换文件中的几个字符?
- 装了weblogic 6.1,照说明,PETSTORE总是出不来,求救各位
- 求助,对 super 的调用必须是构造函数中的第一个语句
- 关于面向接口编程的一点疑惑
import java.awt.Point;
import java.awt.event.MouseEvent;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.event.MouseInputAdapter;
import javax.swing.tree.TreePath;
public class T
{
public static void main(String[] args)
{
DragHandler dragHandler = new DragHandler();
JTree tree = new JTree();
tree.setTransferHandler(new TransferHandler("selectionPath"));
tree.addMouseListener(dragHandler);
tree.addMouseMotionListener(dragHandler); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
new JScrollPane(tree), new JScrollPane(new TestTable()));
JFrame f = new JFrame();
f.getContentPane().add(splitPane, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class DragHandler extends MouseInputAdapter
{
private TreePath path;
private Point location; @Override
public void mousePressed(MouseEvent e)
{
if (SwingUtilities.isRightMouseButton(e)) {
JTree tree = (JTree) e.getSource();
path = tree.getPathForLocation(e.getX(), e.getY());
if (path != null) {
tree.setSelectionPath(path);
location = e.getPoint();
}
else {
path = null;
location = null;
}
}
}
@Override
public void mouseDragged(MouseEvent e)
{
if (SwingUtilities.isRightMouseButton(e) && path != null && location != null) {
if ((location.x - e.getX()) * (location.x - e.getX()) +
(location.y - e.getY()) * (location.y - e.getY()) > 10 * 10) {
JTree tree = (JTree) e.getSource();
TransferHandler transferHandler = tree.getTransferHandler();
if (transferHandler != null) {
transferHandler.exportAsDrag(tree, e, TransferHandler.COPY);
path = null;
location = null;
}
}
}
}
}
import javax.swing.TransferHandler;
import javax.swing.tree.TreePath;public class TestTable extends JTable
{
public TestTable()
{
super(5, 5);
setTransferHandler(new TransferHandler("treePath"));
}
public TreePath getTreePath()
{
return null;
}
public void setTreePath(TreePath treePath)
{
JTable.DropLocation dropLoc = getDropLocation();
setValueAt(treePath.getLastPathComponent().toString(), dropLoc.getRow(), dropLoc.getColumn());
}
}