zProgrammer
zProgrammer

Reputation: 739

Java- make a scrollpane's size change to size of table?

right now I have a jtable wrapped inside of a jscrollpane...

Jtable table = new JTable(10,10);
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setBounds(50, 50, 50, 50);

(for example)

I was wondering how you can avoid having to set the size of the scrollpane? I can't get the scrollpane/table to show up without calling setBounds. I would like the scrollpane to be whatever size the table is (but not bigger than some maximum value) so there isn't a big gray rectangle under the table because the scrollpane is a set size. How to avoid setting a set size?

Upvotes: 0

Views: 759

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

As Hovercraft Full Of Eels has suggested, the simplest solution would be to set the container's layout manager to something like BorderLayout and add the scroll pane to it...

The layout manager will automatically take care of positioning and sizing the scrollpane for you.

Take a look at Laying Out Components Within a Container for more details

Upvotes: 1

Related Questions