How can I change jtable height at runtime

Posted by wniroshan on Stack Overflow See other posts from Stack Overflow or by wniroshan
Published on 2010-05-14T13:44:43Z Indexed on 2010/05/14 14:14 UTC
Read the original article Hit count: 182

Filed under:
|
|
|
|

I hava a JFrame with multiple JPanels of similar width aligned one below other. I use one of these JPanels to display a JTable which is the last JPanel of the lot. This JPanel has a JScrollpane as a child component. This is where I try to add my table dynamically. Initial height of this JScrollpane is set to 40.

I designed above template using Netbeans 6.8

Now I'm trying to add the table to the JPanel. When a button is pressed below code snippet is called. The class which includes this code extends javax.swing.JFrame class.

I am expecting below code would adjust table height according to the row count and display the table.

SearchTable = new JTable(RowData, DisplayNames) {

        @Override
        public boolean isCellEditable(int rowIndex, int vColIndex) {
            return false;
        }
    };

    // if row count is less than 10 then display all the rows without a scroll bar
    if (SearchTable.getRowCount() < 10) {
        pnl_tblpanel.setPreferredSize(new Dimension(625, SearchTable.getRowHeight() * (SearchTable.getRowCount() + 4)));
        scr_tblholder.setPreferredSize(new Dimension(625, SearchTable.getRowHeight() * (SearchTable.getRowCount() + 4)));
    } else {// if row count is more than 10 display first 10 rows and add a scroll bar
        pnl_tblpanel.setPreferredSize(new Dimension(625, SearchTable.getRowHeight() * (10 + 2)));
        scr_tblholder.setAutoscrolls(true);
    }


    //pnl_tblpanel.add(scr_tblholder);
    scr_tblholder.setViewportView(SearchTable);
    //pnl_tblpanel.repaint();
    pnl_tblpanel.validate();

    this.validate();
    //this.repaint();
    pnl_tblpanel.setVisible(true);
    this.pack();

The table displays, but the table height is not changed according to the row count. It stays its default value. I have been trying many combinations of validate and repaint but nothing worked. (More in desperation)

Can anyone shed some light on this Thank you

© Stack Overflow or respective owner

Related posts about java

Related posts about jtable