Hey there! I’m a supplier in the Swing world, and today I wanna talk about how to customize the appearance of a table cell in Swing. It’s a super useful skill, especially if you’re looking to make your applications stand out. Swing
First things first, let’s understand why customizing table cell appearance is important. In many Swing – based applications, tables are a crucial part of presenting data. A plain, default – looking table can be boring and hard to read. By customizing the cell appearance, you can make the data more visually appealing and easier to understand.
Changing the Font and Color
One of the simplest ways to customize a table cell is by changing the font and color. In Swing, you can use the DefaultTableCellRenderer class to achieve this.
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
public class CustomTableRenderer extends 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);
}
c.setFont(new Font("Arial", Font.BOLD, 14));
c.setForeground(Color.BLUE);
return c;
}
}
In this code, we’re creating a custom renderer that extends DefaultTableCellRenderer. The getTableCellRendererComponent method is overridden. We’re setting different background colors for even and odd rows, and also changing the font to Arial, bold, size 14, and the text color to blue.
To use this renderer, you simply need to set it for the table:
JTable table = new JTable(data, columnNames);
CustomTableRenderer renderer = new CustomTableRenderer();
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setCellRenderer(renderer);
}
Using Icons in Table Cells
Icons can add a lot of visual interest to your table cells. Let’s say you want to display a checkmark icon for cells that meet a certain condition.
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
public class IconRenderer extends DefaultTableCellRenderer {
private ImageIcon checkIcon = new ImageIcon("checkmark.png");
@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 (value != null && value.toString().equals("Yes")) {
setIcon(checkIcon);
setText("");
} else {
setIcon(null);
setText(value != null? value.toString() : "");
}
return c;
}
}
In this example, if the cell value is "Yes", we display a checkmark icon instead of the text. And we use it in a similar way as the previous renderer:
JTable table = new JTable(data, columnNames);
IconRenderer iconRenderer = new IconRenderer();
table.getColumnModel().getColumn(0).setCellRenderer(iconRenderer);
Customizing Cell Borders
Borders can help to separate cells and make the table look more organized. You can use the BorderFactory class in Swing to create different types of borders.
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
public class BorderRenderer extends DefaultTableCellRenderer {
private Border border = BorderFactory.createLineBorder(Color.RED, 2);
@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);
c.setBorder(border);
return c;
}
}
This code creates a red border around each cell. And again, you can set this renderer for the table columns:
JTable table = new JTable(data, columnNames);
BorderRenderer borderRenderer = new BorderRenderer();
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setCellRenderer(borderRenderer);
}
Handling Different Data Types
Sometimes, you may have different data types in your table cells, like numbers, dates, or strings. You can customize the appearance based on the data type.
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.text.NumberFormat;
public class DataTypeRenderer extends DefaultTableCellRenderer {
private NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
@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 (value instanceof Number) {
setText(numberFormat.format(value));
}
return c;
}
}
In this example, if the cell value is a number, we format it as a currency. And you can set this renderer for the relevant columns:
JTable table = new JTable(data, columnNames);
DataTypeRenderer dataTypeRenderer = new DataTypeRenderer();
table.getColumnModel().getColumn(1).setCellRenderer(dataTypeRenderer);
Interactive Cell Customization
You can also make your table cells more interactive. For example, you can change the appearance when the user hovers over a cell.
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.MouseEvent;
public class InteractiveRenderer extends DefaultTableCellRenderer {
private Color hoverColor = Color.YELLOW;
private int hoverRow = -1;
private int hoverColumn = -1;
public InteractiveRenderer(JTable table) {
table.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseExited(MouseEvent e) {
hoverRow = -1;
hoverColumn = -1;
table.repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
int row = table.rowAtPoint(e.getPoint());
int column = table.columnAtPoint(e.getPoint());
if (row != hoverRow || column != hoverColumn) {
hoverRow = row;
hoverColumn = column;
table.repaint();
}
}
});
}
@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 == hoverRow && column == hoverColumn) {
c.setBackground(hoverColor);
} else {
c.setBackground(table.getBackground());
}
return c;
}
}
This code changes the background color of the cell when the mouse hovers over it. You can use it like this:
JTable table = new JTable(data, columnNames);
InteractiveRenderer interactiveRenderer = new InteractiveRenderer(table);
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setCellRenderer(interactiveRenderer);
}
Wrapping Up

Customizing the appearance of table cells in Swing can really enhance the user experience of your application. Whether it’s changing the font, adding icons, or making the cells interactive, there are many ways to make your tables look great.
Welded Link Chain If you’re interested in implementing these customizations in your projects or need more advice on Swing development, we’re here to help. We’ve got a team of experts who can assist you with all your Swing – related needs. Reach out to us to start a discussion about your requirements and how we can work together to create amazing Swing – based applications.
References
- "Java Swing: A Beginner’s Guide" by Herbert Schildt
- 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/