关于JTree内部节点移动我写了下面的程序 是用鼠标事件实现的 程序没问题 但是托的过程中我希望鼠标能有个比如小方块的提示,写在
public void mouseDragged(MouseEvent e){
jtree.setCursor(new Cursor(Cursor.HAND_CURSOR));
}也没用,不知道怎么设置这个但貌似在JTree内部拖动可以用DragEnabled这个属性,所以我又写了第二个程序,是将文本框中的字符串拖入到树节点生成新的节点。但是如果要照此方法实现树的内部节点拖动该怎么做呢? 迷糊中(1)用鼠标事件实现数的内部节点移动 但是移动中没能改变鼠标形状
public class TreeDemo { private JFrame jframe;
private JTree jtree;

private DefaultMutableTreeNode deparment1;
private DefaultMutableTreeNode deparment2;
private DefaultMutableTreeNode deparment3;
private DefaultMutableTreeNode deparment4;
private DefaultMutableTreeNode deparment5;
private DefaultMutableTreeNode deparment6;
private DefaultMutableTreeNode deparment7;
private DefaultMutableTreeNode facilityRoot;
private JButton editButton;
private JButton addChildButton;
private JButton deleteButton;
private JButton addSiblingButton;

DefaultTreeModel model;
TreePath from;  

JPanel panel;

public void initTree() {
facilityRoot = new DefaultMutableTreeNode("facilityRoot");
deparment1 = new DefaultMutableTreeNode("department1");
deparment2 = new DefaultMutableTreeNode("department2");
deparment3 = new DefaultMutableTreeNode("department3");
deparment4 = new DefaultMutableTreeNode("department4");
deparment5 = new DefaultMutableTreeNode("department5");
deparment6 = new DefaultMutableTreeNode("department6");
deparment7 = new DefaultMutableTreeNode("department7");
facilityRoot.add(deparment1);
deparment1.add(deparment2);
deparment2.add(deparment3);
deparment3.add(deparment4);
facilityRoot.add(deparment5);
deparment5.add(deparment6);
deparment5.add(deparment7);

jtree = new JTree(facilityRoot);
jtree.setEditable(true);
jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

model = (DefaultTreeModel)jtree.getModel();

MouseListener ml = new MouseAdapter() {
 public void mousePressed(MouseEvent e) {
 TreePath originalPath = jtree.getPathForLocation(e.getX(), e.getY());
 if(originalPath != null) {
 from = originalPath;
 }
 }
 
 public void mouseReleased(MouseEvent e) {
 
 TreePath destinationPath = jtree.getPathForLocation(e.getX(), e.getY());
 if(destinationPath != null && from != null && destinationPath != from) {
 if(destinationPath.isDescendant(from)) {
 return;
 } else {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) destinationPath.getLastPathComponent();
node.add((DefaultMutableTreeNode)from.getLastPathComponent());
from = null;
jtree.updateUI();
 }
 }
 }
};

jtree.addMouseListener(ml);


}

public void initFrame() {
initTree();
jframe = new JFrame("drag and drop node between tree");
jframe.add(new JScrollPane(jtree));
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);

}

public static void main(String[] args) {
TreeDemo td = new TreeDemo();
td.initFrame();
}
}
(2)用DragEnabled属性 实现将文本框中的文字拖入到树中生成新的节点
但不知道怎么用类似的方法实现树内部节点的移动麻烦指点
public class DndTree {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame f = new JFrame("D-n-D JTree");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout());
JLabel dragLabel = new JLabel("Drag me:");
JTextField text = new JTextField();
text.setDragEnabled(true);
top.add(dragLabel, BorderLayout.WEST);
top.add(text, BorderLayout.CENTER);
f.add(top, BorderLayout.NORTH); final JTree tree = new JTree();
final DefaultTreeModel model = (DefaultTreeModel) tree
.getModel();
tree.setTransferHandler(new TransferHandler() {
public boolean canImport(
TransferHandler.TransferSupport support) {
if (!support
.isDataFlavorSupported(DataFlavor.stringFlavor)
|| !support.isDrop()) {
return false;
} JTree.DropLocation dropLocation = (JTree.DropLocation) support
.getDropLocation(); return dropLocation.getPath() != null;
} public boolean importData(
TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
} JTree.DropLocation dropLocation = (JTree.DropLocation) support
.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData;
try {
transferData = (String) transferable
.getTransferData(DataFlavor.stringFlavor);
} catch (IOException e) {
return false;
} catch (UnsupportedFlavorException e) {
return false;
} int childIndex = dropLocation.getChildIndex();
if (childIndex == -1) {
childIndex = model.getChildCount(path
.getLastPathComponent());
} DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
transferData);
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path
.getLastPathComponent();
model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode);
tree.makeVisible(newPath);
tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true;
}
}); JScrollPane pane = new JScrollPane(tree);
f.add(pane, BorderLayout.CENTER); JPanel bottom = new JPanel();
JLabel comboLabel = new JLabel("DropMode");
String options[] = { "USE_SELECTION", "ON", "INSERT",
"ON_OR_INSERT" };
final DropMode mode[] = { DropMode.USE_SELECTION, DropMode.ON,
DropMode.INSERT, DropMode.ON_OR_INSERT };
final JComboBox combo = new JComboBox(options);
combo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selectedIndex = combo.getSelectedIndex();
tree.setDropMode(mode[selectedIndex]);
}
});
bottom.add(comboLabel);
bottom.add(combo);
f.add(bottom, BorderLayout.SOUTH);
f.setSize(300, 400);
f.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}