Home > Articles > Programming > Java

This chapter is from the book

Buttons and Action Listeners

I claim not to have controlled events, but confess plainly that events have controlled me.
Abraham Lincoln, Letter to A. G. Hodges (April 4, 1864)

So far the GUIs we have produced using Swing have included no actions. They just sat there and displayed some text. The only action they took was to go away in response to clicking the close-window button. In this section, we start to show you how to design GUIs that do things like changing color, changing text, or some more complicated actions. A button is simply a component in a GUI that looks like a button and that does something when you click it with your mouse. You create buttons in a way that is very similar to how you create labels. You add buttons to a JFrame in the same way that you add labels to a JFrame, but there is also something very new about buttons. You can associate an action with a button, so that when the user clicks the button with a mouse, the GUI performs some action. An example should make the details clear.

Programming Example: Adding Buttons

Display 12 contains a program that creates a window with two buttons: labeled "Red" and "Green". When the program is run, the window shown in Display 12 is displayed. If you click the button marked "Red" with your mouse, the color of the window changes from blue (or whatever color it is) to red. If you click the button labeled "Green". the color of the window changes to green. That is all the program does (but as you can see, you are gradually learning to build more complicated windowing interfaces). To end the program and make the window disappear, you click the close-window button.

Display 12—A GUI with Buttons Added

import javax.swing.*;
import java.awt.*; 
import java.awt.event.*;

/***********************************************
 *Simple demonstration of putting buttons in a JFrame.
 ***********************************************/
public class ButtonDemo extends JFrame implements ActionListener
{
  public static final int WIDTH = 300;
  public static final int HEIGHT = 200;

  /*************************************************
   *Creates and displays a window of the class ButtonDemo.
   *************************************************/
  public static void main(String[] args)
  {
    ButtonDemo buttonGui = new ButtonDemo();
    buttonGui.setVisible(true);
  }

  public ButtonDemo()
  {
    setSize(WIDTH, HEIGHT);

    addWindowListener(new WindowDestroyer()); 
    setTitle("Button Demo"); 
    Container contentPane = getContentPane();
    contentPane.setBackground(Color.blue);
 
    contentPane.setLayout(new FlowLayout());

    JButton stopButton = new JButton("Red");
    stopButton.addActionListener(this);
    contentPane.add(stopButton);

    JButton goButton = new JButton("Green");
    goButton.addActionListener(this); 
    contentPane.add(goButton);
  }
//It will take several subsections to fully explain this program. 
//The explanation does not end until the end of the subsection 
//entitled Action Listeners and Action Events
  public void actionPerformed(ActionEvent e) 
  {
    Container contentPane = getContentPane();

    if (e.getActionCommand().equals("Red")) 
      contentPane.setBackground(Color.red);
    else if (e.getActionCommand().equals("Green"))
      contentPane.setBackground(Color.green);
    else
      System.out.println("Error in button interface.");
  }
}

Much of what appears in Display 12 is already familiar to you. The class ButtonDemo is a derived class of the class JFrame and so it is a window interface similar to the ones we have already seen in this article. A window listener of the class WindowDestroyer is added with the method addWindowListener as in previous examples. The size is set, and the content pane is obtained, with getContentPane as in previous examples. We use a layout manager as we did in the previous example (Display 10), although this time we use the FlowLayout manager class.

What is new in Display 12 is the use of button objects of the class JButton and a new kind of listener class. We will discuss buttons and the new listener class in the next few subsections.

Buttons

A button is a GUI component that looks like a button and that can do something when you click it with your mouse. In this subsection we tell you how to add buttons to your GUI. In the next subsection we tell you how to specify what happens when the button is clicked.

A button object is created in the same way that any other object is created, but you use the class JButton when you want buttons. For example, the following example from Display 12 creates a button:

JButton stopButton = new JButton("Red");

The argument to the construct, in this case "Red", is a string that will be written on the button when the button is displayed. The string argument to the constructor, in this example "Red", specifies the string that will appear on the button when it is displayed on the screen. If you look at the GUI in Display 12, you will see that the two buttons are labeled "Red" and "Green".

The button is added to the content pane with the following:

contentPane.add(stopButton);

There is no second argument to the method add because we are using the FlowLayout manager class. If we had instead used the BorderLayout manager class, then we would have used some second argument, such as BorderLayout.NORTH.

In the next subsection we explain the lines from Display 12 involving the method addActionListener.

Action Listeners and Action Events

When you click a button with your mouse (or activate some other item in a GUI), it creates an object known as an event and sends the object to another object (or objects) known as the listener. This is called firing the event. The listener then performs some action. When we say that the event is "sent" to the listener object, what we really mean is that some method in the listener object is invoked with the event object as the argument. This invocation happens automatically. Your Swing GUI class definition will not normally contain an invocation of this method. However, your Swing GUI class definition does need to do two things: First for each button, it needs to specify what object(s) are listeners that will respond to events fired by that button; this is called registering the listener. Second, your GUI class definition(s) must also define the method(s) that will be invoked when the event is sent to the listener. Note that these methods will be defined by you, but in normal circumstances, you will never write an invocation of these methods. The invocations will take place automatically.

The following line from Display 12

stopButton.addActionListener(this);

registers this as a listener to receive events from the button named stopButton. A similar statement also registers this as a listener to receive events from the button named goButton. Because the argument is this, it means that this (the class ButtonDemo itself) is the listener class. Recall that within the definition of a class, an object of that class is called this. The class ButtonDemo is itself the listener class for the buttons inside of ButtonDemo. (To be a bit more precise, this means that each object of the class ButtonDemo is the listener for the buttons in that object.) Next, we explain how to make a class, such as ButtonDemo, into a listener class for events fired by buttons.

Different kinds of components require different kinds of listener classes to handle the events they fire. A button fires events known as action events, which are handled by listeners known as action listeners.

An action listener is an object of type ActionListener. ActionListener is not a class, but is a property that you can give to any class you define. (These properties, such as ActionListener, are known as interfaces.) To make a class into an ActionListener, you need to do two things:

  1. You add the phrase implements ActionListener to the beginning of the class definition, normally at the end of the first line.

  2. You define a method named actionPerformed.

In Display 12, we made the JFrame class named ButtonDemo into an ActionListener in just this way. In what follows, we reproduce an outline of the definition of the class ButtonDemo (with the omitted sections indicated by three dots):

public class ButtonDemo extends JFrame implements ActionListener
{
 .
 .
 .
  public void actionPerformed(ActionEvent e) 
  {
 .
 .
 .
  }
 .
 .
 .
}

We could have defined a separate class that did nothing but handle button events, but it's more convenient to make the window class ButtonDemo into the Action-Listener that will handle button events. This is convenient because the button events are supposed to change the window, and the easiest way to change a window is by a method within the window itself.

Now, suppose we create an object, buttonGui, of the class ButtonDemo as follows:

ButtonDemo buttonGui = new ButtonDemo();

The this parameter in the definition of the class ButtonDemo refers to buttonGui. So buttonGui is the action listener for the two buttons inside of buttonGui. So when a button is clicked, the method actionPerformed will be automatically invoked with the event fired by the button as the argument to actionPerformed. All that is left to explain is how the method actionPerformed works. Let's continue with our object buttonGui of the class ButtonDemo.

If you click one of the buttons inside of buttonGui with your mouse, that sends an action event to the action listener for that button. But buttonGui is the action listener for the buttons in buttonGui, so the action event goes to buttonGui. When an action listener receives an action event, the event is automatically passed to the method actionPerformed. The method actionPerformed is typically a branching statement that determines what kind of action event was fired and then performs some appropriate action. Let's look at the code for the method actionPerformed in the class ButtonDemo in Display 12. For convenience, we reproduce the definition in what follows:

public void actionPerformed(ActionEvent e) 
{
  Container contentPane = getContentPane();

  if (e.getActionCommand().equals("Red")) 
    contentPane.setBackground(Color.red);
  else if (e.getActionCommand().equals("Green"))
    contentPane.setBackground(Color.green);
  else
    System.out.println("Error in button interface.");
}

In this case, the method actionPerformed needs to know whether the action event came from the button labeled "Red" or the button labeled "Green". If e is an action event that was fired by clicking a button, then e.getActionCommand() returns the string written on the button; in this case it returns either "Red" or "Green". So, all that the method actionPerformed needs to do is to see if e.getActionCommand() is "Red" or "Green" and perform the appropriate action for that button. Note that e.getActionCommand() is an object of the class String. The class String has a method equals that can be used to check to see if e.getActionCommand() is equal to "Red" or "Green" (or any other string). The method invocation

e.getActionCommand().equals(String_Argument)

returns true if e.getActionCommand() is equal to the String_Argument, and returns false otherwise. Thus, the following code tests to see if e.getActionCommand() is equal to "Red" or "Green" and changes the color of the GUI accordingly:

if (e.getActionCommand().equals("Red")) 
  contentPane.setBackground(Color.red);
else if (e.getActionCommand().equals("Green"))
  contentPane.setBackground(Color.green);
else
  System.out.println("Error in button interface.");

The final else clause should never need to be executed. It is just there so that we would get a warning, if we made some unnoticed mistake in the code.

Gotcha: Changing the Parameter List for actionPerformed

When you make a class into an action listener, the header for the method actionPerformed is determined for you. It must have exactly one parameter, and that parameter must be of type ActionEvent, as in the following:

  public void actionPerformed(ActionEvent e)

If you change the type of the parameter or if you add (or subtract) a parameter, you will not have given a correct definition of an action listener. (Although it would be rather questionable style, you can overload the method name actionPerformed so you have multiple versions of the method actionPerformed, each with a different parameter list. But only the one shown above has anything to do with making a class into an action listener.)

The only thing you can change is the name of the parameter e because it is just a "place holder." So, the following change is acceptable:

public void actionPerformed(ActionEvent theEvent)

Of course, if you make this change, then inside the body of the method actionPerformed, you will use the identifier theEvent in place of the identifier e.

Programming Tip: Code Look and Actions Separately

You can simplify writing the code for a Swing GUI into two major parts: coding what the GUI looks like on the screen and coding the actions of the GUI. For example, consider the program in Display 12. Your first version of this program might use the following definition of the method actionPerformed:

public void actionPerformed(ActionEvent e) 
{}

With this "do-nothing" version of the method actionPerformed, your program will run and will show a display on the screen just as shown in Display 12. If you press either of the two color change buttons nothing will happen, but you can use this phase of coding to adjust details, such as which button is listed first. (Note that the close-window button does, however, work.)

After you get the GUI to look like you want it to look, you can then define the action parts of the GUI, typically the method actionPerformed. This breaks the task of coding the Swing GUI into two smaller tasks: coding the appearance of the GUI and coding the actions of the GUI. In this case that may not seem like a big deal, but on a complicated Swing GUI, each of these two tasks can be formidable, but reasonable, while coding the entire GUI as one piece can be maddeningly difficult. In such cases, this dividing into two parts can be a great help.

If you include the phrase implements ActionListener at the start of your JFrame definition, then you must include some definition of the method actionPerformed. A "do nothing" or "do little" method definition, such as

public void actionPerformed(ActionEvent e) 
{}

is often called a stub. Using stubs is a good programming technique in many contexts not just in Swing programs.

Alternatively, when doing your first version of a Swing GUI like the one in Display 12, you could omit the definition of the method actionPerformed completely, provided you also omit the phrase implements ActionListener and omit the invocations of addActionListener.

Java Tip: Use the Method setActionCommand

When you click a button with your mouse, it fires an event e known as an action event. The event e normally goes to the method actionPerformed of the action listener(s) for that button. The method actionPerformed needs to find out what button was clicked when it gets the event e. When discussing the class ButtonDemo in Display 12, we said that the method invocation e.getActionCommand() returns the string written on the button. That was a slightly simplified explanation. The invocation e.getActionCommand() returns a string known as the action command for the button. The default action command is the string written on the button, but if you want to, you can specify that a different string be the action command for a button.

For example, in Display 12, we created the stopButton as follows:

JButton stopButton = new JButton("Red");

If we do nothing more, the action command for the stopButton will be "Red". However, if you want, you can change the action command to some other string, such as "Stop". You would do so as follows:

stopButton.setActionCommand("Stop");

The method actionPerformed would then check for the string "Stop" rather than the string "Red".

You may eventually find yourself in a situation where you want the same string written on two different buttons. In such a case, you can distinguish the two buttons by using setActionCommand to give them different action commands.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020