Sort JTable on a Particular Column in Java



In this article, we will learn to sort a JTable on a particular column in Java. Data sorting in a JTable is something that most Java Swing applications require. If the users can sort table data by clicking on column headers, it improves the experience greatly.

What is a JTable?

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable component can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns.

Syntax

The following is the syntax:

JTable table = new JTable(data, columnNames);

A JTable can generate different types of listeners:

  • TableModelListener
  • TableColumnModelListener
  • ListSelectionListener
  • CellEditorListener
  • RowSorterListener

The following are the ways to sort JTable on a particular column:

Using setAutoCreateRowSorter()

The first approach is to use the setAutoCreateRowSorter() method provided by the JTable class. This method creates a TableRowSorter for the table and enables sorting on all columns. We can sort a JTable in a particular column by using the method setAutoCreateRowSorter() and set to true of JTable class.

Syntax

The following is the syntax:

table.setAutoCreateRowSorter(true);

Example

The following is an example to sort a JTable on a particular column using the setAutoCreateRowSorter() method:

import java.awt.*;
import javax.swing.*;
public final class JTableSorterTest extends JFrame {
   private JTable table;
   private JScrollPane scrollPane;
   public JTableSorterTest() {
      setTitle("JTableHeaderHide Test");
      String[] columnNames = {"Name", "Age", "City"};
      Object[][] data = {{"Raja", "35", "Hyderabad"}, {"Adithya", "25", "Chennai"}, {"Vineet", "23",       "Mumbai"}, {"Archana", "32", "Pune"}, {"Krishna", "30", "Kolkata"}};
      table = new JTable(data, columnNames);
      scrollPane= new JScrollPane(table);
      table.setAutoCreateRowSorter(true); // sorting of the rows on a particular column
      add(scrollPane, BorderLayout.CENTER);
      setSize(375, 250);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] args) {
      new JTableSorterTest();
   }
}

Output

Using TableRowSorter

When we require more control over the way items are sorted, particularly when we need to change the way a single column is sorted. In such cases, on your end, it is best practice to create a TableRowSorter manually.

Syntax

The following is the syntax:

TableRowSorter sorter = new TableRowSorter<>(table.getModel());
table.setRowSorter(sorter);

Example

The following is an example to sort a JTable on a particular column using TableRowSorter:

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

public class JTableSorterManual extends JFrame {
    private JTable table;
    
    public JTableSorterManual() {
        setTitle("JTable Sorting with TableRowSorter");
        String[] columnNames = {"Name", "Age", "City"};
        Object[][] data = {
            {"Raja", 35, "Hyderabad"}, 
            {"Adithya", 25, "Chennai"}, 
            {"Vineet", 23, "Mumbai"}, 
            {"Archana", 32, "Pune"}, 
            {"Krishna", 30, "Kolkata"}
        };
        
        table = new JTable(data, columnNames);
        
        // Create a TableRowSorter
        TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
        table.setRowSorter(sorter);
        
        // Add to scroll pane and display
        add(new JScrollPane(table), BorderLayout.CENTER);
        setSize(375, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JTableSorterManual());
    }
}

Output

Updated on: 2025-04-14T19:20:26+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements