@Override public void keyPressed(KeyEvent e) { TreePath path = myTree.getLeadSelectionPath(); if (path == null) return; final Object lastComponent = path.getLastPathComponent(); if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (lastComponent instanceof ParentNode) return; doOKAction(); e.consume(); } else if (e.getKeyCode() == KeyEvent.VK_INSERT) { if (lastComponent instanceof ElementNode) { final DefaultMutableTreeNode node = (DefaultMutableTreeNode) lastComponent; if (!mySelectedNodes.contains(node)) { if (node.getNextNode() != null) { myTree.setSelectionPath(new TreePath(node.getNextNode().getPath())); } } else { if (node.getNextNode() != null) { myTree.removeSelectionPath(new TreePath(node.getPath())); myTree.setSelectionPath(new TreePath(node.getNextNode().getPath())); myTree.repaint(); } } e.consume(); } } }
public void actionPerformed(ActionEvent arg0) { DefaultMutableTreeNode curTreeNode = (DefaultMutableTreeNode) treWhere.getLastSelectedPathComponent(); if (curTreeNode == null) { new MessageBox(calcColumnDialog, "请选择树节点!", MessageBox.MESSAGE, MessageBox.BUTTON_OK) .show(); return; } if (!curTreeNode.isLeaf()) return; int row = treWhere.getSelectionRows()[0]; DefaultMutableTreeNode nextTreeNode = curTreeNode.getNextNode(); if (!nextTreeNode.isLeaf()) return; DefaultTreeModel model = (DefaultTreeModel) treWhere.getModel(); DefaultMutableTreeNode parTreeNode = (DefaultMutableTreeNode) curTreeNode.getParent(); int index = parTreeNode.getIndex(curTreeNode); model.removeNodeFromParent(curTreeNode); model.insertNodeInto(curTreeNode, parTreeNode, index + 1); // 刷新节点中文名称 if (curTreeNode != null) refreshNodeChName(curTreeNode); if (curTreeNode.getPreviousSibling() != null) refreshNodeChName(curTreeNode.getPreviousSibling()); model.nodeChanged(curTreeNode); model.nodeChanged(curTreeNode.getPreviousSibling()); treWhere.repaint(); treWhere.setSelectionRow(row + 1); }