Select Page

How to Use Buttons in Java Applications Staff
Published: August 23, 2022

A button is a Swing component in Java that is usually used to register some action from a user. The action comes in the form of a button being clicked. To use a button in an application or as part of a graphical user interface (GUI), developers need to create an instance of the JButton class. JButton is a class that inherits from JComponent. Therefore, you can apply the JComponent features, such as layout and key bindings, on your buttons.

In this tutorial, programmers will learn how to work with buttons in Java.

Before we begin, have you ever considered taking an online course to learn Java software development? We have a great list of the Top Online Courses to Learn Java to help get you started.

How to Use the JButton Class in Java

To create a button, simply instantiate the JButton class in your Java code like so:

JButton button = new JButton("Button");

Programmers can supply a string (or icon) to the constructor of JButton as an identifier on the screen. Since JButton is a JComponent, you need to add it to a top level container, such as JFrame, JDialog, or JApplet in order for it to appear on screen.

The Java code example below uses the JFrame container:

import javax.swing.*;
class SimpleButton{
   public static void main(String args[]){
       JFrame frame = new JFrame();
       JButton button = new JButton("Button");

You should be able to see a button displayed on your screen when you run this code in your integrated development environment (IDE) or code editor:

Java JButton Tutorial

It is important for developers to note that, when you run the code above, you may not get a similar display. This is because Swing components, by default, take on the look and feel of your application’s environment.

The example code shown does not achieve anything when you click or press the button. In practice, buttons are used to perform some action when a certain event on them occurs (i.e when pressed). This is referred to as listening for an event. The next section discusses how to listen for button events in Java.

How to Listen for Events on Buttons in Java

There are three steps programmers need to follow in order to listen for an event on a button. First, you need to implement the ActionListener interface on your event handling class. You could also extend a class that implements ActionListener instead. Here is how that looks in Java code:

class EventClass implements ActionListener { 
//some code here

Second, you need to add an instance of the event handler as an action listener to one or more components using the addActionListener() method:


The final step is to provide an implementation of the actionPerformed(ActionEvent e) method, which performs some action whenever an event is registered on a component. This method is the only method in the ActionListener interface and it is always called when an action is performed.

Read: The Top Tools for Remote Developers

Java Code Example for Button Click Events

The Java code example below displays the number of clicks a user has so far made when they click Button1:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ClicksCount implements ActionListener{
   int count = 0;// store number of clicks
       JFrame frame = new JFrame();
       JButton button1 = new JButton("Button1");
       JButton button2 = new JButton("Button2");
       frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
       frame.getRootPane().setDefaultButton(button1); // sets default button
   public void actionPerformed(ActionEvent e) {
       System.out.println("You have clicked the ACTIVE button " + count + " times");
   public static void main(String args[]){
       ClicksCount Clicks = new ClicksCount();

When you compile and run the code above, you should see two buttons. If you take good notice, you will observe that Button1 has been highlighted. This is because it has been set as the default button:

JButton vents in Java

The default button is the button that initially appears to have the focus when the program is first run. When you press Enter on your keyboard, the program clicks this button since it was already selected by default. Pressing Tab will shift focus to the other button.

You can only have, at most, one default button, and you set it by calling the setDefaultButton() method on the root pane of a top-level container.

If you click Button2 in this example, you will notice that there is not a message displayed. This is because no event handler has been registered to listen for events on this button. In other words, you would have to use the addActionListener() method with Button2 to ensure that actionPerformed(ActionEvent e) is called when it is clicked.

Final Thoughts on Buttons and Events in Java

Since you are dealing with Swing components when using buttons and JButtons, remember to import the javax.swing library into your Java code. Also, in order to use an event listener, you need to add the java.awt library, as shown in the last code example. If you do not include these libraries, you will get a compilation error.

Read more Java programming and software development tutorials.