In this article JButton in Swing we give the information about It is used to perform various actions in the user interface. Using JButton you can associate events with the button, so that when the user clicks the button, a particular action or event is performed.

JButton in Swing:

JButton is an important component in Swing which is used to create a clickable button. It is used to perform various actions in the user interface. Using JButton you can associate events with the button, so that when the user clicks the button, a particular action or event is performed.

Features of JButton:

  1. Button with text:

You can display text on the button, which tells what the button does.

  1. Button with image:

You can also display an image or icon on the JButton.

  1. Both text and image:

You can also display both text and image together in JButton.

  1. Event Handling:

When a user clicks on a JButton, an event handler can be attached to it so that a particular action can be performed.

Constructors of JButton :

  1. JButton():

This creates an empty button, with neither text nor image.

  1. JButton(String text):

This creates a button that contains a text.

  1. JButton(Icon icon):

This creates a button that contains an image (Icon).

  1. JButton(String text, Icon icon):

This creates a button that contains both text and an image.

Example of JButton:

import javax.swing.*;

import java.awt.event.*;

public class JButtonExample {

    public static void main(String[] args) {

        // Creating a JFrame

        JFrame frame = new JFrame(“JButton Example”);

                // JButton with text

        JButton button = new JButton(“Click Hear”);

        // Event handling for Button click

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                System.out.println(“Clicking on Button!”);

            }

        });

        // Join button in JFrame

        frame.add(button);

                // Setting in Frame

        frame.setSize(300, 200);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }

}

In this example:

  • A JButton is created with the text “Click here”.
  • An ActionListener handles the event, so that when the button is clicked, the message “Button was clicked!” appears in the console.

Methods of JButton:

  1. setText(String text):

Sets the text to be displayed on the button.

  1. getText():

Gets the text set on the button.

  1. setIcon(Icon icon):

Sets the image (icon) on the button.

  1. setEnabled(boolean enabled):

Used to enable or disable the button. When the button is disabled, it cannot be clicked.

  1. addActionListener(ActionListener listener):

Adds an ActionListener to perform an event or action when the button is clicked.

  1. setToolTipText(String text):

Used to show a small tooltip text when the mouse is moved over the button.

Event Handling in JButton:

Event handling on a JButton means that when the user clicks the button, a certain process (such as submitting a form or processing data) is performed. For this, you can use an ActionListener.

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

/The action to be performed during the event

System.out.println(“Button clicked!”);

}

});

In this code, when the button is clicked, the code inside the actionPerformed method is executed.

Benefits of JButton:

  1. Clickable Interface:

JButton provides a simple way for the user to interact with the application through interactive buttons.

  1. Event-Driven Programming:

Event-driven programming is done through JButton, which allows reactions to user actions.

  1. Customization:

You can customize JButton as per your requirement, such as adding text, image, tooltip, etc.

  1. Lightweight and easy to use:

JButton is a lightweight and easy to use Swing component, which makes the interface of Java application simple and attractive.

Summary:

  • JButton is an important Swing component that represents a clickable button in a GUI.
  • You can add text, images, or both to it and combine it with event handling.
  • Using JButton you can create interactive and responsive applications that perform tasks based on user actions.

The use of JButton in Swing is extremely important as it is the main source of adding interactivity to any GUI application.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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