Share on Google+Share on Google+

Packing a JTable Component

In this section you will learn about the packing of a JTable by adjusting it in the center.

Packing a JTable Component


In this section you will learn about the packing of  a JTable by adjusting it in the center. 

Description of program:

This program helps you in packing a JTable component. For this you will need a JTable having the data and columns with column headers. To pack the JTable, you will need to apply the getPerferredScrollableVeiwportSize method that returns getPreferredSize.  After doing entire process, you will get the packed JTable that  always be  in center.

Description of code:

This method returns a dimension containing the size of the viewport required for displaying visibleRowCount rows in JTable.

This method returns the preferred size of specified component.

Here is the code of program:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class PackJTableComponent{
  JTable table;
  public static void main(String[] args) {
  new PackJTableComponent();

  public PackJTableComponent(){
  JFrame frame = new JFrame("Packing a JTable Component!");
  JPanel panel = new JPanel();
  String data[][] {{"Vinod","Computer","3"},
  String col [] {"Name","Course","Year"};
  DefaultTableModel model = new DefaultTableModel(data,col);
  table = new JTable(model){
  public Dimension getPreferredScrollableViewportSize() {
  return getPreferredSize();
  JTableHeader header = table.getTableHeader();
  JScrollPane pane = new JScrollPane(table);

Download this example.

Output of program:


Posted on: April 14, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Packing a JTable Component  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image