[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

JTable & scroll pane to stretch to fill the panel

Posted on 2010-09-17
2
Medium Priority
?
2,545 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 4

Accepted Solution

by:
jlar310 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

650 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