1.1 创建树组件
1.1.1 最简方式:
JTree tree = new JTree();
在显示时,树组件会带上JTree默认的节点。
1.1.2 指定树的节点后创建树:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("根节点");
root.add(new DefaultMutableTreeNode("子节点"));
JTree tree = new JTree(root);
1.1.3 将树加入到面板中
以下是将树加上滚动窗格后加入一个面板的示例程序。
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,1));
panel.add(new JScrollPane(tree));
1.2 树节点相关
1.2.1 取得树的根节点
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root =(DefaultMutableTreeNode)model.getRoot();
1.2.2 更新树的根节点
DefaultMutableTreeNode newRoot=;
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.setRoot(newRoot);
model.reload();
tree.updateUI();
1.2.3 从一个节点开始递归遍历其下的所有节点
private String getNodeText(DefaultMutableTreeNode node){
Category category=(Category)node.getUserObject();
StringBuilder sb=new StringBuilder(category.getText());
if (node.getChildCount() >= 0) {
for (Enumeration<?> e=node.children(); e.hasMoreElements(); ) {
DefaultMutableTreeNode childNode = (DefaultMutableTreeNode)e.nextElement();
sb.append(getNodeText(childNode));
}
}
return sb.toString();
}
1.2.4 在某节点下创建一个节点
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode();
childNode.setUserObject();
parentNode.add(childNode);
1.2.5 节点改名
Category selectedCategory=(Category)clickedNode.getUserObject();
selectedCategory.setName("");
clickedNode.setUserObject(selectedCategory);
1.2.6 删除节点
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
model.removeNodeFromParent(clickedNode);
注意被删除的节点必须有父节点,若要删除根节点直接用null去替代原有根节点即可,
1.2.7 遍历用户选择的树节点
TreePath[] paths = tree.getSelectionPaths();
for(TreePath selPath:paths){
Object[] nodes = selPath.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes[nodes.length - 1];
// 对node进行处理
}
1.3 树的事件处理
1.3.1 左键点击事件处理
tree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int selRow = tree.getRowForLocation(e.getX(), e.getY());// 返回节点所在的行,-1表示鼠标定位不在显示的单元边界内
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());// 返回指定节点的树路径
boolean condition = true;
condition = condition && (selRow != -1);// 如果选中
//condition = condition && (e.getButton() == 1);// 左键 e.getButton() == 1 右键 e.getButton() == 3
condition = condition && (e.getClickCount() == 1);// 单击
// 如果是左键点击
if (condition == true && (e.getButton() == 1)) {
Object[] nodes = selPath.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes[nodes.length - 1];
Category selectedCategory=(Category)node.getUserObject();
}
}
});
1.3.2 在树节点上点击右键弹出右键菜单
tree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int selRow = tree.getRowForLocation(e.getX(), e.getY());// 返回节点所在的行,-1表示鼠标定位不在显示的单元边界内
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());// 返回指定节点的树路径
boolean condition = true;
condition = condition && (selRow != -1);// 如果选中
condition = condition && (e.getClickCount() == 1);// 单击
// 如果是右键点击
if(condition == true && (e.getButton() == 3)){
Object[] nodes = selPath.getPath();
DefaultMutableTreeNode rightClickedNode = (DefaultMutableTreeNode) nodes[nodes.length - 1];
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
1.4 树的渲染
1.4.1 树节点渲染器示例
public class CategoryNodeRenderer extends DefaultTreeCellRenderer{
private static final long serialVersionUID = 8532405600839140757L;
private static final ImageIcon categoryLeafIcon = new ImageIcon(CategoryNodeRenderer.class.getResource("/categoryLeaf.gif"));
private static final ImageIcon openFolderIcon = new ImageIcon(CategoryNodeRenderer.class.getResource("/openFolder.gif"));
private static final ImageIcon closedFolderIcon = new ImageIcon(CategoryNodeRenderer.class.getResource("/closedFolder.gif"));
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus){
super.getTreeCellRendererComponent(tree,
value,
sel,
expanded,
leaf,
row,
hasFocus);
if(leaf){
setIcon(categoryLeafIcon);
}
else if(expanded){
setIcon(openFolderIcon);
}
else{
setIcon(closedFolderIcon);
}
return this;
}
}
1.4.2 用树节点渲染器渲染一棵树
DefaultMutableTreeNode root = null;
JTree tree = new JTree(root);
tree.setCellRenderer(new CategoryNodeRenderer());