Table of contents
1.
Introduction
1.1.
Class Declaration
1.2.
Constructors in JButton
1.3.
 Methods of AbstractButton Class
2.
How to Create JButton in Java
3.
JButton Example with ActionListener
3.1.
JButton Example
4.
Frequently Asked Questions
4.1.
What is a JButton?
4.2.
What are the AbstractButton class's subclasses?
4.3.
What is the difference between JLabel and JButton?
4.4.
Is JButton an interface?
5.
Conclusion
Last Updated: Mar 27, 2024

JButton In Java

Author Kanak Rana
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The JButton class is used to build a platform-independent labeled button. When the button is pressed, the application performs some action. It is inherited from the AbstractButton class.

Class Declaration

Following is the declaration for javax. swing.JButton class -

public class JButton extends Abstract Button implements Accessible


Constructors in JButton

Constructor Description

JButton()

No text and Icon.

JButton(String string)

Button with the specified text.

JButton(Icon icon)

Button with the specified icon object.

JButton(Action a)

It creates a button where properties were taken from the Action supplied.

JButton(String text, Icon icon)

Creates a button with an initial text and an icon


 Methods of AbstractButton Class

Methods Description

void setText(String s)

Set specified text on the button

String getText()

Return the text of the button.

void setEnabled(boolean b)

It enable or disable the button.

void setIcon(Icon b)

It set the specified Icon on the button.

Icon getIcon()

It get the Icon of the button.

void setMnemonic(int a)

Set the mnemonic on the button.

void addActionListener(ActionListener a)

Add the action listener to this object.

Also read about swing component in java

How to Create JButton in Java

import javax.swing.*;    
public class Jbutton_Class{  
public static void main(String[] args) {  
    JFrame f=new JFrame("JButton ");  
    JButton b=new JButton("Click Here");  
    b.setBounds(85,110,105,50);  
    f.add(b);  
    f.setSize(300,300);  
    f.setLayout(null);  
    f.setVisible(true);   
}  
}  
You can also try this code with Online Java Compiler
Run Code

Output

JButton Example with ActionListener

import java.awt.event.*;  
import javax.swing.*;    
public class Jbutton_Class{  
public static void main(String[] args) {  
    JFrame f=new JFrame("JButton");  
    final JTextField tf=new JTextField();  
    tf.setBounds(60,60, 200,40);  
    JButton b=new JButton("Click Here");  
    b.setBounds(60,110,105,30);  
    b.addActionListener(new ActionListener(){  
    	public void actionPerformed(ActionEvent e){  
            tf.setText("Welcome to Coding Ninjas!!!! Ninjas");  
        }
    });
    f.add(b);f.add(tf);  
    f.setSize(500,500);  
    f.setLayout(null);  
    f.setVisible(true);   
}  
}  
You can also try this code with Online Java Compiler
Run Code

Output

JButton Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class MainClass {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
 
   public MainClass(){
      prepareGUI();
   }
   public static void main(String[] args){
      MainClass  swingControlDemo = new MainClass();      
      swingControlDemo.showButtonDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java JButton Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(380,140);
 
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private static ImageIcon createImageIcon(String path, String description) {
      java.net.URL imgURL = MainClass.class.getResource(path);
      if (imgURL != null) {
         return new ImageIcon(imgURL, description);
      } else {            
         System.err.println("Couldn't find file: " + path);
         return null;
      }
   }   
   private void showButtonDemo(){
      headerLabel.setText("Control in action: Button"); 
 
      //resources folder should be inside the SWING folder.
      ImageIcon icon = createImageIcon("/resources/java_icon.png","Java");

      JButton okButton = new JButton("OK");        
      JButton javaButton = new JButton("Submit", icon);
      JButton cancelButton = new JButton("Cancel", icon);
      cancelButton.setHorizontalTextPosition(SwingConstants.LEFT);   

      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText("Ok Button clicked.");
         }          
      });
      javaButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText("Submit Button clicked.");
         }
      });
      cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText("Cancel Button clicked.");
         }
      });
      controlPanel.add(okButton);
      controlPanel.add(javaButton);
      controlPanel.add(cancelButton);       
 
      mainFrame.setVisible(true);  
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

Must Read Type Conversion in Java

Frequently Asked Questions

What is a JButton?

The JButton class is a push-button implementation. When pressed, this component generates an event and has a label. It can also have an image attached to it.

What are the AbstractButton class's subclasses?

JButton, JToggleButton, JCheckBox, and JRadioButton are the four types of buttons defined by Swing. All of these classes are subclasses of AbstractButton, which extends JComponent. As a result, all buttons have a set of characteristics in common.

What is the difference between JLabel and JButton?

A JButton is a push button that performs a specific action. JLabel is a component that displays a short text string, an image, or both at the same time.

Is JButton an interface?

The JButton class is used to generate a push-button control that can trigger an ActionEvent when pressed. The ActionListener interface should be implemented in order to handle a button click event. JButton is a container-addable component that extends the JComponent class.

Conclusion

In this blog, we learned about what is JButton in Java and the implementation, methods, and constructor of Jbutton in Java.

You can check out more information about other classes below:

Why is Java Platform Independent 

Super Keyword In Java

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja! 🥷

Live masterclass