• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

render jtree in jtable

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
}
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic