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);
// 选项卡一:好友列表
jtp1_jpl1 = new JPanel(new BorderLayout());
jtp1_jpl1.add(jpt1_jsp1, "Center");
jtp1_jpl2 = new JPanel();
jpt1_jpl3 = new JPanel();
jtp1.add(jtp1_jpl1, "联系人");
jtp1.add(jtp1_jpl2, "群");
jtp1.add(jpt1_jpl3, "最近联系人");
jtp1.setBounds(10, 160, 235, 280);
this.add(jtp1);
// 为好友列表树添加响应事件
jpt1_tree1.addMouseListener(new MouseAdapter() {
// // 返回当前选择的第一个节点中的最后一个路径组件
// DefaultMutableTreeNode dmtnTemp = (DefaultMutableTreeNode) jpt1_tree1.getLastSelectedPathComponent();
@Override
public void mouseClicked(MouseEvent e) {
JTree tree = (JTree) e.getSource();
int selRow = tree.getRowForLocation(e.getX(), e.getY());
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
if(node != null) {
// 返回此节点的用户对象
TreeNode treeNode = (TreeNode) node.getUserObject();
// 如果是叶子节点
if(node.isLeaf()) {
// 如果是双击
if(e.getClickCount() == 2) {
// 获得要聊天的好友id
String friendId = treeNode.getId();
QQChat qqChat = new QQChat(owner, friendId);
//把聊天界面加入管理聊天窗口的类中
ManageQQChat.addQQChat(owner + " " + friendId, qqChat);
}
}
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
super.mouseExited(arg0);
}
});
}
class MyCellRenderer extends DefaultTreeCellRenderer {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
// 获得节点对象
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
TreeNode treeNode = (TreeNode) node.getUserObject();
String id = treeNode.getId();
String nickname = treeNode.getValue();
String image = treeNode.getImage();
// 设置叶节点的图标
if(leaf && image != null) {
setLeafIcon(new ImageIcon(image));
}
// 设置展开的节点的图标
if(expanded) {
setOpenIcon(new ImageIcon("image/openFloder.jpg"));
}
// 设置关闭的节点的图标
else {
setClosedIcon(new ImageIcon("image/closeFloder.jpg"));
}
// 执行父类默认的节点绘制操作
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
// 设置节点显示内容
this.setText(nickname);
// 节点状态
String statue = treeNode.getStatue();
// 设置节点灰色
if(leaf && statue != null) {
this.setEnabled(false);
// 在线状态显示高亮
if(treeNode.getStatue().equals(QQStatue.qq_online)) {
this.setEnabled(true);
}
// 本人显示高亮
if(treeNode.getId().equals(owner)) {
this.setEnabled(true);
}
}
return this;
}
}
public static void main(String[] args) {
new QQClientMain("600");
}
}
========================================
public class TreeNode {
// 节点编号
String id;
// 节点值
String value;
// 节点图片
String image;
// 节点状态
String statue;
//............
}
(运行效果请见http://photo.blog.sina.com.cn/photo/8bcfeedagc855de609d6b)
7 个解决方案
#1
jTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
String name=node.toString();
if(node.isLeaf()){
if(name.equals("100")){
setEnable(true); }
});
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
String name=node.toString();
if(node.isLeaf()){
if(name.equals("100")){
setEnable(true); }
});
#2
#3
DefaultMutableTreeNode 提供遍历树的方法。
Enumeration depthFirstEnumeration() 深度优先的顺序遍历
Enumeration breadthFirstEnumeration() 广度优先的顺序遍历
比如:
Enumeration e = tree.breadthFirstEnumeration();
while(e.hasMoreElements()){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
..............
..............
}
Enumeration depthFirstEnumeration() 深度优先的顺序遍历
Enumeration breadthFirstEnumeration() 广度优先的顺序遍历
比如:
Enumeration e = tree.breadthFirstEnumeration();
while(e.hasMoreElements()){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
..............
..............
}
#4
一般来说遍历树结构建议用递归,也就是从根节点开始,进行递归搜索叶子节点。
大致如下:
大致如下:
// 主入口函数
public void setOnline(DefaultMutableTreeNode root, String friends) {
String[] fs = friends.split("\\s");
HashSet<String> set = new HashSet<String>(Arrays.asList(fs)); // 转成Set便于快速检索
setOnlineRecursion(root, set); // 调用递归子函数
xxoo.UpdateUI(); // 刷新下树显示
}
// 递归函数
private void setOnlineRecursion(DefaultMutableTreeNode node, Set onlines) {
Enumeration<DefaultMutableTreeNode> children = node.children();
while (children.hasMoreElements()) {
DefaultMutableTreeNode child = children.nextElement();
if (child.isLeaf()) { // 是否叶子节点
TreeNode treeNode = (TreeNode) node.getUserObject();
if (onlines.contains(treeNode.getId())) { // 检查该ID是否在set中
treeNode.setStatus(QQStatue.在线);
} else {
treeNode.setStatus(QQStatue.离线);
}
} else {
// 非叶子节点则继续递归调用了
setOnlineRecursion(child, onlines);
}
}
}
#5
不知道楼主想用setEnable方法达到什么效果,DefaultMutableTreeNode应该没用这个方法吧。
#6
setEnable,是为了显示不在线的灰色效果。
#7
jtree 可以有用setCellRenderer方法,为结点设置颜色,背景。
jdk上一个例子:
DefaultMutableTreeNode root = createNewNode("Root");
DefaultTreeModel treeModel = new SampleTreeModel(root);
JTree tree = new JTree(treeModel);
tree.setCellRenderer(new SampleTreeCellRenderer());
.........................................
具体可查下jdk目录demo/jfc/SampleTree/src下的例子
jdk上一个例子:
DefaultMutableTreeNode root = createNewNode("Root");
DefaultTreeModel treeModel = new SampleTreeModel(root);
JTree tree = new JTree(treeModel);
tree.setCellRenderer(new SampleTreeCellRenderer());
.........................................
具体可查下jdk目录demo/jfc/SampleTree/src下的例子
#1
jTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
String name=node.toString();
if(node.isLeaf()){
if(name.equals("100")){
setEnable(true); }
});
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
String name=node.toString();
if(node.isLeaf()){
if(name.equals("100")){
setEnable(true); }
});
#2
#3
DefaultMutableTreeNode 提供遍历树的方法。
Enumeration depthFirstEnumeration() 深度优先的顺序遍历
Enumeration breadthFirstEnumeration() 广度优先的顺序遍历
比如:
Enumeration e = tree.breadthFirstEnumeration();
while(e.hasMoreElements()){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
..............
..............
}
Enumeration depthFirstEnumeration() 深度优先的顺序遍历
Enumeration breadthFirstEnumeration() 广度优先的顺序遍历
比如:
Enumeration e = tree.breadthFirstEnumeration();
while(e.hasMoreElements()){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
..............
..............
}
#4
一般来说遍历树结构建议用递归,也就是从根节点开始,进行递归搜索叶子节点。
大致如下:
大致如下:
// 主入口函数
public void setOnline(DefaultMutableTreeNode root, String friends) {
String[] fs = friends.split("\\s");
HashSet<String> set = new HashSet<String>(Arrays.asList(fs)); // 转成Set便于快速检索
setOnlineRecursion(root, set); // 调用递归子函数
xxoo.UpdateUI(); // 刷新下树显示
}
// 递归函数
private void setOnlineRecursion(DefaultMutableTreeNode node, Set onlines) {
Enumeration<DefaultMutableTreeNode> children = node.children();
while (children.hasMoreElements()) {
DefaultMutableTreeNode child = children.nextElement();
if (child.isLeaf()) { // 是否叶子节点
TreeNode treeNode = (TreeNode) node.getUserObject();
if (onlines.contains(treeNode.getId())) { // 检查该ID是否在set中
treeNode.setStatus(QQStatue.在线);
} else {
treeNode.setStatus(QQStatue.离线);
}
} else {
// 非叶子节点则继续递归调用了
setOnlineRecursion(child, onlines);
}
}
}
#5
不知道楼主想用setEnable方法达到什么效果,DefaultMutableTreeNode应该没用这个方法吧。
#6
setEnable,是为了显示不在线的灰色效果。
#7
jtree 可以有用setCellRenderer方法,为结点设置颜色,背景。
jdk上一个例子:
DefaultMutableTreeNode root = createNewNode("Root");
DefaultTreeModel treeModel = new SampleTreeModel(root);
JTree tree = new JTree(treeModel);
tree.setCellRenderer(new SampleTreeCellRenderer());
.........................................
具体可查下jdk目录demo/jfc/SampleTree/src下的例子
jdk上一个例子:
DefaultMutableTreeNode root = createNewNode("Root");
DefaultTreeModel treeModel = new SampleTreeModel(root);
JTree tree = new JTree(treeModel);
tree.setCellRenderer(new SampleTreeCellRenderer());
.........................................
具体可查下jdk目录demo/jfc/SampleTree/src下的例子