- 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 to change JButton font dynamically in Java?
The following is an example to change JButton font dynamically:
Example
import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo extends JFrame { JButton button = new JButton("Change"); int fontSize = 10; public SwingDemo() { setSize(500, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); add(button); // changing font size dynamically on button click button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { button.setFont(new Font("Dialog", Font.PLAIN, ++fontSize)); button.revalidate(); } }); setVisible(true); } public static void main(String[] args) { new SwingDemo(); } }
Output
Click “Change” button above to change the font:
- Related Articles
- How can we change the JButton text dynamically in Java?\n
- How to change JLabel font in Java
- How to change JTable's header font in Java
- How to add Icon to JButton 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 add action listener to JButton in Java
- How to set action command to JButton in Java
- How to change font size in HTML?
- How to change text font in HTML?
- How to change font size in ttk.Button?
- How to add empty border to a JButton in Java?
- How can we apply different borders to JButton in Java?
- How to change variables in the .env file dynamically in Laravel?
- How to change font face of Webview in Android?

Advertisements