Reputation: 2037
Why the JTree does not show up? Here is my code:
initComponents();
JTree treeView;
DefaultMutableTreeNode top = new DefaultMutableTreeNode("myBooks");
DefaultMutableTreeNode category = new DefaultMutableTreeNode("Drama");
DefaultMutableTreeNode book1 = new DefaultMutableTreeNode("Macbeth");
DefaultMutableTreeNode book2 = new DefaultMutableTreeNode("Hamlet");
category.add(book1);
category.add(book2);
top.add(category);
treeView = new JTree(top);
JScrollPane pane = new JScrollPane(treeView);
mainPanel.add(pane);
Upvotes: 3
Views: 1505
Reputation: 38168
Give your JTree
a background and see what space it occupies on screen. Also you can use CTRL + SHIFT + F1 on a swing app and see the different components in console with size, position and everything.
My guess is that mainPanel
doesn't have a good layout or the jtree should have a decent preferred size set.
Upvotes: 5
Reputation: 285440
To extend on Snicolas' answer (1+ to him) you appear to be adding the JScrollPane to your mainPanel without regard for the layout manager used. I'm guessing that your GUI is using the GroupLayout, and if so, I suggest you use a layout manager that is more user friendly. Also, are you adding the pane JScrollPane after pack and setVisible(true) are called on the top-level window? If so you'll need to revalidate and repaint the container that is receiving the new component.
Upvotes: 4