views:

1073

answers:

2
+2  A: 

Yes, that's a known problem with JTables. By default it displays 20 rows, whatever the actual content.

If you want to change that you have to use code like follows:

static public void setTableHeight(JTable table, int rows)
{
    int width = table.getPreferredSize().width; 
    int height = rows * table.getRowHeight(); 
    table.setPreferredScrollableViewportSize(new Dimension(width, height));
}

Then just call setTableHeight(5); if you want 5 rows visible by default.

Note: this is Java, you may have to adapt it slightly to Groovy.

This is something I described in my blog last month (item #7).

jfpoilpret
+1  A: 

@jfpoilpret is correct in that the preferredScrollableViewpoet size needs to be managed. The groovy way would be to bind it to the preferred size of the table. So if this was added inside of the outer scrollPane element it would automatically track the size:

[serviceTable, groupsTable].each { table ->
    bind(source:table, sourceProperty:'preferredSize', 
         target:table, targetProperty:'preferredScrollableViewportSize',
         converter: { ps -> 
             [ps.width + 100, 
              (table.rowCount > 20 ? 20: table.rowCount) * table.rowHeight]
         })
}

We can also do neat things with the binding like adding a converter that will limit the height to 20 rows (or whatever you want it to be limited to) so we still get scrollbars when the table gets too long.

shemnon
Thanks, it's always good to learn something new :)
Miguel Ping