- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 to change JTable's header font in Java
To change the table header font, you need to first get the header -
JTableHeader tableHeader = table.getTableHeader();
Now, use the Font class to set the new font. Here, we have set the font face to be Verdana, style as PLAIN and font size as 14 -
Font headerFont = new Font("Verdana", Font.PLAIN, 14);
Now, set this font to table header -
tableHeader.setFont(headerFont);
The following is an example to change the header font -
Example
package my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.JTableHeader; public class SwingDemo { public static void main(String[] argv) throws Exception { Integer[][] marks = { { 70, 66, 76, 89, 67, 98 }, { 67, 89, 64, 78, 59, 78 }, { 68, 87, 71, 65, 87, 86 }, { 80, 56, 89, 98, 59, 56 }, { 75, 95, 90, 73, 57, 79 }, { 69, 49, 56, 78, 76, 77 } }; String col[] = { "S1", "S2", "S3", "S4", "S5", "S6"}; JTable table = new JTable(marks, col); Font font = new Font("Verdana", Font.PLAIN, 12); table.setFont(font); table.setRowHeight(30); table.setBackground(Color.blue); table.setForeground(Color.white); JTableHeader tableHeader = table.getTableHeader(); tableHeader.setBackground(Color.black); tableHeader.setForeground(Color.white); Font headerFont = new Font("Verdana", Font.PLAIN, 14); tableHeader.setFont(headerFont); JFrame frame = new JFrame(); frame.setSize(600, 400); frame.add(new JScrollPane(table)); frame.setVisible(true); } }
Output
- Related Articles
- How to change a Tkinter widget's font style without knowing the widget's font family/size?
- How to change JLabel font in Java
- How to change JButton font dynamically in Java?
- How can we show/hide the table header of a JTable in Java?
- How to change each column width of a JTable in Java?
- Can we hide the table header from a JTable in Java?
- How to change header background color of a table in Java
- How to change text font for JLabel with HTML in Java?
- How to change font size with HTML in Java Swing JEditorPane?
- How to change font size in HTML?
- How to change text font in HTML?
- How to change font size in ttk.Button?
- How to change a table's fontsize with matplotlib.pyplot?
- How to change an element's class with JavaScript?
- Java Program to change the background color of rows in a JTable

Advertisements