- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we set the background/foreground color for individual column of a JTable in Java?
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. A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener, RowSorterListener interfaces. We can change the background and foreground color for each column of a JTable by customizing the DefaultTableCellRenderer class and it has only one method getTableCellRendererComponent() to implement it.
Example
import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class JTableColumnColorTest extends JFrame { private JTable table; private TableColumn tColumn; public JTableColumnColorTest() { setTitle("JTableColumnColor Test"); table = new JTable(10, 5); tColumn = table.getColumnModel().getColumn(2); tColumn.setCellRenderer(new ColumnColorRenderer(Color.lightGray, Color.red)); add(new JScrollPane(table), BorderLayout.CENTER); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String [] args) { new JTableColumnColorTest(); } } // Customize the code to set the background and foreground color for each column of a JTable class ColumnColorRenderer extends DefaultTableCellRenderer { Color backgroundColor, foregroundColor; public ColumnColorRenderer(Color backgroundColor, Color foregroundColor) { super(); this.backgroundColor = backgroundColor; this.foregroundColor = foregroundColor; } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); cell.setBackground(backgroundColor); cell.setForeground(foregroundColor); return cell; } }
Output
- Related Articles
- How can we set the foreground and background color to JComboBox items in Java?
- How can we change the background and foreground color of a JTooltip in Java?
- How can we set the background color to a JPanel in Java?
- How can we set a background color to JSplitPane in Java?
- Customize the tooltip font, color , background and foreground color in Java
- How to change JLabel background and foreground color in Java?
- Java Program to change the background color of rows in a JTable
- How can we sort a JTable on a particular column in Java?
- How to set foreground color for different words in a JTextPane
- How to set the background color of a column in a matplotlib table?
- How to set default background color for JTextPane in Java?
- How to set the color to alternate rows of JTable in Java?
- How can we filter a JTable in Java?
- How can we implement the word wrap JTableHeader of a JTable in Java?
- How can we show/hide the table header of a JTable in Java?

Advertisements