troubleshooting Question

getPreferredScrollableViewportSize() height is too much

Avatar of allelopath
allelopath asked on
Java
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) {

		super();

		defaultTableModel = new DefaultTableModel(dataArray, columnNameArray);

		jTable = new JTable (defaultTableModel);

		this.setName(name);

		setLayout();

	}

	/**
	 * set layout of components
	 */
	public void setLayout() {

		this.setLayout(new BorderLayout());
		
		jTable.setFillsViewportHeight(true);

		jScrollPane = new JScrollPane(jTable);
		jScrollPane.getViewport().setPreferredSize(jTable.getPreferredSize());

		final Border loweredbevel = BorderFactory.createLoweredBevelBorder();
		jScrollPane.setBorder(loweredbevel);

		jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		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) {

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

		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() {
			@Override
			public void windowClosing(WindowEvent e) {
			}
		});

		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.setLocation(300, 200);
		frame.pack();
		frame.setVisible(true);

	}


}
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