6

I am putting a JTable in a JScrollPane, but the table only contain several rows, which makes the rest of the space in the pane just empty.

Is there any way I can do to make the JTable fill the entire pane? say, put empty rows so that the pane will be filled.

Thanks

Leon
  • 8,151
  • 11
  • 45
  • 51

3 Answers3

10
table.setFillsViewportHeight( true );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • As far as I can tell, this will just fill the viewport with the table background color, not with empty rows - Guess it depends on what is required. Personally, I think I prefer the look of this. – Riaan Cornelius May 16 '11 at 15:43
  • Yes, thats the idea. It doesn't make sense (to me) to add extra rows because then your model is not accurate since it contains filler rows which don't contain any data. – camickr May 16 '11 at 16:28
3

The JTable method setPreferredScrollableViewportSize() is another way to manage screen real estate. There's an example here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

I'm not sure if this is the best way, but it should work fine:

int rows = jTable1.getRowCount();
    int rowHeight = jTable1.getRowHeight();
    int tableHeight = jTable1.getTableHeader().getHeight()+(rows*rowHeight);
    System.out.println("JScrollpane: "+jScrollPane1.getViewport().getHeight());
    while( tableHeight<jScrollPane1.getViewport().getHeight()){
        System.out.println("JTable: "+tableHeight);
        ((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{null,null,null,null});
        tableHeight+=rowHeight;
    }

Obviously, you'll need to change the model and the inserted data to whatever you're using.

Riaan Cornelius
  • 1,933
  • 1
  • 14
  • 17