Topic: JTree的问题:

  Print this page

1.JTree的问题: Copy to clipboard
Posted by: vssivl
Posted on: 2004-10-26 18:32

用一个Vector构造了一个JTree,调用setRootVisible( true ); 生成了一个默认的root根节点,请问如何把这个root节点的标签或者说显示出来的名字改为别的?

2.Re:JTree的问题: [Re: vssivl] Copy to clipboard
Posted by: yamakasy
Posted on: 2004-10-26 18:53

重载该节点中包含的UserObject的toString方法

3.Re:JTree的问题: [Re: vssivl] Copy to clipboard
Posted by: kylin
Posted on: 2004-10-29 15:13

给你一个简单的例子看一下吧,希望对你有帮助:)

import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import java.util.Vector;
import javax.swing.JTree;
import java.awt.event.WindowEvent;
import javax.swing.tree.DefaultMutableTreeNode;

public class treetest {
public static void main(String[] args) {
Vector v = new Vector();
v.add("one");
v.add("two");
v.add("three");
JTree tree = new JTree(v);
tree.setRootVisible(true);
DefaultMutableTreeNode root = (DefaultMutableTreeNode)tree.getModel().getRoot();
root.setUserObject("自定义根节点名称");
JFrame frame = new JFrame();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
frame.getContentPane().add(tree);
frame.setSize(600, 300);
frame.setLocation(240, 200);
frame.setVisible(true);

}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923