I am using the first example mentioned here for creating my JTree but I want to put more than one tree as root in the JscrollPanel. I just changed the layout of the box and create one more tree with the same code but when I run the program the tree is not appearing to me.
我使用这里提到的第一个例子来创建JTree,但是我想在JscrollPanel中放置不止一棵树作为根。我只是改变了框的布局,用相同的代码创建了另一棵树,但是当我运行程序时,树并没有出现在我的面前。
Here is my code
这是我的代码
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class SimpleTree extends JFrame {
public static void main(String[] args) {
new SimpleTree();
}
public SimpleTree() {
super("Creating a Simple JTree");
Container content = getContentPane();
Object[] hierarchy =
{ "javax.swing",
"javax.swing.border",
"javax.swing.colorchooser",
"javax.swing.event",
"javax.swing.filechooser",
new Object[] { "javax.swing.plaf",
"javax.swing.plaf.basic",
"javax.swing.plaf.metal",
"javax.swing.plaf.multi" },
"javax.swing.table",
new Object[] { "javax.swing.text",
new Object[] { "javax.swing.text.html",
"javax.swing.text.html.parser" },
"javax.swing.text.rtf" },
"javax.swing.tree",
"javax.swing.undo" };
DefaultMutableTreeNode root = processHierarchy(hierarchy);
DefaultMutableTreeNode root2 = processHierarchy(hierarchy);
JTree tree = new JTree(root);
JTree tree2 = new JTree(root2);
content.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
content.add(new JScrollPane(tree));
content.add(new JScrollPane(tree2));
setSize(getPreferredSize());
setVisible(true);
}
/** Small routine that will make node out of the first entry
* in the array, then make nodes out of subsequent entries
* and make them child nodes of the first one. The process is
* repeated recursively for entries that are arrays.
*/
private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
DefaultMutableTreeNode node =
new DefaultMutableTreeNode(hierarchy[0]);
DefaultMutableTreeNode child;
for(int i=1; i<hierarchy.length; i++) {
Object nodeSpecifier = hierarchy[i];
if (nodeSpecifier instanceof Object[]) // Ie node with children
child = processHierarchy((Object[])nodeSpecifier);
else
child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
node.add(child);
}
return(node);
}
}
Could someone please help me with this. All help would be really appreciated.
有人能帮我一下吗?所有的帮助都是非常感谢的。
Thanks Ashish Tyagi
谢谢阿施施Tyagi
2 个解决方案
#1
3
A JScrollPane
has a view to a single child component in its ViewPort
.
JScrollPane具有其ViewPort中单个子组件的视图。
You could however place the 2 JScrollPane
components on a single panel with say, an evenly split GridLayout(2, 1)
and set the view of the JScrollPane
to the new panel.
但是,您可以将两个JScrollPane组件放在一个面板上,其中包括一个均匀分割的GridLayout(2,1),并将JScrollPane的视图设置为新面板。
Aside: You should use the ContentPane
as the container for BoxLayout
:
旁白:您应该使用ContentPane作为BoxLayout的容器:
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
#2
2
-
have to accepting that
JScrollPane
isn't designated to hold more than oneJComponents
必须接受JScrollPane没有指定包含多个JComponents的事实吗
-
you can to put
JPanel
intoJScrollPane
, put required numbers ofJTree
s to (inJScrollPane
)JPanel
, maybe to useGridLayout
forJPanel
您可以将JPanel放入JScrollPane,将所需的jtree数放入JPanel(在JScrollPane中),也可以将GridLayout用于JPanel
-
注意,可在父JScrollPane和每个jtree JScrollPanes之间进行可滚动
#1
3
A JScrollPane
has a view to a single child component in its ViewPort
.
JScrollPane具有其ViewPort中单个子组件的视图。
You could however place the 2 JScrollPane
components on a single panel with say, an evenly split GridLayout(2, 1)
and set the view of the JScrollPane
to the new panel.
但是,您可以将两个JScrollPane组件放在一个面板上,其中包括一个均匀分割的GridLayout(2,1),并将JScrollPane的视图设置为新面板。
Aside: You should use the ContentPane
as the container for BoxLayout
:
旁白:您应该使用ContentPane作为BoxLayout的容器:
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
#2
2
-
have to accepting that
JScrollPane
isn't designated to hold more than oneJComponents
必须接受JScrollPane没有指定包含多个JComponents的事实吗
-
you can to put
JPanel
intoJScrollPane
, put required numbers ofJTree
s to (inJScrollPane
)JPanel
, maybe to useGridLayout
forJPanel
您可以将JPanel放入JScrollPane,将所需的jtree数放入JPanel(在JScrollPane中),也可以将GridLayout用于JPanel
-
注意,可在父JScrollPane和每个jtree JScrollPanes之间进行可滚动