views:

308

answers:

2

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

+1  A: 

Why did you create a panel just to hold a scroll pane. Add the scrollpane directly to the main panel. Set the preferred size of the scroll pane to the size you want. Then use

mainPanel.revalidate();

Or maybe you need to use frame.pack() to reset the size of the search dialog.

Anyway, assuming you use the proper layout manager on the main panel everything should work.

camickr
I tried what you instructed but it doesnt work. I am using netbeans 6.8 and it uses javax.swing.GroupLayout, which is the default layout in netbeansany suggestions??Thank you
Niroshan
I know nothing about GroupLayout. It uses so many constraints which makes it confusing to use. If you are adding component dynamically to a GroupLayout you are responsible for making sure all the constraints are set properly. I alway write layout code myself so I understand how it works and I know I can change it dynamically easily if required. Don't know if it will work, but I suggest you "don't" add the table dynamically but add it at design time. Then you can try just using table.setModel(...) to update the table.
camickr