troubleshooting Question

getPreferredScrollableViewportSize() height is too much

Avatar of allelopath
allelopath asked on
1 Comment1 Solution1150 ViewsLast Modified:
My experience has been that the height from JTable.getPreferredScrollableViewportSize() is always way too much, as in the example below. Why is this?

jTable getPreferredSize: java.awt.Dimension[width=225,height=96]
jTable getPreferredScrollableViewportSize: java.awt.Dimension[width=450,height=400]

package jtableinjscrollpane;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

 * contains a table
public class MyPanel extends JPanel {

	private JScrollPane jScrollPane;
	private final JTable jTable;
	private DefaultTableModel defaultTableModel;

	private final String[] columnNameArray = {"One" , "Two",	"Three"};

	Object[][]  dataArray = {
			{ "row 1", "1b", ""},
			{ "row 2", "", "2c"},
			{ "row 3", "3b", ""},
			{ "row 4", "", "4c"},
			{ "row 5", "5b", ""},
			{ "row 6", "", "6c"},

	 * constructor
	 * @param name
	public MyPanel (String name) {


		defaultTableModel = new DefaultTableModel(dataArray, columnNameArray);

		jTable = new JTable (defaultTableModel);




	 * set layout of components
	public void setLayout() {

		this.setLayout(new BorderLayout());

		jScrollPane = new JScrollPane(jTable);

		final Border loweredbevel = BorderFactory.createLoweredBevelBorder();


		this.add(jScrollPane, BorderLayout.CENTER);
		System.out.println ("jTable getPreferredSize: " + jTable.getPreferredSize());
		System.out.println ("jTable getPreferredScrollableViewportSize: " + jTable.getPreferredScrollableViewportSize());

	 * @param args
	public static void main(String[] args) {

			UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
		catch (final Exception e) {

		final MyPanel jPanel = new MyPanel("My Panel");
		final JFrame frame = new JFrame();
		frame.add(jPanel, BorderLayout.CENTER);
		frame.setTitle("My Panel");
		//		frame.setPreferredSize(new Dimension(500, 250));
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {

		frame.setLocation(300, 200);


Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros