Solved

JTable & scroll pane to stretch to fill the panel

Posted on 2010-09-17
2
2,324 Views
Last Modified: 2013-11-23
How do I get the table & scroll pane to stretch to fill the panel as it is resized?
(Resized using the mouse).
package tableStretch;



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;



/**

 * JPanel with JTable

 */

public class MyPanel extends JPanel {



	private JTable jTable;



	private DefaultTableModel defaultTableModel;



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



	private Object[][]  dataArray = { 

			{ "", "", ""},

			{ "", "", ""},

			{ "", "", ""},

			{ "", "", ""},

	};



	/**

	 * constructor

	 * 

	 */

	public MyPanel () {



		super();



		defaultTableModel = new DefaultTableModel(dataArray, columnNameArray);



		jTable = new JTable (defaultTableModel); 



		setLayout();



		setListeners();



	}



	/**

	 * set layout of components

	 */

	public void setLayout() {



		jTable.setFillsViewportHeight(true);



		JScrollPane jScrollPane = new JScrollPane(jTable);

		jScrollPane.getViewport().setPreferredSize(jTable.getPreferredSize());



		Border loweredbevel = BorderFactory.createLoweredBevelBorder();

		jScrollPane.setBorder(loweredbevel);



		jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);



		this.add(jScrollPane, BorderLayout.CENTER);

	}



	/**

	 * set listeners

	 */

	public void setListeners() {



	}



	/**

	 * @return defaultTableModel

	 */

	public DefaultTableModel getDefaultTableModel() {

		return defaultTableModel;

	}





	/**

	 * 

	 * @param defaultTableModel

	 */

	public void setDefaultTableModel(DefaultTableModel defaultTableModel) {



		this.defaultTableModel = defaultTableModel;

		jTable.setModel(defaultTableModel);



	}



	/**

	 * 

	 * @param args

	 */

	public static void main(String[] args) {



		try{

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		}

		catch (Exception e){

			e.printStackTrace();

		}



		final MyPanel jPanel = new MyPanel();

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



	}



}

Open in new window

0
Comment
Question by:allelopath
  • 2
2 Comments
 
LVL 4

Accepted Solution

by:
jlar310 earned 500 total points
ID: 33703432
in setLayout() add

this.setLayout(new BorderLayout());

You need to give each panel a layout manager before the contraints in the add() calls have any meaning.
0
 
LVL 4

Expert Comment

by:jlar310
ID: 33703474
A little more info: If you don't call setLayout(someLayoutManager), the default layout for a container is FlowLayout.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 23
Java Message handling in Service Layer 3 58
backtracking recursion  code 19 54
Java Options in Zimbra Server 2 46
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now