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);
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With