Java jtable column header custom renderer examples
Here's an example of how to create a custom renderer for the column headers of a JTable
in Java:
import java.awt.Component; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.JTableHeader; import javax.swing.table.TableColumnModel; import javax.swing.table.TableModel; public class CustomTableHeaderRendererExample extends JFrame { public CustomTableHeaderRendererExample() { super("Custom Table Header Renderer Example"); // Create the table model Object[][] data = { {"John Doe", "25", "Male"}, {"Jane Smith", "30", "Female"}, {"Bob Johnson", "40", "Male"} }; String[] columnNames = {"Name", "Age", "Gender"}; TableModel tableModel = new CustomTableModel(data, columnNames); // Create the table JTable table = new JTable(tableModel); // Set the column header renderer TableColumnModel columnModel = table.getColumnModel(); for (int i = 0; i < columnModel.getColumnCount(); i++) { columnModel.getColumn(i).setHeaderRenderer(new CustomTableHeaderRenderer()); } // Add the table to a scroll pane JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); // Set the size, location and visibility of the frame pack(); setLocationRelativeTo(null); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new CustomTableHeaderRendererExample(); } // Custom table model private static class CustomTableModel extends javax.swing.table.DefaultTableModel { public CustomTableModel(Object[][] data, String[] columnNames) { super(data, columnNames); } // Disable cell editing @Override public boolean isCellEditable(int row, int column) { return false; } } // Custom table header renderer private static class CustomTableHeaderRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); // Set the font, background and foreground color of the header setFont(new Font("Arial", Font.BOLD, 14)); setBackground(table.getTableHeader().getBackground()); setForeground(table.getTableHeader().getForeground()); return this; } } }
In this example, we create a custom CustomTableHeaderRenderer
class that extends DefaultTableCellRenderer
and overrides the getTableCellRendererComponent()
method. This method is called by the JTableHeader
to render the column headers.
In the getTableCellRendererComponent()
method, we set the font, background color and foreground color of the header using the setFont()
, setBackground()
and setForeground()
methods. We also call the super.getTableCellRendererComponent()
method to perform any default rendering.
In the CustomTableModel
class, we extend DefaultTableModel
and override the isCellEditable()
method to disable cell editing.
In the CustomTableHeaderRendererExample
class, we create a JTable
with a custom table model and set the column header renderer to a new instance of CustomTableHeaderRenderer
. We then add the JTable
to a JScrollPane
and add the scroll pane to the frame.
When you run this example, you'll see a JTable
with column headers that have a custom font and color.