JTree 未显示在 SplitPane 的左侧 ScrollPane 内
JTree not showing up inside left ScrollPane of SplitPane
我正在尝试实现一个两侧都带有滚动窗格的拆分窗格。左侧应该显示我正在尝试实现的 JTree,但它不工作,我看不到树。
我不确定我做错了什么。我的代码是这样的:
public class SplitPane extends JFrame {
DefaultTreeModel treeModel;
JEditorPane editorPane = new JEditorPane();
DefaultMutableTreeNode Root;
JTree tree;
JScrollPane leftscrollPane;
JScrollPane rightscrollPane;
public SplitPane() {
setSize(600,400);
tree = new JTree(Root);
Root = new DefaultMutableTreeNode("");
setTree(); // I connect all the nodes here
treeModel = new DefaultTreeModel(Root);
tree = new JTree(Root);
tree.setRootVisible(false);
leftscrollPane = new JScrollPane(tree);
rightscrollPane = new JScrollPane(editorPane);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(leftscrollPane);
splitPane.setRightComponent(rightscrollPane);
splitPane.setDividerLocation(160);
setVisible(true);
splitPane.setPreferredSize(new Dimension(600,400));
getContentPane().add(splitPane);
}
}
然后初始化,我就这样SplitPane newpane = new SplitPane();
我想我已经正确添加了所有节点,因为当我这样做时
Enumeration e = Root.preorderEnumeration();
while(e.hasMoreElements()) {
System.out.println(e.nextElement());
}
我按顺序查看所有节点。
我做错了什么?非常感谢您的帮助和反馈!
你的第二个
tree = new JTree(Root);
必须使用模型进行初始化:
tree = new JTree(treeModel);
你可以删除树的第一个初始化,因为它被第二个覆盖了。
这是一个运行宁的例子:
public class SplitPane extends JFrame {
private MutableTreeNode createTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
root.add(new DefaultMutableTreeNode("child 1"));
root.add(new DefaultMutableTreeNode("child 2"));
return root;
}
public SplitPane() {
setSize(600, 400);
// create model and add nodes
TreeModel model = new DefaultTreeModel(createTree());
// initialize tree, set the model
JTree tree = new JTree(model);
tree.setRootVisible(false);
JScrollPane leftScrollPane = new JScrollPane(tree);
JScrollPane rightScrollPane = new JScrollPane(new JLabel("Text ..."));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightScrollPane);
splitPane.setDividerLocation(200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(splitPane);
this.setVisible(true);
}
}
您可以 运行 这个例子,只需像这样在某个地方实例化它:
SplitPane splitPane = new SplitPane();
我正在尝试实现一个两侧都带有滚动窗格的拆分窗格。左侧应该显示我正在尝试实现的 JTree,但它不工作,我看不到树。
我不确定我做错了什么。我的代码是这样的:
public class SplitPane extends JFrame {
DefaultTreeModel treeModel;
JEditorPane editorPane = new JEditorPane();
DefaultMutableTreeNode Root;
JTree tree;
JScrollPane leftscrollPane;
JScrollPane rightscrollPane;
public SplitPane() {
setSize(600,400);
tree = new JTree(Root);
Root = new DefaultMutableTreeNode("");
setTree(); // I connect all the nodes here
treeModel = new DefaultTreeModel(Root);
tree = new JTree(Root);
tree.setRootVisible(false);
leftscrollPane = new JScrollPane(tree);
rightscrollPane = new JScrollPane(editorPane);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(leftscrollPane);
splitPane.setRightComponent(rightscrollPane);
splitPane.setDividerLocation(160);
setVisible(true);
splitPane.setPreferredSize(new Dimension(600,400));
getContentPane().add(splitPane);
}
}
然后初始化,我就这样SplitPane newpane = new SplitPane();
我想我已经正确添加了所有节点,因为当我这样做时
Enumeration e = Root.preorderEnumeration();
while(e.hasMoreElements()) {
System.out.println(e.nextElement());
}
我按顺序查看所有节点。
我做错了什么?非常感谢您的帮助和反馈!
你的第二个
tree = new JTree(Root);
必须使用模型进行初始化:
tree = new JTree(treeModel);
你可以删除树的第一个初始化,因为它被第二个覆盖了。
这是一个运行宁的例子:
public class SplitPane extends JFrame {
private MutableTreeNode createTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
root.add(new DefaultMutableTreeNode("child 1"));
root.add(new DefaultMutableTreeNode("child 2"));
return root;
}
public SplitPane() {
setSize(600, 400);
// create model and add nodes
TreeModel model = new DefaultTreeModel(createTree());
// initialize tree, set the model
JTree tree = new JTree(model);
tree.setRootVisible(false);
JScrollPane leftScrollPane = new JScrollPane(tree);
JScrollPane rightScrollPane = new JScrollPane(new JLabel("Text ..."));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightScrollPane);
splitPane.setDividerLocation(200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(splitPane);
this.setVisible(true);
}
}
您可以 运行 这个例子,只需像这样在某个地方实例化它:
SplitPane splitPane = new SplitPane();