我正在创建这个应用程序,它在按钮单击事件上创建选项卡和 JTree。问题是当我尝试将 ne 节点添加到 JTree 时,它不会刷新 JTree(或者它不会向其中添加节点......我真的不知道)。
这是创建选项卡和树的函数:
jTabbedPane1.add(st,jSplitpane10);
int count = jTabbedPane1.getTabCount();
jTabbedPane1.setSelectedIndex(count-1);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("All Notebooks");
DefaultMutableTreeNode notebook1 = new DefaultMutableTreeNode("Notebook 1");
root.add(notebook1);
// Create tree
JTree tree = new JTree(root);
//Create Scroll Pane for the tree
JScrollPane sp = new JScrollPane(tree);
Global.trees.add(tree);
这是应该将新节点“Green”添加到所选选项卡中的树的代码:
int i = jTabbedPane1.getSelectedIndex();
DefaultTreeModel model = (DefaultTreeModel)Global.trees.get(i).getModel();
// Find node to which new node is to be added
int startRow = 0;
String prefix = "J";
TreePath path = Global.trees.get(i).getNextMatch(prefix, startRow, Position.Bias.Forward);
MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();
// Create new node
MutableTreeNode newNode = new DefaultMutableTreeNode("green");
// Insert new node as last child of node
model.insertNodeInto(newNode, node, node.getChildCount());
model.reload(newNode);
这也是 JTree 全局列表的声明:
public class Global {
public java.util.List<JTree> trees = new ArrayList<JTree>();
}Global Global;
任何想法为什么新节点没有显示在树中???