This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes render jtree in jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "render jtree in jtable" Watch "render jtree in jtable" New topic

render jtree in jtable

Aditya Singh
Ranch Hand

Joined: Mar 06, 2008
Posts: 62
I got a sample program for showing JButton in JTable. I modified it to show Jtree in Jtable column. But its not working.
Here is the code,


import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class TreeTable extends JFrame
public TreeTable()
String[] columnNames = {"Date", "String", "Integer", "Decimal", ""};
Object[][] data =
{new Date(), "A", new Integer(1), new Double(5.1), "Tree0"},
{new Date(), "B", new Integer(2), new Double(6.2), "Tree1"},
{new Date(), "C", new Integer(3), new Double(7.3), "Tree2"},
{new Date(), "D", new Integer(4), new Double(8.4), "Tree3"}

DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model )
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
return getValueAt(0, column).getClass();

JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );

// Create button column
Tree buttonColumn = new Tree(table, 4);

public static void main(String[] args)
TreeTable frame = new TreeTable();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

class Tree extends AbstractCellEditor
implements TableCellRenderer, TableCellEditor, ActionListener
JTable table;
JTree renderButton;
JTree editButton;
String text;

public Tree(JTable table, int column)
this.table = table;
renderButton = new JTree();

editButton = new JTree();
//editButton.setFocusPainted( false );
//editButton.addActionListener( this );

TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(column).setCellRenderer( this );
columnModel.getColumn(column).setCellEditor( this );

public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
if (hasFocus)
else if (isSelected)

DefaultTreeModel treeModel = createDataModel();
return renderButton;

private DefaultTreeModel createDataModel() {
//IconNode[] nodes = new IconNode[strs.length];

DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("child");
child.add(new DefaultMutableTreeNode("dummy"));

return new DefaultTreeModel(root);

public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
text = (value == null) ? "" : value.toString();
//editButton.setText( text );
DefaultTreeModel treeModel = createDataModel();
return editButton;

public Object getCellEditorValue()
return text;

public void actionPerformed(ActionEvent e)
System.out.println( e.getActionCommand() + " : " + table.getSelectedRow());
Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 872
Originally posted by Aditya Singh:

I got a sample program for showing JButton in JTable. I modified it to show Jtree in Jtable column. But its not working.

In what way isn't it working? You might want to tell the details.
(also: Use code tags.)

I might quibble with parts of your code, but I don't see why it
wouldn't work. You might want to call table.setRowHeight(50)
or something to make more room for the tree-containing cells.

btw, you might want to rename this to something other than TreeTable.
TreeTable is the name usually given to a table that contains a single
tree that fills an entire column, not a table with one tree per row.

bitguru blog
I agree. Here's the link:
subject: render jtree in jtable
jQuery in Action, 3rd edition