Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

JTable within JTree

Thread view: 
On Ali - 26 Jan 2007 13:50 GMT
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.

Problem:
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:

/*
* TreeTabEx.java
*
* Created on January 19, 2007, 11:18 AM
*/

package research;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.Vector;
import javax.management.modelmbean.ModelMBean;
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() {
       initComponents();
       jTree1.setCellRenderer(new MyTreeRenderer());
       DefaultMutableTreeNode root = new DefaultMutableTreeNode("The
Root");
       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.WindowConstants.EXIT_ON_CLOSE);
       jScrollPane1.setViewportView(jTree1);

       getContentPane().add(jScrollPane1,
java.awt.BorderLayout.CENTER);

       pack();
   }// </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);
       System.out.println(rowheight);
       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++) {
               columnRow.add(data[0][j]);
           }
           for(int i = 1; i < data.length; i++) {
               Vector dataRow = new Vector();
               for(int j = 0 ; j < data[i].length; j++) {
                   dataRow.add(data[i][j]);
               }
               dataVector.add(dataRow);
           }
           table.setModel(new DefaultTableModel(dataVector,
columnRow));
           table.setRowHeight(rowheight);
           return table;
       }
       return new JLabel(value.toString());
   }
}

class MyTableRenderer extends DefaultTableCellRenderer {
   private double heightfactor = 1;
   MyTableRenderer(double heightfactor) {
     super();
     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()) {
           table.setRowHeight(rowheight);
       }
       return new JLabel((String)value.toString());
   }
}

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

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
Andrew Thompson - 26 Jan 2007 14:44 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.