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 JTable In JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable In JTree" Watch "JTable In JTree" New topic
Author

JTable In JTree

Shah Devang
Greenhorn

Joined: Jul 27, 2006
Posts: 12
I am trying to implement a UI component wherein a leaf node in JTree when expanded shows a populated table. Below is the code for this, can anyone please check where am I going wrong ...

/************ TreeComponent ************/

import java.awt.Container;
import java.awt.Dimension;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.JScrollPane;

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

public class TreeComponent {

JPanel topPanel = null;
JFrame frame = null;
Container contentPane = null;
Dimension d = null;
private DefaultMutableTreeNode root = null, node = null, parent = null;

public TreeComponent () {

try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}

frame = new JFrame("Table In Tree Component");

contentPane = frame.getContentPane();
d = frame.getToolkit().getScreenSize();

frame.setSize(700, 600);
frame.setLocation(d.width/2-frame.getWidth()/2, d.height/2-frame.getHeight()/2);

initComponent();

JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public void initComponent () {
// panel with Border Layout
topPanel = new JPanel(new BorderLayout());
JTree tree = createTreeComponent();

JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(tree);
topPanel.add(scrollPane, BorderLayout.CENTER);

topPanel.setVisible(true);
contentPane.add(topPanel);
}

public JTree createTreeComponent() {
DefaultTreeModel treeModel = null;
treeModel = createTreeModel();

JTree tree = new JTree(treeModel);
tree.setCellRenderer(new CustomTreeCellRenderer());
tree.setCellEditor(new CustomTreeCellEditor(tree));
tree.setEditable(false);
return tree;
}

public DefaultTreeModel createTreeModel() {
root = new DefaultMutableTreeNode("Tree Top");
parent = root;

node = new DefaultMutableTreeNode("Table A");
node.add(new DefaultMutableTreeNode(new TableComponent()));
parent.add(node);

node = new DefaultMutableTreeNode("Table B");
parent.add(node);

return new DefaultTreeModel(root);
}

public static void main (String args[]) {
new TreeComponent();
}
}

/************ TableComponent ************/


import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import javax.swing.table.TableColumn;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;


public class TableComponent extends JPanel {
private JTable table = null;
private JScrollPane scrollPane = null;

public TableComponent() {
super(new GridLayout(1,1));
table = new JTable(new CustomTableModel());
initColumnSizes();
table.setPreferredScrollableViewportSize(new Dimension(150, 100));
scrollPane = new JScrollPane(table);
add(scrollPane);
}

public void initColumnSizes() {
TableColumn column = null;
Component component = null;
int headerWidth = 0;
int cellWidth = 0;
JTableHeader tableHeader = null;

CustomTableModel tableModel = (CustomTableModel)table.getModel();

Object longValues[] = tableModel.longValues;

tableHeader = table.getTableHeader();
TableCellRenderer headerRenderer = tableHeader.getDefaultRenderer();

for( int i=0; i<5; i++ ) {
column = table.getColumnModel().getColumn(i);
component = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
headerWidth = component.getPreferredSize().width;

System.out.println(table.getDefaultRenderer(tableModel.getColumnClass(i)).getTableCellRendererComponent(table, longValues, false, false, 0, i));
component = table.getDefaultRenderer(tableModel.getColumnClass(i)).getTableCellRendererComponent(table, longValues, false, false, 0, i);
cellWidth = component.getPreferredSize().width;

column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}

public static void main(String args[]) {
JFrame frame = new JFrame();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
frame.getContentPane().add(new TableComponent());
frame.setSize(350, 350);
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}

/************ CustomTableModel ************/

public class CustomTableModel extends AbstractTableModel {
private boolean DEBUG = true;
private String[] columnNames = {
"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"
};
private Object[][] data = {
{ "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false) },
{ "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
{ "Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false) },
{ "Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true) },
{ "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) }
};

public final Object[] longValues = { "Sharon", "Campione", "None of the above", new Integer(20), Boolean.TRUE };

public int getColumnCount() {
return columnNames.length;
}

public int getRowCount() {
return data.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
}

/************ CustomTreeCellEditor ************/

import java.awt.Component;

import javax.swing.JTree;
import javax.swing.JScrollPane;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;


public class CustomTreeCellEditor extends DefaultTreeCellEditor {
public CustomTreeCellEditor(JTree tree) {
super(tree, (DefaultTreeCellRenderer)null);
}

public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
if ( value instanceof DefaultMutableTreeNode ) {
DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) value;
if ( node.getUserObject() instanceof TableComponent ) {
return new JScrollPane( ( TableComponent ) node.getUserObject() );
}
}
return super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
}
}


/************ CustomTreeCellRenderer ************/

import java.awt.Component;

import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

public class CustomTreeCellRenderer extends DefaultTreeCellRenderer {

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
if (node.getUserObject() instanceof TableComponent) {
return new JScrollPane((TableComponent) node.getUserObject());
}
}
return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
}
}


/*****************************************/

Do let me know at the earliest.
Execute the TreeComponent class
[ July 27, 2006: Message edited by: Shah Devang ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Have you seen this article on TreeTables. And a thread about compiling it in j2se 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable In JTree