1.1 創建樹組件
1.1.1 最簡方式:
在顯示時,樹組件會帶上JTree默認的節點。
1.1.2 指定樹的節點後創建樹:
root.add(new DefaultMutableTreeNode("子節點"));
JTree tree = new JTree(root);
1.1.3 將樹加入到面板中
以下是將樹加上滾動窗格後加入一個面板的示例程序。
panel.setLayout(new GridLayout(1,1));
panel.add(new JScrollPane(tree));
1.2 樹節點相關
1.2.1 取得樹的根節點
DefaultMutableTreeNode root =(DefaultMutableTreeNode)model.getRoot();
1.2.2 更新樹的根節點
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.setRoot(newRoot);
model.reload();
tree.updateUI();
1.2.3 從一個節點開始遞歸遍歷其下的所有節點
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 在某節點下創建一個節點
childNode.setUserObject();
parentNode.add(childNode);
1.2.5 節點改名
selectedCategory.setName("");
clickedNode.setUserObject(selectedCategory);
1.2.6 刪除節點
model.removeNodeFromParent(clickedNode);
注意被刪除的節點必須有父節點,若要刪除根節點直接用null去替代原有根節點即可,
1.2.7 遍歷用戶選擇的樹節點
for(TreePath selPath:paths){
Object[] nodes = selPath.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes[nodes.length - 1];
// 對node進行處理
}
1.3 樹的事件處理
1.3.1 左鍵點擊事件處理
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 在樹節點上點擊右鍵彈出右鍵菜單
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 樹節點渲染器示例
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 用樹節點渲染器渲染一棵樹
JTree tree = new JTree(root);
tree.setCellRenderer(new CategoryNodeRenderer());