Forums Register Login
render jtree in jtable
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,

package com.ubs;

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());
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.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

All times above are in ranch (not your local) time.
The current ranch time is
Oct 19, 2017 14:20:44.