JButton of Swing

Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)

This time we will deal with Swing buttons.

Swing has a class called JButton that handles buttons. What we learned this time is to add a JButton object to the frame. In this article, we will introduce the basic usage of JButton.

Use the following program as a template.

Source
import javax.swing.JFrame;

public class Test extends JFrame {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(350, 250);

        init();

        setVisible(true);

    }

    private void init() {
        // installation process
    }

}

Button with text

There are several constructors, but here we will use JButton(String text).

Deployment

In the following example, two buttons are generated and placed.

Source
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Test2 extends JFrame {

    public static void main(String[] args) {
        new Test2();
    }

    public Test2() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(350, 250);

        init();

        setVisible(true);

    }

    private void init() {
        setLayout(new FlowLayout());

        JButton button1 = new JButton("test1");
        add(button1);

        JButton button2 = new JButton("test2");
        add(button2);
    }

}

JFrame’s LayoutManager is initially set to BorderLayout, so we specified FlowLayout. In the case of BorderLayout, the first button placed will be spread over the entire area. The argument of the constructor JButton(String text) is the string to be displayed on the button. The generated button object can be added using the add(Component comp) method.

Active / Inactive

In the following example, the first button installed will be active and the next button will be inactive. (The part to be added at the end of the init() method.)

Source
button1.setEnabled(true);
button2.setEnabled(false);

The method inherited from the AbstractButton class is the public void setEnabled(boolean b) method. This method can be used to activate or inactivate the button. The argument is true if the button is to be enabled, otherwise it is false.

Operation when button is pressed

The following example shows how to set the behavior when a button is pressed. Initially, the first button is active and the next button is inactive. The process of “activating the next button" is described in the behavior when the first button is pressed.

Source
button1.setEnabled(true);
button2.setEnabled(false);

button1.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        button2.setEnabled(true);
    }
});

The method inherited from the Component class is the public void addMouseListener(MouseListener l) method. In order to receive mouse events, add a mouse listener by specifying it as an argument to this method. The MouseListener implementation class is the MouseAdapter class. This class is defined with an empty method implementation. To define the behavior when a button is pressed, override the mouseClicked method.

Buttons with icons

The constructor JButton(Icon icon) can be used to create a button with an icon.

Source
import java.awt.FlowLayout;

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

public class Test5 extends JFrame {

    public static void main(String[] args) {
        new Test5();
    }

    public Test5() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(350, 250);

        init();

        setVisible(true);

    }

    private void init() {
        setLayout(new FlowLayout());

        Icon icon = new ImageIcon("icon/sample.png");

        JButton button1 = new JButton(icon);
        add(button1);

        JButton button2 = new JButton(icon);
        add(button2);

        button1.setEnabled(true);
        button2.setEnabled(false);
    }

}

In the case of inactivity, the image became grayscale.

Buttons with text and icons

The constructor JButton(String text, Icon icon) can be used to create a button with text and an icon.

Source
setLayout(new FlowLayout());

Icon icon = new ImageIcon("icon/sample.png");

JButton button1 = new JButton("test1", icon);
add(button1);

JButton button2 = new JButton("test2", icon);
add(button2);

button1.setEnabled(true);
button2.setEnabled(false);

According to the API specification, the position of the string and icon can be adjusted using the following methods.

  • public void setVerticalTextPosition(int textPosition)
  • public void setHorizontalTextPosition(int textPosition)

That’s all. I hope this is helpful to you.

Articles next and previous in the same category