Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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:

Advertisements