Java Swing How to - Expand JTree row








Question

We would like to know how to expand JTree row.

Answer

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
/*from  w  ww .  j a  v a 2  s  .  c  om*/
public class Main {

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTree tree = new JTree();
    for (int i = 0; i < tree.getRowCount(); i++) {
      tree.expandRow(i);
    }
    f.add(new JScrollPane(tree));
    f.pack();
    f.setSize(200, 200);
    f.setVisible(true);
  }
}