Implement Word Wrap in JTableHeader of a JTable in Java



In this article, we will learn to implement the word wrap JTableHeader of a JTable in Java. The Java Swing default JTableHeader does not support word wrapping. This is a problem if you have long column headers. We can implement this by customizing the DefaultTableModel class or the AbstractTableModel class.

JTableHeader

A JTableHeader is a subclass of the JComponent class. When we create a JTable object, the constructor creates a new JTableHeader object to manage the table component's header.

Syntax

The following is the syntax for JTableHeader Declaration:

public class JTableHeader extends JComponent

The JTableHeader object is associated with the table component's column model so that its UI delegate can drag the columns and render each column's header cell.

Methods

setTable()
setTable () is called to set the JTableHeader object for the table header component on a JTable.

Method Declaration:

public void setTable(JTable table)

getTable()

The getTable() method returns a reference to the JTableHeader object for the table header component.

Method Declaration:

public JTable getTable()

getDefaultRenderer

This method returns the default renderer to be used if a TableColumn does not define a headerRenderer.

Method Declaration:

public TableCellRenderer getDefaultRenderer()

Approach

The following are the steps for implementing the word wrap JTableHeader of a JTable in Java:

  • Creating the JFrame: We begin by creating a JFrame, which will be our app's main window for the Swing GUI.
  • Setting Up the JFrame: Windows title, size, close operation, as well as centering on the screen, will be set by the methods setTitle(), setSize(), setDefaultCloseOperation(), and setLocationRelativeTo().
  • Custom Column Model: A DefaultTableColumnModel is overridden to set a default header renderer for each column.
  • Creating the JTable: A JTable is initialized with custom columns and data models, set to fill the viewport height.
  • Adding Scroll Pane: The table is placed in a JScrollPane to display it scrollably.
  • Custom Table Model (DemoTableModel):
    • Takes column names and row count.
    • Generates integer data.
    • Supplies column headers and data types.
  • Running the App: main() creates and shows the GUI by instantiating the class.

Creating a Custom Table Column Model:

DefaultTableColumnModel tableColumnModel = new DefaultTableColumnModel() {
         public void addColumn(TableColumn column) {
            column.setHeaderRenderer(new JTableHeader().getDefaultRenderer());
            super.addColumn(column);
         }
      };

Example of Word Wrap JTableHeader

Below is an example of implementing the word wrap JTableHeader of a JTable in Java using the DefaultTableColumnModel class:

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class WordWrappingTableHeaderTest extends JFrame {
   private JTable table;
   public WordWrappingTableHeaderTest() {
      setTitle("WordWrappingTableHeader Test");
      DefaultTableColumnModel tableColumnModel = new DefaultTableColumnModel() {
         public void addColumn(TableColumn column) {
            column.setHeaderRenderer(new JTableHeader().getDefaultRenderer());
            super.addColumn(column);
         }
      };
      table = new JTable();
      table.setFillsViewportHeight(true);
      table.setColumnModel(tableColumnModel);
      table.setModel(new DemoTableModel(Arrays.asList("Word Wrapping TableHeader Test",
      "Word Wrapping TableHeader Test", "Word Wrapping TableHeader Test"), 20));
      add(new JScrollPane(table));
      setSize(450, 300);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setResizable(false);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   class DemoTableModel extends AbstractTableModel {
      private ArrayList wrappedColumnNames = new ArrayList();
      private int numRows;
      DemoTableModel(List columnNames, int numRows) {
         for (String name: columnNames)
            wrappedColumnNames.add("" + name + "");
            this.numRows = numRows;
      }
      public int getRowCount() {
         return numRows;
      }
      public int getColumnCount() {
         return wrappedColumnNames.size();
      }
      public Object getValueAt(int rowIndex, int columnIndex) {
         return Integer.valueOf(10000 + (rowIndex + 1)*(columnIndex + 1));
      }
      public String getColumnName(int column) {
         return wrappedColumnNames.get(column);
      }
      public Class getColumnClass(int columnIndex) {
         return Integer.class;
      }
   }
   public static void main(String[] args) {
     new WordWrappingTableHeaderTest();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-05T18:32:47+05:30

762 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements