Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shrink JScroll Pane to same Height as JTable

I currently have JTables nested in JScrollPanes like so:

enter image description here

My problem is that the number of rows in each table is variable when the table is created. What I want to do is make the JScrollpane smaller if the table is too short, but I want to keep it at a set size if the table is too long.

How can I accomplish this?

like image 588
Alex Bliskovsky Avatar asked Jun 29 '11 16:06

Alex Bliskovsky


1 Answers

Dimension d = table.getPreferredSize();
scrollPane.setPreferredSize(
    new Dimension(d.width,table.getRowHeight()*rows+1));

Where rows represents the number of rows of data, or the limit.

like image 167
Andrew Thompson Avatar answered Nov 08 '22 12:11

Andrew Thompson