This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

render jtree in jtable  RSS feed

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 964
1
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!