Set constant size of JScrollPane

Joel Christophel picture Joel Christophel · Jan 5, 2013 · Viewed 10.5k times · Source

I have a JTabbedPane with a Border Layout.

enter image description here

Here's the code I'm using to add the components:

add(columnNames, BorderLayout.NORTH);
add(scroll, BorderLayout.CENTER);
add(useCtrl, BorderLayout.SOUTH);
setVisible(true);

Question: Notice the excess whitespace to the right inside the JScrollPane. I don't want that there. I would like for the JScrollPane not to change size at all when changing the size of the JFrame. I have tried setSize() and setPreferredSize(), but the size of the JScrollPane always changes. I've tried using GridLayout, but I get the same result.

Answer

StanislavL picture StanislavL · Jan 5, 2013

Place the JScrollPane in a JPanel with another layout. (e.g. BoxLayout or GridBagLayout). And add the JPanel to the center.