JTree 遍历叶节点
我用JTree做了一个QQ好友列表。当服务器发送一个在线好友的ID过来,如"100 500 200",ID之间用空格分隔。我要如何遍历JTree,找到该叶节点,并设置setEnable(true)。求帮忙解决。字数有限,贴出关键代码:
public class QQClientMain extends JFrame {
//登入者id
private String owner;
// 是否有消息
private static boolean hasMessage;
// 初始化中部(好友列表和群列表的显示)
public void initCenter() {
// 初始化好友列表
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new TreeNode("0","好友列表"));
// 一级节点
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(new TreeNode("1","我的好友"));
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(new TreeNode("2","陌生人"));
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode(new TreeNode("3","黑名单"));
// 子节点
// 好友
DefaultMutableTreeNode node1_1 = new DefaultMutableTreeNode(new TreeNode("100","张三","image/1-1.gif", "0"));
DefaultMutableTreeNode node1_2 = new DefaultMutableTreeNode(new TreeNode("200","李四","image/2-1.gif", "0"));
DefaultMutableTreeNode node1_3 = new DefaultMutableTreeNode(new TreeNode("300","王五","image/3-1.gif", "0"));
DefaultMutableTreeNode node1_4 = new DefaultMutableTreeNode(new TreeNode("400","李流","image/4-1.gif", "0"));
DefaultMutableTreeNode node1_5 = new DefaultMutableTreeNode(new TreeNode("500","黄山","image/5-1.gif", "0"));
DefaultMutableTreeNode node1_6 = new DefaultMutableTreeNode(new TreeNode("600","武当","image/6-1.gif", "0"));
// 陌生人
DefaultMutableTreeNode node2_1 = new DefaultMutableTreeNode(new TreeNode("1300","张三1","image/4-1.gif", "0"));
// 黑名单
DefaultMutableTreeNode node3_1 = new DefaultMutableTreeNode(new TreeNode("1010","张三1","image/2-1.gif", "0"));
jpt1_tree1 = new JTree(root);
jpt1_tree1.setCellRenderer(new MyCellRenderer());
jpt1_jsp1 = new JScrollPane(jpt1_tree1);
// 设置单击展开树节点
jpt1_tree1.setToggleClickCount(1);
root.add(node1);
root.add(node2);
root.add(node3);
node1.add(node1_1);
node1.add(node1_2);
node1.add(node1_3);
node1.add(node1_4);
node1.add(node1_5);
node1.add(node1_6);
node2.add(node2_1);
node3.add(node3_1);
//设置是否显示根节点的“展开/折叠”图标,默认是false
jpt1_tree1.setShowsRootHandles(false);
//设置节点是否可见,默认是true
jpt1_tree1.setRootVisible(false);
jpt1_tree1.setEditable(false);
//设置使用Windows风格外观
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (
ClassNotFoundException e1) {
e1.printStackTrace();
} catch (
InstantiationException e1) {
e1.printStackTrace();
} catch (
IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
//更新JTree的UI外观
SwingUtilities.updateComponentTreeUI(jpt1_tree1);
// 创建选项卡窗口
jtp1 = new JTabbedPane(JTabbedPane.TOP);
// 选项卡一:好友列表