Example of TreeTable

The example program about TreeTable discussed in Sun’s article Creating TreeTables: Part 2 adds unnecessary complexity. The following is a much simpler one and will get you started much quicker.

Prerequisite: Download src.zip from the above url and compile the with both programs presented here.


import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
public class Main extends JFrame {
   protected MyTreeTableModel  model;
   protected JTreeTable        treeTable;
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
      model     = new MyTreeTableModel();
      treeTable = new JTreeTable(model);
      getContentPane().add(new JScrollPane(treeTable));
      try {
      catch(Exception e) {
   public static void main(String[] args) {
      new Main();


import java.io.IOException;
import java.io.File;
import java.util.Date;
import java.util.Stack;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreePath;
import java.util.*;
public class MyTreeTableModel extends AbstractTreeTableModel {
   // Names of the columns.
   static protected String[] cNames = {"Column 1", "Column 2", 
                                       "Column 3", "Column 4"};
   // Types of the columns.
   static protected Class[]  cTypes = { TreeTableModel.class, String.class, 
                                        String.class, String.class };
   static Entry rootEntry;  
   /******** Entry class represents the parent and leaf nodes **********/
   static class Entry {
      private String name; 
      private boolean isLeaf;
      private Vector children = new Vector();
      public Entry(String name, boolean isLeaf) {
         this.name = name;
         this.isLeaf = isLeaf;
      public Vector getChildren() {
         return children;
      public boolean isLeaf() {
         return isLeaf;
      public String getName() {
         return name;
      public String toString() {
         return name;
   static {
      rootEntry = new Entry("rootentry", false);
      rootEntry.getChildren().addElement(new Entry("test1", true));
      rootEntry.getChildren().addElement(new Entry("test2", true));

      Entry subEntry1 = new Entry("subentry1", false);
      subEntry1.getChildren().addElement(new Entry("test3", true));
      subEntry1.getChildren().addElement(new Entry("test4", true));
      Entry subEntry2 = new Entry("subentry2", false);
      subEntry2.getChildren().addElement(new Entry("test5", true));
      subEntry2.getChildren().addElement(new Entry("test6", true));
   public MyTreeTableModel() { 
   public int getChildCount(Object node) { 
      if (!((Entry) node).isLeaf()) {
         return ((Entry) node).getChildren().size();
      return 0;
   public Object getChild(Object node, int i) { 
      return ((Entry) node).getChildren().elementAt(i);
   public boolean isLeaf(Object node) {
      return ((Entry) node).isLeaf();
   public int getColumnCount() {
      return cNames.length;
   public String getColumnName(int column) {
      return cNames[column];
   public Class getColumnClass(int column) {
      return cTypes[column];
   public Object getValueAt(Object node, int column) {
      switch(column) {
         case 0:
            return node;
         case 1:
            return "value1";
         case 2:
            return "value2";
         case 3:
            return "value3";
      return null;