1. Technology

Creating A Simple Calculator to Handle Button Events


5 of 7

Implement the ActionListener Interface As An Inner Class

We could implement the ActionListener interface in a separate class to SimpleCalc but because all we want the class to do is handle button click events it can be neater to use an inner class inside the SimpleCalc class. This also has the advantage of allowing the inner class access to the components defined within the SimpleCalc class.

When they are clicked the arithmetic operation buttons (i.e., + and -) will need to perform actions that are very similar to each other. The only difference is whether numbers are going to be added or subtracted. This makes it ideal for both buttons to use an inner class which implements the ActionListener interface.

Declaring the inner class is the same as any class except it lives inside the SimpleCalc class:

public class SimpleCalc{
    private class OperatorAction implements ActionListener
        public void actionPerformed(ActionEvent event)


The inner class is called operatorAction as its main task is to populate the currentCalc and calcOperation variables of the SimpleCalc class. The currentCalc variable holds the number currently in the JTextField and the calcOperation variable holds an int value that represents whether it will be an addition (i.e., 1) or a subtraction (i.e., 2). These variables will be used by the equals button when it comes to add or subtract the numbers.

Let's fill in the rest of the code for the inner class:

private class OperatorAction implements ActionListener
    private int operator;
    public OperatorAction(int operation)
       operator = operation;
    public void actionPerformed(ActionEvent event)
       currentCalc = Integer.parseInt(numberCalc.getText()); 
       calcOperation = operator;

The inner class is given a constructor to hold the operation (i.e., either add or subtract) which will depend on which of the two buttons is clicked. This type of operation will be defined when the ActionListener is added to each button. The code in the actionPerformed method simply sets the currentCalc variable with the value in the JTextField and sets the operation according to the button being clicked.

As before we just need to hook the ActionListener to each button:

JButton addButton = new JButton("+");
OperatorAction subAction = new OperatorAction(1);
JButton subButton = new JButton("-");
OperatorAction addAction = new OperatorAction(2);

As you can see when a new OperatorAction object is created it is passed the value representing the arithmetic operation (i.e, 1 for add, 2 for subtract). Then the OperatorAction object is attached to the button using the addActionListener method.

Now when either of the two arithmetic operation buttons are clicked the Simple Calculator program will know what the first number to be used is and what the arithmetic operation to be performed by the equals button is.

  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Handling Events
  6. Implement the ActionListener Interface As An Inner Class

©2014 About.com. All rights reserved.