Change Each Column Width of a JTable in Java



In this article, we will learn to change each column width of a JTable in Java. JTable is Swing's strongest component for displaying and editing tabular information. Adjusting column widths appropriately to display content is among the most prevalent needs for customization.

JTable

A JTable is a subclass of JComponent for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. The DefaultTableModel class can extend AbstractTableModel and it can be used to add the rows and columns to a JTable dynamically.

Syntax

The following is the syntax for JTable initialization:

JTable table1 = new JTable();

The DefaultTableCellRenderer class can extend the JLabel class, and it can be used to add images, colored text, etc., inside the JTable cell. A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener, and RowSorterListener interfaces.

Changing Each Column Width

By default, the width of a JTable is fixed; we can also change the width of each column by using the setPreferredWidth() method of the JTable class.

Step-by-step process to change each column width of a JTable in Java:

Class Definition & Imports

The javax.swing.* provides Swing components (JTable, JScrollPane, etc.) while the java.awt.* provides AWT classes (Color, layout managers) and the class extends JFrame to create a window.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*; public class JTableTest extends JFrame { }

Instance Variables

The table variable of the JTable component shows the tabular data, the scrollPane is the JScrollPane that contains the table to provide scrolling, the model is the data model that contains the content of the table, and the cellRenderer determines how cells are rendered.

private JTable table;
private JScrollPane scrollPane;
private DefaultTableModel model;
private DefaultTableCellRenderer cellRenderer;

Constructor Setup

Sets the window title to "JTable Test", it uses the FlowLayout for the frame's content pane and creates a JScrollPane and a JTable, then puts the table inside the scroll pane.

public JTableTest() {
   setTitle("JTable Test");
   setLayout(new FlowLayout());
   scrollPane = new JScrollPane();
   JTable table = new JTable();
   scrollPane.setViewportView(table);

Table Model Configuration

Gets the table's default model and casts it to DefaultTableModel, adds five columns with descriptive headers, and adds sample data into it.

model = (DefaultTableModel)table.getModel();
model.addColumn("S.No");
model.addColumn("First Name");
model.addColumn("Last Name");
model.addColumn("Email");
model.addColumn("Contact");

Column Width Customization

Using the setPreferredWidth() method sets the first column width to 5 pixels, sets the fourth column width to 100 pixels, and other columns keep their default widths.

table.getColumnModel().getColumn(0).setPreferredWidth(5);
table.getColumnModel().getColumn(3).setPreferredWidth(100);

Main Method

The main method launches the application by creating an object of JTableTest.

public static void main(String[] args) {
   new JTableTest();
}

Example

Below is an example of changing the width of each column of a JTable in Java:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTableTest extends JFrame {
   private JTable table;
   private JScrollPane scrollPane;
   private DefaultTableModel model;
   private DefaultTableCellRenderer cellRenderer;
   public JTableTest() {
      setTitle("JTable Test");
      setLayout(new FlowLayout());
      scrollPane = new JScrollPane();
      JTable table = new JTable();
      scrollPane.setViewportView(table);
      model = (DefaultTableModel)table.getModel();
      model.addColumn("S.No");
      model.addColumn("First Name");
      model.addColumn("Last Name");
      model.addColumn("Email");
      model.addColumn("Contact");
      for(int i = 0;i < 4; i++) {
         model.addRow(new Object[0]);
         model.setValueAt(i+1, i, 0);
         model.setValueAt("Tutorials", i, 1);
         model.setValueAt("Point", i, 2);
         model.setValueAt("@tutorialspoint.com", i, 3);
         model.setValueAt("123456789", i, 4);
      }
      // set the column width for each column
      table.getColumnModel().getColumn(0).setPreferredWidth(5);
      table.getColumnModel().getColumn(3).setPreferredWidth(100);
      cellRenderer = new DefaultTableCellRenderer();
      cellRenderer.setHorizontalAlignment(JLabel.CENTER);
      table.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
      add(scrollPane);
      setSize(475, 250);
      setResizable(false);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] args) {
      new JTableTest();
   }
}

Output

Updated on: 2025-04-29T19:13:05+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements