File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "render jtree in jtable" Watch "render jtree in jtable" New topic
Author

render jtree in jtable

Aditya Singh
Ranch Hand

Joined: Mar 06, 2008
Posts: 62
Hi,
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 );
frame.pack();
frame.setVisible(true);
}

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

public Tree(JTable table, int column)
{
super();
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)
{
renderButton.setForeground(table.getForeground());
renderButton.setBackground(UIManager.getColor("Button.background"));
}
else if (isSelected)
{
renderButton.setForeground(table.getSelectionForeground());
renderButton.setBackground(table.getSelectionBackground());
}
else
{
renderButton.setForeground(table.getForeground());
renderButton.setBackground(UIManager.getColor("Button.background"));
}

DefaultTreeModel treeModel = createDataModel();
renderButton.setModel(treeModel);
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"));

root.add(child);
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();
editButton.setModel(treeModel);;
return editButton;
}

public Object getCellEditorValue()
{
return text;
}

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

Joined: Sep 20, 2005
Posts: 862
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
 
wood burning stoves
 
subject: render jtree in jtable
 
Similar Threads
JTree
Checkbox with Jtree
JTable In JTree
DND from JTree to JTable
Code for JTree with checkbox