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
Introduction
JButton is one of the most commonly used components in Java Swing. It represents a clickable button that can perform specific actions when pressed.
Buttons are used to trigger events or commands in a graphical user interface (GUI).
Features of JButton
- Button with Text:
Displays text indicating the purpose of the button.
Example: new JButton(“Submit”); - Button with Image:
Displays an icon or image.
Example: new JButton(new ImageIcon(“icon.png”)); - Button with Text and Image:
Displays both text and image together.
Example: new JButton(“Save”, new ImageIcon(“save.png”)); - Event Handling:
You can attach an ActionListener to perform an action when the button is clicked.
Constructors of JButton
| Constructor | Description |
| JButton() | Creates an empty button (no text or image). |
| JButton(String text) | Creates a button with text. |
| JButton(Icon icon) | Creates a button with an image (icon). |
| JButton(String text, Icon icon) | Creates a button with both text and an image. |
Example of JButton
import javax.swing.*;
import java.awt.event.*;
public class JButtonExample {
public static void main(String[] args) {
// Create a JFrame
JFrame frame = new JFrame(“JButton Example”);
// Create a JButton with text
JButton button = new JButton(“Click Here”);
// Event handling for button click
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(“Button was clicked!”);
}
});
// Add button to frame
frame.add(button);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
button.setBounds(90, 70, 120, 30);
frame.setVisible(true);
}
}
Explanation
- A JFrame is created as the main window.
- A JButton is created with the text “Click Here”.
- An ActionListener is added to handle button clicks.
- When the button is clicked, “Button was clicked!” is printed in the console.
Common Methods of JButton
| Method | Description |
| setText(String text) | Sets text on the button. |
| getText() | Returns the current text of the button. |
| setIcon(Icon icon) | Sets an image/icon on the button. |
| setEnabled(boolean enabled) | Enables or disables the button. |
| addActionListener(ActionListener listener) | Adds an event listener to handle clicks. |
| setToolTipText(String text) | Shows a tooltip when the mouse hovers over the button. |
Event Handling in JButton
Event handling means responding to user actions — like a click.
This is done using ActionListener.
Example:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(“Button clicked!”);
}
});
When the button is clicked, the code inside the actionPerformed() method executes.
Benefits of JButton
- Clickable Interface:
Provides an interactive element for user input. - Event-Driven Programming:
Supports event handling for dynamic GUI behavior. - Customization:
You can easily add text, images, or tooltips. - Lightweight and Easy to Use:
JButton is a lightweight component that simplifies GUI creation.
Summary
| Point | Description |
| Component Name | JButton |
| Purpose | To create a clickable button in a GUI |
| Event Handling | Achieved using ActionListener |
| Customization | Text, image, tooltip, enable/disable |
| Importance | Adds interactivity and responsiveness to GUI apps |
Conclusion
JButton is a core component in Swing used to create interactive, event-driven applications.
By combining text, images, and event listeners, you can build intuitive and responsive Java GUIs easily.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining