Search code examples
javajtablejscrollpane

Default display area of JScrollPane in Java


I have the following problem: I have a JTable inside a JScrollPane, and the table has 20 rows. However I want the JScrollPane to only display 10 row at a time (thus one has to scroll down to see the rest of the rows). I am using GridBagLayout to position the JScrollPane.

Any suggestion? Thanks.


Solution

  • table.setPreferredScrollableViewportSize(...);
    

    You can get the preferred width by using the getPreferredSize() method of the table.

    You will need to caclulate the height to be the height of the table header plus the height of the rows which can be determine by using table.getRowHeight().