目前我们只支持书本上的问题,对于出本之外的代码和需求我们不予解答。
如果你是想知道如何维护树的结构和如何获取选中的树节点,可以通过下面这个例子来了解一下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class ExampleFrame_05 extends JFrame {
private JTextField textField;
private DefaultTreeModel treeModel;
private JTree tree;
public static void main(String args[]) {
ExampleFrame_05 frame = new ExampleFrame_05();
frame.setVisible(true);
}
public ExampleFrame_05() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("根节点");
DefaultMutableTreeNode nodeA = new DefaultMutableTreeNode("经理办公室");
root.add(nodeA);
DefaultMutableTreeNode nodeB = new DefaultMutableTreeNode("人事部");
root.add(nodeB);
DefaultMutableTreeNode nodeC = new DefaultMutableTreeNode("技术部");
root.add(nodeC);
nodeC.add(new DefaultMutableTreeNode("网络维护部"));
nodeC.add(new DefaultMutableTreeNode("应用开发部"));
treeModel = new DefaultTreeModel(root, true);
tree = new JTree(treeModel);
tree.setSelectionPath(new TreePath(root));
scrollPane.setViewportView(tree);
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.SOUTH);
final JLabel label = new JLabel();
label.setText("名称:");
panel.add(label);
textField = new JTextField();
textField.setColumns(8);
textField.setText("测试");
panel.add(textField);
tree.addTreeSelectionListener(new TreeSelectionListener() { // 树选中节点时触发的监听
public void valueChanged(TreeSelectionEvent e) {
if (!tree.isSelectionEmpty()) {// 查看是否存在被选中的节点
// 获得所有被选中节点的路径
TreePath[] selectionPaths = tree.getSelectionPaths();
for (int i = 0; i < selectionPaths.length; i++) {
// 获得被选中节点的路径
TreePath treePath = selectionPaths[i];
// 以Object数组的形式返回该路径中所有节点的对象
Object[] path = treePath.getPath();
for (int j = 0; j < path.length; j++) {
DefaultMutableTreeNode node;// 获得节点
node = (DefaultMutableTreeNode) path[j];
String s = node.getUserObject()
+ (j == (path.length - 1) ? "" : "-->");
System.out.print(s);// 输出节点标签
}
System.out.println();
}
System.out.println();
}
}
});
final JButton addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(textField.getText());// 创建欲添加节点
TreePath selectionPath = tree.getSelectionPath();// 获得选中的父节点路径
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();// 获得选中的父节点
treeModel.insertNodeInto(node, parentNode, parentNode.getChildCount());// 插入节点到所有子节点之后
TreePath path = selectionPath.pathByAddingChild(node);// 获得新添加节点的路径
if (!tree.isVisible(path))
tree.makeVisible(path);// 如果该节点不可见则令其可见
}
});
panel.add(addButton);
final JButton updButton = new JButton("修改");
updButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获得选中的欲修改节点的路径
TreePath selectionPath = tree.getSelectionPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();// 获得选中的欲修改节点
node.setUserObject(textField.getText());// 修改节点的用户标签
treeModel.nodeChanged(node);// 通知树模型该节点已经被修改
tree.setSelectionPath(selectionPath);// 选中被修改的节点
}
});
panel.add(updButton);
final JButton delButton = new JButton("删除");
delButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();// 获得选中的欲删除节点
// 查看欲删除的节点是否为根节点,根节点不允许删除
if (!node.isRoot()) {
DefaultMutableTreeNode nextSelectedNode = node.getNextSibling();// 获得下一个兄弟节点,以备选中
if (nextSelectedNode == null)// 查看是否存在兄弟节点
nextSelectedNode = (DefaultMutableTreeNode) node.getParent();// 如果不存在则选中其父节点
treeModel.removeNodeFromParent(node);// 删除节点
tree.setSelectionPath(new TreePath(nextSelectedNode.getPath()));// 选中节点
}
}
});
panel.add(delButton);
}
}