如何将列表转换为到 DefaultMutableTreeNode?
如何将列表转换为DefaultMutableTreeNode?
我正在从列表中的数据库中获取数据。现在我想将该列表转换为 JTree。我该怎么做?
public class ExpressionTree extends JFrame implements ActionListener {
private JComboBox group;
private JLabel groupLabel;
Container content;
JTree tree;
DefaultMutableTreeNode root, child;
public static void main(String[] args) {
new ExpressionTree();
}
public ExpressionTree() {
super("Creating a Simple JTree");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
content = getContentPane();
group = new JComboBox();
groupLabel = new JLabel("GROUP");
ExpressionBuilder expressionBuilder = new ExpressionBuilder();
Set<Integer> set = expressionBuilder.getGroup().keySet();
Iterator<Integer> iterator = set.iterator();
group.addItem("select");
while(iterator.hasNext()){
group.addItem(expressionBuilder.getGroup().get(iterator.next()));
}
content.add(groupLabel);
content.add(group);
group.addActionListener(this);
content.setLayout(new FlowLayout());
root= new DefaultMutableTreeNode("root");
tree = new JTree(root);
content.add(new JScrollPane(tree), BorderLayout.CENTER);
setSize(300, 475);
setVisible(true);
}
public String groupString = null;
public int groupId;
public void actionPerformed(ActionEvent e){
groupString = group.getSelectedItem().toString();
ExpressionBuilder builder=new ExpressionBuilder();
builder.getGroupId(groupString);
//tree.setModel(new NodeTree());.........//what to write over here
//root.add(new NodeTree());
}
}
public class NodeTree extends DefaultMutableTreeNode{
private JTree tree;
DefaultMutableTreeNode top;
public NodeTree(){
ExpressionTree expressionTree= new ExpressionTree();
String groupstring = expressionTree.groupString;
int groupId = expressionTree.groupId;
top = new DefaultMutableTreeNode(groupstring);
tree = new JTree(top);
tree.setEditable(true);
JScrollPane treeView = new JScrollPane(tree);
createNode(top,groupId);
}
public void createNode(DefaultMutableTreeNode top,int group){
DefaultMutableTreeNode expression[] = null;
ExpressionBuilder builder=new ExpressionBuilder();
Object object = (Object)builder.getExpression(group,0);
expression[1] = new DefaultMutableTreeNode(object);
top.add(expression[1]);
expression[1].add(new DefaultMutableTreeNode((Object)builder.getExpression(group,1)));
}
}
How to convert list to DefaultMutableTreeNode?
I am fetching data from database in list. Now i want to convert that list to JTree. How can i do it?
public class ExpressionTree extends JFrame implements ActionListener {
private JComboBox group;
private JLabel groupLabel;
Container content;
JTree tree;
DefaultMutableTreeNode root, child;
public static void main(String[] args) {
new ExpressionTree();
}
public ExpressionTree() {
super("Creating a Simple JTree");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
content = getContentPane();
group = new JComboBox();
groupLabel = new JLabel("GROUP");
ExpressionBuilder expressionBuilder = new ExpressionBuilder();
Set<Integer> set = expressionBuilder.getGroup().keySet();
Iterator<Integer> iterator = set.iterator();
group.addItem("select");
while(iterator.hasNext()){
group.addItem(expressionBuilder.getGroup().get(iterator.next()));
}
content.add(groupLabel);
content.add(group);
group.addActionListener(this);
content.setLayout(new FlowLayout());
root= new DefaultMutableTreeNode("root");
tree = new JTree(root);
content.add(new JScrollPane(tree), BorderLayout.CENTER);
setSize(300, 475);
setVisible(true);
}
public String groupString = null;
public int groupId;
public void actionPerformed(ActionEvent e){
groupString = group.getSelectedItem().toString();
ExpressionBuilder builder=new ExpressionBuilder();
builder.getGroupId(groupString);
//tree.setModel(new NodeTree());.........//what to write over here
//root.add(new NodeTree());
}
}
public class NodeTree extends DefaultMutableTreeNode{
private JTree tree;
DefaultMutableTreeNode top;
public NodeTree(){
ExpressionTree expressionTree= new ExpressionTree();
String groupstring = expressionTree.groupString;
int groupId = expressionTree.groupId;
top = new DefaultMutableTreeNode(groupstring);
tree = new JTree(top);
tree.setEditable(true);
JScrollPane treeView = new JScrollPane(tree);
createNode(top,groupId);
}
public void createNode(DefaultMutableTreeNode top,int group){
DefaultMutableTreeNode expression[] = null;
ExpressionBuilder builder=new ExpressionBuilder();
Object object = (Object)builder.getExpression(group,0);
expression[1] = new DefaultMutableTreeNode(object);
top.add(expression[1]);
expression[1].add(new DefaultMutableTreeNode((Object)builder.getExpression(group,1)));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您从数据库中正确获取节点名称并且节点是扁平的(无层次结构),您可以使用此代码创建一棵树
注意:我创建了虚拟节点作为根并使用了 setRootVisible(false) 。如果您有有效的根节点,则可以使用它并删除隐藏根的行。
Assuming that you are fetching the node names properly from database and the nodes are flat (no hierarchy), you can create a tree with this code
Note: I created dummy node as root and used the
setRootVisible(false)
. If you have a valid root node you can use it and remove the line that is hiding the root.