Herong's Tutorial Notes on Swing
Dr. Herong Yang, Version 3.05, 2006

Swing JButton

This chapter discusses the following problems:

  • How to create buttons.
  • How to handle user clicks on buttons.

My First JButton

Button is so to create. Here is a sample program:

/**
 * JButtonOk.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import javax.swing.*;
public class JButtonOk {
   public static void main(String[] a) {
      JFrame f = new JFrame("My First Button");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JButton b = new JButton("OK");
      f.getContentPane().add(b);
      f.pack();      
      f.setVisible(true);
   }
}

Image Button

You can also create a button with your own image. Here is a sample program:

/**
 * JButtonIcon.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import javax.swing.*;
public class JButtonIcon {
   public static void main(String[] a) {
      JFrame f = new JFrame("My Icon Button");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JButton b = new JButton(new ImageIcon("java.gif"));
      f.getContentPane().add(b);
      f.pack();      
      f.setVisible(true);
   }
}

Handling Button Actions

One way to handle button actions is to add an action listener to the button object. An action listener is an object of any class that implements the ActionListener interface.

The following program shows you how to extend the JButton class to implement the ActionListener class:

/**
 * JButtonAction1.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonAction1 {
   public static void main(String[] a) {
      JFrame f = new JFrame("My Switch Button");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JButton b = new MyButton();
      f.getContentPane().add(b);
      f.pack();
      f.setVisible(true);
   }
   private static class MyButton extends JButton 
      implements ActionListener {
       String text = "On";
       public MyButton() {
          super();
          setText(text);
          addActionListener(this);
       }
       public void actionPerformed(ActionEvent e) {
          if (text.equals("On")) text = "Off";
          else text = "On"; 
          setText(text);
       }
   }
}

The button works well. If you click the button, the button label text will change from "On" to "Off"; and from "Off" to "On", if you click it again.

Extending the button to handle its own action is fine, if the action only requires modifying the behavior of the same button. If the action requires modifying the behavior of other components, you need to implement the action listener as a higher level. The following program shows you how to use the action handler to modify the text of a label component:

/**
 * JButtonAction2.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonAction2 implements ActionListener {
   JButton myButton = null;
   JLabel myLebal = null;
   String text = null;
   public static void main(String[] a) {
      JButtonAction2 myTest = new JButtonAction2();
      myTest.createFrame();
   }
   public void createFrame() {
      JFrame f = new JFrame("My Switch Button");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container c = f.getContentPane();
      c.setLayout(new GridLayout(2,1));
      text = "On";
      myButton = new JButton(text);
      myButton.addActionListener(this);
      c.add(myButton);
      myLebal = new JLabel(text,SwingConstants.CENTER);
      c.add(myLebal);
      f.pack();
      f.setVisible(true);
   }
   public void actionPerformed(ActionEvent e) {
      if (text.equals("On")) text = "Off";
      else text = "On"; 
      myButton.setText(text);
      myLebal.setText(text);
   }
}

Of course, using ActionListeners is not the only way to handle user clicks on buttons. You can also use MouseListeners to handle user clicks. Here is an example program:

/**
 * JButtonAction3.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonAction3 extends MouseAdapter {
   JButton myButton = null;
   JLabel myLebal = null;
   String text = null;
   public static void main(String[] a) {
      JButtonAction3 myTest = new JButtonAction3();
      myTest.createFrame();
   }
   public void createFrame() {
      JFrame f = new JFrame("My Switch Button");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container c = f.getContentPane();
      c.setLayout(new GridLayout(2,1));
      text = "On";
      myButton = new JButton(text);
      myButton.addMouseListener(this);
      c.add(myButton);
      myLebal = new JLabel(text,SwingConstants.CENTER);
      c.add(myLebal);
      f.pack();
      f.setVisible(true);
   }
   public void mouseClicked(MouseEvent e) {
      if (text.equals("On")) text = "Off";
      else text = "On"; 
      myButton.setText(text);
      myLebal.setText(text);
   }
}
Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Swing - Swing JButton