In the realm of Java GUI programming, Swing stands out as a powerful and versatile toolkit. One of the most useful components in Swing is the JTable, which allows developers to display and manipulate tabular data in a user – friendly way. As a Swing supplier, I’ve had the privilege of working with numerous clients to implement JTable in their applications, and I’m excited to share my insights on how to use it effectively. Swing

Understanding the Basics of JTable
A JTable is a visual component that presents data in a two – dimensional table format, similar to a spreadsheet. It consists of rows and columns, where each cell can hold a value. To create a basic JTable, you first need to define the data model.
The simplest way to create a JTable is by using a DefaultTableModel. Here’s a basic example:
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class BasicJTableExample {
public static void main(String[] args) {
// Define column names
String[] columnNames = {"Name", "Age", "City"};
// Define data
Object[][] data = {
{"John", 25, "New York"},
{"Jane", 30, "Los Angeles"},
{"Bob", 22, "Chicago"}
};
// Create a DefaultTableModel
DefaultTableModel model = new DefaultTableModel(data, columnNames);
// Create a JTable with the model
JTable table = new JTable(model);
// Create a JFrame to hold the table
JFrame frame = new JFrame("Basic JTable Example");
frame.add(table.getTableHeader(), java.awt.BorderLayout.NORTH);
frame.add(table, java.awt.BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In this example, we first define an array of column names and a two – dimensional array of data. We then create a DefaultTableModel using these arrays. Finally, we create a JTable with the model and add it to a JFrame for display.
Customizing the Appearance of JTable
While the basic JTable looks functional, you may want to customize its appearance to match the overall look and feel of your application. You can customize various aspects such as cell colors, fonts, and borders.
To change the cell color, you can use a custom TableCellRenderer. Here’s an example:
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.Component;
public class CustomCellColorExample {
public static void main(String[] args) {
String[] columnNames = {"Name", "Age", "City"};
Object[][] data = {
{"John", 25, "New York"},
{"Jane", 30, "Los Angeles"},
{"Bob", 22, "Chicago"}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
// Create a custom cell renderer
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (row % 2 == 0) {
c.setBackground(Color.LIGHT_GRAY);
} else {
c.setBackground(Color.WHITE);
}
return c;
}
};
// Apply the renderer to all columns
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setCellRenderer(renderer);
}
JFrame frame = new JFrame("Custom Cell Color Example");
frame.add(table.getTableHeader(), java.awt.BorderLayout.NORTH);
frame.add(table, java.awt.BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In this code, we create a custom DefaultTableCellRenderer and override the getTableCellRendererComponent method. Inside this method, we set the background color of every other row to light gray. We then apply this renderer to all columns of the table.
Handling User Interaction
JTable allows users to interact with the data in various ways, such as selecting rows, editing cells, and sorting columns.
Row Selection
To handle row selection, you can add a ListSelectionListener to the table’s selection model. Here’s an example:
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class RowSelectionExample {
public static void main(String[] args) {
String[] columnNames = {"Name", "Age", "City"};
Object[][] data = {
{"John", 25, "New York"},
{"Jane", 30, "Los Angeles"},
{"Bob", 22, "Chicago"}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
System.out.println("Selected row: " + selectedRow);
for (int i = 0; i < table.getColumnCount(); i++) {
System.out.println(table.getColumnName(i) + ": " + table.getValueAt(selectedRow, i));
}
}
}
}
});
JFrame frame = new JFrame("Row Selection Example");
frame.add(table.getTableHeader(), java.awt.BorderLayout.NORTH);
frame.add(table, java.awt.BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In this example, we add a ListSelectionListener to the table’s selection model. When the user selects a row, the valueChanged method is called, and we print out the selected row and its data.
Cell Editing
By default, cells in a JTable are editable. However, you can control the editing behavior by implementing a custom TableModel or by using the isCellEditable method of the TableModel.
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
class CustomTableModel extends DefaultTableModel {
public CustomTableModel(Object[][] data, String[] columnNames) {
super(data, columnNames);
}
@Override
public boolean isCellEditable(int row, int column) {
// Make only the "Age" column editable
return column == 1;
}
}
public class CellEditingExample {
public static void main(String[] args) {
String[] columnNames = {"Name", "Age", "City"};
Object[][] data = {
{"John", 25, "New York"},
{"Jane", 30, "Los Angeles"},
{"Bob", 22, "Chicago"}
};
CustomTableModel model = new CustomTableModel(data, columnNames);
JTable table = new JTable(model);
JFrame frame = new JFrame("Cell Editing Example");
frame.add(table.getTableHeader(), java.awt.BorderLayout.NORTH);
frame.add(table, java.awt.BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In this code, we create a custom TableModel that overrides the isCellEditable method. We make only the "Age" column editable.
Column Sorting
JTable provides built – in support for column sorting. You can enable sorting by calling the setAutoCreateRowSorter method on the table.
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class ColumnSortingExample {
public static void main(String[] args) {
String[] columnNames = {"Name", "Age", "City"};
Object[][] data = {
{"John", 25, "New York"},
{"Jane", 30, "Los Angeles"},
{"Bob", 22, "Chicago"}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
table.setAutoCreateRowSorter(true);
JFrame frame = new JFrame("Column Sorting Example");
frame.add(table.getTableHeader(), java.awt.BorderLayout.NORTH);
frame.add(table, java.awt.BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
This code enables column sorting. Users can click on the column headers to sort the data in ascending or descending order.
Working with Large Datasets
When dealing with large datasets, you may encounter performance issues if you load all the data into the DefaultTableModel at once. A better approach is to use a custom TableModel that loads data on – demand.
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
class LargeDatasetTableModel extends AbstractTableModel {
private static final int ROW_COUNT = 10000;
private static final int COLUMN_COUNT = 5;
@Override
public int getRowCount() {
return ROW_COUNT;
}
@Override
public int getColumnCount() {
return COLUMN_COUNT;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// Here you can implement logic to load data on - demand
return "Row " + rowIndex + ", Column " + columnIndex;
}
@Override
public String getColumnName(int column) {
return "Column " + column;
}
}
public class LargeDatasetExample {
public static void main(String[] args) {
LargeDatasetTableModel model = new LargeDatasetTableModel();
JTable table = new JTable(model);
JFrame frame = new JFrame("Large Dataset Example");
frame.add(table.getTableHeader(), java.awt.BorderLayout.NORTH);
frame.add(table, java.awt.BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In this example, we create a custom TableModel that extends AbstractTableModel. The getValueAt method can be implemented to load data from a database or other data source on – demand, which helps to improve performance when dealing with large datasets.
Conclusion

JTable is a powerful and flexible component in the Swing toolkit. By understanding its basic usage, customizing its appearance, handling user interaction, and working with large datasets, you can create sophisticated and user – friendly applications.
Clothes Rack As a Swing supplier, we have extensive experience in implementing JTable and other Swing components in various projects. Whether you need a simple table for displaying data or a complex, customized table with advanced features, we can provide the solutions you need. If you’re interested in learning more about our Swing – related services or have a project in mind, we invite you to contact us for a procurement discussion.
References
- "Java Swing" by Cay S. Horstmann
- "Effective Java" by Joshua Bloch
- Oracle Java Documentation on Swing components
Pujiang Shenli Chain Co., Ltd.
We’re well-known as one of the most experienced swing suppliers in China, featured by quality products and low price. Please feel free to buy discount swing made in China here from our factory. Contact us for more details.
Address: No. 18, Zaifeng Road, Pujiang County, Zhejiang Province
E-mail: Chen@shenlichain.com
WebSite: https://www.chainshenli.com/