Thursday, January 19, 2012

JTable size policy

Here's the deal. During layout, the viewport is queried for its preferred size. The viewport returns one of the following:
  1. zero width, zero height
  2. its single child's preferred size if the child's not a Scrollable
  3. its single child's preferredScrollableViewportSize if the child's a Scrollable
Since JTable is a Scrollable(i.e., it implements the Scrollable interface), the viewport returns 3) above. JTable's initial preferredScrollableViewportSize's height is 400. Thus, you get a height of 400 even if you set the table's preferred size's height to 17. Call setPreferredScrollableViewportSize() to change the preferred viewport size.