gpt4 book ai didi

java - 如何将 list 转换为 DefaultMutableTreeNode?

转载 作者:行者123 更新时间:2023-12-01 05:49:46 25 4
gpt4 key购买 nike

如何将列表转换为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)));

}

}

最佳答案

假设您从数据库中正确获取节点名称并且节点是扁平的(无层次结构),您可以使用此代码创建一棵树

private JTree createTree(List<String> nodeNames) {

DefaultMutableTreeNode root = new DefaultMutableTreeNode(new Object());
TreeModel model = new DefaultTreeModel(root);
for (int i = 0; i < nodeNames.size(); i++) {
root.add(new DefaultMutableTreeNode(nodeNames.get(i)));
}
JTree tree = new JTree(model);
tree.setRootVisible(false);
return tree;
}

注意:我创建了虚拟节点作为根节点并使用了setRootVisible(false)。如果您有有效的根节点,则可以使用它并删除隐藏根的行。

关于java - 如何将 list<String> 转换为 DefaultMutableTreeNode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5029124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com