Java Swing Tutorial - Java Swing JButton

A JButton is also known as a push button. The user presses or clicks a JButton to perform an action.

Create JButton

JButton can display text and an icon. We can use constructors listed in the following table to create a JButton.

JButton() Creates a JButton without any label or icon.
JButton(String text)Creates a JButton and sets the specified text as its label.
JButton(Icon icon)Creates a JButton with an icon and no label.
JButton(String text, Icon icon) Creates a JButton with the specified label and icon.
JButton(Action action)Creates a JButton with an Action object.

We can create a JButton with its text as OK in the following code.

JButton closeButton  = new JButton("OK");

To create a JButton with an icon, we need to have an image file.

Icon  previousIcon = new ImageIcon("C:/images/previous.gif"); 
Icon  nextIcon = new ImageIcon("C:/images/next.gif");

// Create buttons with  icons
JButton previousButton  = new JButton("Previous", previousIcon); 
JButton nextButton = new JButton("Next", nextIcon);

Adding Icon to JButton

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
//  w w w  .  j  av  a  2s  .c  om
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("DefaultButton");
    Icon warnIcon = new ImageIcon("yourFile.gif");
    JButton button2 = new JButton(warnIcon);
    frame.setSize(300, 200);

The following code shows how to add various icons to a JButton.

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Main {
  public static void main(String[] argv) throws Exception {
    JButton button = new JButton();
    // Add rollover icon
    Icon rolloverIcon = new ImageIcon("r.gif");

    // Add pressed icon
    Icon pressedIcon = new ImageIcon("p.gif");

    // To remove rollover icon, set to null

    // To remove pressed icon, set to null


Adding a Disabled Icon to a JButton Component

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Main {
  public static void main(String[] argv) throws Exception {
    JButton button = new JButton();
    // Icon will appear gray

    // Set a disabled version of icon
    Icon disabledIcon = new ImageIcon("d.gif");

    // To remove the disabled version of the icon, set to null

    button.setDisabledIcon(new ImageIcon("icon.gif"));

Setting the Gap Size Between the Label and Icon in a JButton Component

    JButton button = new JButton();
    // Get gap size; default is 4
    int gapSize = button.getIconTextGap();

    // Set gap size


Here is how we add action handler using a lambda expression for the ActionEvent for a closeButton.

closeButton.addActionListener(() ->    {
    // The code  to handle the   action  event goes  here

Mnemonic Key

A JButton supports keyboard mnemonic, which is also known as a keyboard shortcut or keyboard indicator.

The mnemonic key is often pressed in combination with a modifier key such as an Alt key.

To install the C key as a mnemonic for a Close JButton. When we press Alt + C, the Close JButton is clicked.

If the character that is represented by the mnemonic key is found in the JButton text, its first occurrence is underlined.

The following snippet of code sets C as a mnemonic key for a Close JButton:


We can also use the following code to set a mnemonic key.


To set the F3 key as a mnemonic key, we can use the KeyEvent.VK_F3 constant.


Mnemonic, one character in a label appears underlined.

import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
/*  w ww  . j  a  v a  2s  .co m*/
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("DefaultButton");
    JButton button1 = new JButton("Text Button");
    frame.setSize(300, 200);

JButton method

The following table shows the commonly used methods of the JButton Class.

1Action getAction()
Returns the Action object associated with the JButton.
2void setAction(Action a)
Sets an Action object for the JButton and refreshes all properties for the JButton from the specified Action object.
3Icon getIcon()
Returns the Icon object associated with the JButton.
4void setIcon(Icon icon)
Sets an icon for the JButton.
5int getMnemonic()
Returns the keyboard mnemonic for this JButton.
6void setMnemonic(int n)
void setMnemonic(char c)
Sets the keyboard mnemonic for the JButton.
7String getText()
Returns the text for the JButton.
8void setText()
Sets the text for the JButton.

JButton Action

The following code shows how to use an Action object to create a JButton.

We can create an Action object and then create a JButton from it. The action object can be reused to create menu item. By disable the action object we can disable all UI controls created from it.

An Action object encapsulates the state and the behavior of a button.

We can set the text, icon, mnemonic, tool tip text, other properties, and the ActionListener in an Action object, and use the same Action object to create different UI controls.

Action is an interface. The AbstractAction class provides the default implementation for the Action interface.

AbstractAction is an abstract class. We need to inherit class from it.

import java.awt.Container;
import java.awt.event.ActionEvent;
//from w w w. j  a va2s.  c  om
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
class CloseAction extends AbstractAction {
  public CloseAction() {

  public void actionPerformed(ActionEvent event) {
public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("JFrame");
    Container contentPane = frame.getContentPane();

    JButton closeButton = new JButton(new CloseAction());


To set any property for the JButton while using the Action object, we can do so by using putValue(String key, Object value) method of the Action interface.

To set the tool tip text for the Action object closeAction.putValue(Action.SHORT_DESCRIPTION, "Closes the application");

To set the mneminic key for the Action object closeAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);

HTML Content String

To display text on a component using different fonts and color or in multiple lines, we can use an HTML string as the text for the component.

Swing components have built-in support for displaying HTML text as their labels.

We can use an HTML-formatted string as a label for a JButton, JMenuItem, JLabel, JToolTip, JTabbedPane, JTree, etc.

When using an HTML string, which should start and end with the <html> and </html> tags, respectively.

For example, to display the text "Close Window" on a JButton as its label, use the following code:

JButton b1  = new JButton("<html><body><b>Close</b>  Window</body></html>");

To display a string that contains HTML tags as a label, disable the default HTML interpretation using the html.disable component's client property.

The following code disables the HTML property for a JButton and uses HTML tags in its label:

JButton b3  = new JButton(); 
b3.putClientProperty("html.disable",  Boolean.TRUE); 
b3.setText("<html><body>HTML is disabled</body></html>");

To display multiple text in JButton add <br/> to the html string.

JButton b1  = new JButton(); 
b1.setText("<html><body>Line 1  <br/>Line 2</body></html>");