JButton in Java Swing with Examples

JButton in Java is defined in javax.swing package .

JButton in child of javax.swing.AbstractButton.

public class JButton
extends AbstractButton
implements Accessible

To create instance of JButton many constructors are provided.

JButton Consturctor

Sr No Constructors and Description
1 JButton()
Creates a button with no set text or icon.
2 JButton(Action a)
Creates a button where properties are taken from the Action supplied.
3 JButton(Icon icon)
Creates a button with an icon.
4 JButton(String text)
Creates a button with text.
5 JButton(String text, Icon icon)
Creates a button with initial text and an icon.

Let us see all constructors one by one.

Commonly used AbstractButton methods

Sr No Method and Description
1 String getText()
get the text of Button
2 void setText​(String text)
Sets the button’s text.
3 Icon getIcon()
Get the icon of Button
4 void setIcon​(Icon defaultIcon)
Set the icon on button
5 void addActionListener​(ActionListener l)
Add an action listener to a button
6 void setEnabled​(boolean b)
Enable or disable the button based on boolean argument
7 void setMnemonic​(int mnemonic)
Set the keyboard Mnemonic for button

Java JButton Examples

How to add Icon to JButton in Java

import java.awt.Container;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class JButtonExamle1 extends JFrame {

    Container container;
    JButton b1, b2, b3, b4;

    public JButtonExamle1() {

        container = this.getContentPane();
        container.setLayout(null);

        //default constructor
        b1 = new JButton();
        b1.setBounds(20, 40, 100, 40);
        container.add(b1);
        //constructor with string argument
        b2 = new JButton("Login");
        b2.setBounds(140, 40, 100, 40);
        container.add(b2);

        ImageIcon ii = new ImageIcon("d://icons/Java-icon.png");
        //constructor with image argument
        b2 = new JButton(ii);
        b2.setBounds(260, 40, 100, 40);
        container.add(b2);

        ImageIcon i1 = new ImageIcon("d://icons/loud.png");
        //constructor with string and image
        b3 = new JButton("Loud", i1);
        b3.setBounds(380, 40, 100, 40);
        container.add(b3);
    }

    public static void main(String[] args) {
        JButtonExamle1 frame = new JButtonExamle1();
        frame.setTitle("Button Example");
        frame.setVisible(true);
        frame.setBounds(250, 100, 520, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
    }

}
JButton in Java with constructor
Fig: JButton in Java with constructor

JButton Click and ActionListner Example

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class JButtonExamle2 extends JFrame implements ActionListener {

    Container container;
    JButton login;

    public JButtonExamle2() {

        container = this.getContentPane();
        container.setLayout(null);

        ImageIcon image = new ImageIcon("d://icons/login.png");
        login = new JButton("Login ", image);
        login.setBounds(140, 100, 150, 40);
        container.add(login);
        login.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == login) {
            JOptionPane.showMessageDialog(null, "You Clicked Login Button");
        }
    }

    public static void main(String[] args) {
        JButtonExamle2 frame = new JButtonExamle2();
        frame.setTitle("Button Example");
        frame.setVisible(true);
        frame.setBounds(250, 100, 520, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
    }

}
JButton Click and ActionListner Example
Fig: JButton Click and ActionListner Example

ActionCommand and Mnemonic Example in JButton

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class JButtonExamle3 extends JFrame implements ActionListener {

    Container container;
    JButton b1, b2;
    ImageIcon imageLock, imageUnLock;
    JLabel label;

    public JButtonExamle3() {

        container = this.getContentPane();
        container.setLayout(null);

        b1 = new JButton("Lock ");
        b1.setBounds(140, 170, 200, 40);
        container.add(b1);
        b1.setActionCommand("lock");
        b1.setMnemonic(KeyEvent.VK_D);
        b1.addActionListener(this);
        label = new JLabel("Alt +d to lock and unlock button");
        label.setBounds(140, 230, 200, 20);
        container.add(label);

        imageLock = new ImageIcon("d://icons/lock.png");
        imageUnLock = new ImageIcon("d://icons/unlock.png");
        b2 = new JButton(imageUnLock);
        b2.setBounds(140, 20, 200, 100);
        container.add(b2);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("lock")) {
            b2.setEnabled(false);
            b2.setIcon(imageLock);
            b1.setActionCommand("Unlock");
            b1.setText("Unlock");
        } else {
            b2.setEnabled(true);
            b2.setIcon(imageUnLock);
            b1.setActionCommand("lock");
            b1.setText("Lock");
        }
    }

    public static void main(String[] args) {
        JButtonExamle3 frame = new JButtonExamle3();
        frame.setTitle("Button Example");
        frame.setVisible(true);
        frame.setBounds(250, 100, 520, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
    }

}
ActionCommand and Mnemonic Example in JButton
Fig: ActionCommand and Mnemonic Example in JButton

Image source https://freeicons.io/