Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTree does not show up

Tags:

java

swing

jtree

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);
like image 385
FadelMS Avatar asked Apr 26 '26 11:04

FadelMS


2 Answers

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.

like image 136
Snicolas Avatar answered Apr 29 '26 00:04

Snicolas


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.

like image 27
Hovercraft Full Of Eels Avatar answered Apr 28 '26 23:04

Hovercraft Full Of Eels