JButton Java Swing Example Tutorial With Code

Robert Jul 13, 2026

JButton is a fundamental component in Java Swing, used to create buttons that allow users to interact with button-labeled actions in your application. In this comprehensive guide, we'll delve into the practical aspects of using JButton in Java Swing, complete with illuminating examples to demonstate its functionality and limitless possibilities.

online help: JButton in Java |  What is JButton in java swing with example - Online Help
online help: JButton in Java | What is JButton in java swing with example - Online Help

JButton is part of the Swing library, an essential package for developing Graphical User Interface (GUI) applications in Java. It extends the Swing's AbstractButton class, and by utilizing its rich set of methods and properties, you can craft highly interactive and user-friendly interfaces.

Java Swings #1  Simple Login UI
Java Swings #1 Simple Login UI

Getting Started with JButton

To commence using JButton in your program, you first need to import the necessary Java Swing packages into your class. This includes the classes for Button, Frame, and other essential components. Here's an elementary illustration:

Java Swing Tutorial: How to Create a GUI Application in Java
Java Swing Tutorial: How to Create a GUI Application in Java

```java import javax.swing.*; ```

Simple JButton Creation

Java Swing Tutorial | Tpoint Tech
Java Swing Tutorial | Tpoint Tech

Let's initiate by creating a simple JButton. Firstly, you instantiate a new JButton object by using its constructor. Typically, a JLabel is used to set the text displayed on the button.

```java JButton button = new JButton("Click Me"); ```

JButton in a JFrame

Basic GUI in Java | Swing GUI Tutorial for Beginners
Basic GUI in Java | Swing GUI Tutorial for Beginners

```java JFrame frame = new JFrame(); // Creating a JFrame JButton button = new JButton("Click Me"); // Creating a JButton frame.add(button); // Adding the JButton to the JFrame frame.setSize(300, 200); // Setting the size of the JFrame frame.setVisible(true); // Displaying the JFrame ```

JButton Event Handling

JButton in Java Swing can respond to events, principally mouse click events. This enables user interaction with your application through button clicks.

How to create button using JButton in Swing GUI API ? | Swing GUI API | @TechRanch
How to create button using JButton in Swing GUI API ? | Swing GUI API | @TechRanch

To handle these events, we use the ActionListener interface. We declare an object of ActionListener and override its 'actionPerformed' method. This method is called whenever a button is clicked.

Using Anonymous Inner Classes

How to design a simple dashboard UI using Swing and Java -Netbeans
How to design a simple dashboard UI using Swing and Java -Netbeans
Java Swing Sign Up using Eclipse WindowBuilder
Java Swing Sign Up using Eclipse WindowBuilder
Java Swing/GUI Cheat Sheet
Java Swing/GUI Cheat Sheet
Java Swing Tutorial
Java Swing Tutorial
Java Swing UI - SignUp Example KControls
Java Swing UI - SignUp Example KControls
Flame Ignited! Elite Python, Java, JS Services: Spark Your Search – Order for Fiery Finishes!
Flame Ignited! Elite Python, Java, JS Services: Spark Your Search – Order for Fiery Finishes!
an image of some type of project ideas
an image of some type of project ideas
JavaFX Vs Java Swing: Which Is Better for Web Application Development?
JavaFX Vs Java Swing: Which Is Better for Web Application Development?
#1 JavaFX UI Design Dashboard Utility
#1 JavaFX UI Design Dashboard Utility
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java Swing : School Management System Inspiration
Java Swing : School Management System Inspiration
Java Swing Class Hierarchy
Java Swing Class Hierarchy
the student registration screen is shown in yellow
the student registration screen is shown in yellow
Multithreading in Java: Know Best Practices, Advantages & Disadvantages
Multithreading in Java: Know Best Practices, Advantages & Disadvantages
Pacman Game in Java with Free Source Code
Pacman Game in Java with Free Source Code
How to Use HTML in Swing Components (The Javaâ„¢ Tutorials >                     Creating a GUI With Swing > Using Swing Components)
How to Use HTML in Swing Components (The Javaâ„¢ Tutorials > Creating a GUI With Swing > Using Swing Components)
Java Switch Example
Java Switch Example
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
a diagram showing the structure of an object in which it appears to be connected with other objects
a diagram showing the structure of an object in which it appears to be connected with other objects

You can anticipate a button click event using an anonymous inner class, defined within the method where you're creating the JButton. Here's an example:

```java JButton button = new JButton("Click Me"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Button has been clicked"); } }); ```

Using Lambda Expressions

Java 8 onwards supports lambda expressions, which can further simplify the handling of button click events:

```java button.addActionListener(e -> System.out.println("Button has been clicked")); ```

Customizing JButton

JButton offers a handful of properties and methods to customize their appearance, positioning, and behavior, enabling the creation of visually captivating and functional applications.

For illustration, consider changing the button's text color or disabling the button:

Changing Buttons Text Color

You can modify the text color of a JButton using its setForeground method. Here's an example:

```java button.setForeground(Color.RED); ```

Disabling a Button

You can disable a JButton using its setEnabled method, preventing any user interaction with it:

```java button.setEnabled(false); ```

Familiarizing yourself with the extensive capabilities of JButton in Java Swing opens up a world of potential for developing rich, interactive GUI applications. Whether you're creating your first Java GUI or a complex, feature-rich application, JButton offers an indispensable tool. So dig deeper, experiment with its methods and properties, and let your creativity guide you.