Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JTable within JTree

Thread Tools

JTable within JTree

On Ali
Posts: n/a
Hi all,

I have a code in which I display a JTable within a JTree.

There is a specific requirement that the row height of the JTable
should scale based on the JTree's height change.

The code which i have written changes the JTable row height correctly.
However, the outer JTree does not get compacted.

When the row height is decreased (by decreasing the frame height) there
is space between the next sibling and the JTable.
When the row height is increased (by increasing the frame height), the
JTable gets clipped.

I do not want to set the row height for the JTree as there can be
multiple JTables with different number of rows.
It should be dynamically calculated based on the node.

A sample code is as below:

* Created on January 19, 2007, 11:18 AM

package research;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;

* @author OnAli
public class TreeTabEx extends javax.swing.JFrame {

/** Creates new form TreeTabEx */
public TreeTabEx() {
jTree1.setCellRenderer(new MyTreeRenderer());
DefaultMutableTreeNode root = new DefaultMutableTreeNode("The
String[] [] data = { {"One", "two"}, {"three", "four"}, {"six",
"seven"} };
root.add(new MyTreeNode(data));
root.add(new DefaultMutableTreeNode("one child"));
jTree1.setModel(new DefaultTreeModel(root));

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);


}// </editor-fold>

* @param args the command line arguments
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TreeTabEx().setVisible(true);

// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
// End of variables declaration


class MyTreeRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object
value, boolean sel, boolean expanded, boolean leaf, int row, boolean
hasFocus) {
int rowheight = (int)((tree.getSize().height / 200.0) * 25);
if(value instanceof MyTreeNode) {
String[] [] data = ((MyTreeNode) value).getData();
JTable table = new JTable(data.length, data[0].length);
Vector dataVector = new Vector();
Vector columnRow = new Vector();
for(int j = 0 ; j < data[0].length; j++) {
for(int i = 1; i < data.length; i++) {
Vector dataRow = new Vector();
for(int j = 0 ; j < data[i].length; j++) {
table.setModel(new DefaultTableModel(dataVector,
return table;
return new JLabel(value.toString());

class MyTableRenderer extends DefaultTableCellRenderer {
private double heightfactor = 1;
MyTableRenderer(double heightfactor) {
this.heightfactor = heightfactor;
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {
int rowheight = (int)(16 * heightfactor);
if(rowheight != table.getRowHeight()) {
return new JLabel((String)value.toString());

class MyTreeNode extends DefaultMutableTreeNode {
private String[] [] data = null;
MyTreeNode(String[] [] data) { = data;
String[] [] getData() {

Can any body suggest a solution to make the JTree calculate the correct
row height for each of the nodes?
Could this be just a repaint/refresh problem?

Thanks in advance

On Ali

Reply With Quote
Andrew Thompson
Posts: n/a
On Jan 27, 12:50 am, "On Ali" <(E-Mail Removed)> wrote:
> Hi all,

Please cross-post, rather than multi-post.

(X-post to c.l.j.p./g., w/ f-u to c.l.j.g. only)

Andrew T.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
JTree Selection lost when JTree looses focus jumpz Java 3 11-12-2007 03:55 PM
Is it possible to put a JPanel within a JTable cell? Nigel Java 0 05-31-2007 12:54 PM
Putting a JTable inside a JTable cell? Tivo Escobar Java 1 04-12-2007 11:09 AM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
JTreeTable problem - JTree not showing inside the JTable Abraham Khalil Java 0 09-17-2003 05:13 AM