tags:

views:

22

answers:

1

Hi everyone,

Do you know any way of making a JTable resize itself up to a maximum predefined height and then provide scroll bars?

A: 

Ok. I found a way. Not the cleanest but it works.

If the size of the JTable is determined by another container (in my case it was a JPanel) the idea is to calculate the panel height based on JTable.getRowCount().

so it was a simple case of setting something like this:

  if (table.getRowCount() > 0 && table.getRowCount()<5){
    lineJPanel.setMaximumSize(new Dimension(32500, 70+table.getRowCount()*table.getRowHeight()));
  }else{
    lineJPanel.setMaximumSize(new Dimension(32500, 200));
  }
Bogdan