简体   繁体   中英

Jtree inside JScrollPanel not working

DefaultMutableTreeNode myComputer = new DefaultMutableTreeNode("My Computer");
DefaultMutableTreeNode c = new DefaultMutableTreeNode("Local Disk(C:)");
DefaultMutableTreeNode vinod = new DefaultMutableTreeNode("Vinod");
DefaultMutableTreeNode swing = new DefaultMutableTreeNode("Swing");
DefaultMutableTreeNode tr = new DefaultMutableTreeNode("Tree");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("3½ Floppy(A:)");
DefaultMutableTreeNode e = new DefaultMutableTreeNode("New Volume(E:)");
c.add(vinod);
vinod.add(swing);
swing.add(tr);
myComputer.add(c);
myComputer.add(a);
myComputer.add(e);


JTree tree = new JTree(myComputer);
JScrollPane scrollPane = new JScrollPane(tree);
jPanel1.add(scrollPane);
tree.setVisible(true);

I got the new tree example from the web, but when I try to show it, it does not appear! I dont know why. Any ideas? Thank you!

Adjusted the code to an SSCCE , which works fine here

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame();

        DefaultMutableTreeNode myComputer = new DefaultMutableTreeNode("My Computer");
        DefaultMutableTreeNode c = new DefaultMutableTreeNode("Local Disk(C:)");
        DefaultMutableTreeNode vinod = new DefaultMutableTreeNode("Vinod");
        DefaultMutableTreeNode swing = new DefaultMutableTreeNode("Swing");
        DefaultMutableTreeNode tr = new DefaultMutableTreeNode("Tree");
        DefaultMutableTreeNode a = new DefaultMutableTreeNode("3½ Floppy(A:)");
        DefaultMutableTreeNode e = new DefaultMutableTreeNode("New Volume(E:)");
        c.add(vinod);
        vinod.add(swing);
        swing.add(tr);
        myComputer.add(c);
        myComputer.add(a);
        myComputer.add(e);


        JTree tree = new JTree(myComputer);
        JScrollPane scrollPane = new JScrollPane(tree);
        frame.getContentPane().add(scrollPane);
        tree.setVisible(true);

        frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
      }
    } );
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM