Java Swing GUI

How to Change JButton Text on Click

In this tutorial, we are going to see how to change JButton text on click. JButton is a subclass of AbstractButton class and it can be used to add platform-independent buttons to a Java Swing application. JButton can generate an ActionListener interface when the user clicks on a button, it can also generate MouseListener and KeyListener interfaces.

By default, we can create a JButton with a text and also change the text of a JButton, it will call the actionPerformed() method of the ActionListener interface and set an updated text in a button by calling setText(‘New text’) method of JButton class.


Java Program to Change JButton Text on Click:
import javax.swing.*;
import java.awt.event.*;

public class JButtonExample
  public static void main(String[] args) 
    JFrame frame = new JFrame("ActionListener Example");
    JButton btn = new JButton("Click here");
    //Change button text on click
    btn.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae) {


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *