1

I have a bit of trouble understanding the ActionListener if I want to separate its implementation from my GUI class, how would I do that? The program is a simple calculator. I am trying to think of a GUI class (Boundary) that only has buttons and then a separate class (Control) that handles all actionevents (button presses). Can I make a private variable of a Control type and have it work this way? Please help if you know how :)

Control Class

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.*;


public class Boundary extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel northPanel;
    private JPanel southPanel;
    private Control control;

    public Boundary()
    {
        super("Calculator");

        getContentPane().setLayout(new BorderLayout());

        northPanel = new JPanel(); northPanel.setLayout(new BorderLayout());
        southPanel = new JPanel(); southPanel.setLayout(new GridLayout(4,4));
        control = new Control();

        JTextField numberField = new JTextField(20);
        JTextField operationField = new JTextField(1);

        JButton button1 = new JButton("1");
        JButton button2 = new JButton("2");
        JButton button3 = new JButton("3");
        JButton button4 = new JButton("4");
        JButton button5 = new JButton("5");
        JButton button6 = new JButton("6");
        JButton button7 = new JButton("7");
        JButton button8 = new JButton("8");
        JButton button9 = new JButton("9");
        JButton button0 = new JButton("0");
        JButton buttonequal = new JButton("=");
        JButton buttonclear = new JButton("C");
        JButton buttonplus = new JButton("+");
        JButton buttonminus = new JButton("-");
        JButton buttondivide = new JButton("/");
        JButton buttonmultiply = new JButton("*");

        button1.addActionListener(control);
        button2.addActionListener(control);
        button3.addActionListener(control);
        button4.addActionListener(control);
        button5.addActionListener(control);
        button6.addActionListener(control);
        button7.addActionListener(control);
        button8.addActionListener(control);
        button9.addActionListener(control);
        button0.addActionListener(control);
        buttonequal.addActionListener(control);
        buttonclear.addActionListener(control);
        buttonplus.addActionListener(control);
        buttonminus.addActionListener(control);
        buttondivide.addActionListener(control);
        buttonmultiply.addActionListener(control);


        northPanel.add(numberField, BorderLayout.CENTER);
        northPanel.add(operationField, BorderLayout.EAST);

        southPanel.add(button1);
        southPanel.add(button2);
        southPanel.add(button3);
        southPanel.add(buttondivide);
        southPanel.add(button4);
        southPanel.add(button5);
        southPanel.add(button6);
        southPanel.add(buttonmultiply);
        southPanel.add(button7);
        southPanel.add(button8);
        southPanel.add(button9);
        southPanel.add(buttonminus);
        southPanel.add(buttonclear);
        southPanel.add(button0);
        southPanel.add(buttonequal);
        southPanel.add(buttonplus);

        numberField.setVisible(true);
        numberField.setEditable(false);
        operationField.setVisible(true);
        operationField.setEditable(false);







        getContentPane().add(northPanel, BorderLayout.NORTH);
        getContentPane().add(southPanel, BorderLayout.CENTER);

        setLocation(300,300);
        setSize(400,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Control Class

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;


public class Control implements ActionListener {

    private JButton button;

    Control()
    {

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {


    }

}
Barney
  • 2,355
  • 3
  • 22
  • 37
reZach
  • 8,945
  • 12
  • 51
  • 97
  • 1) Don't use more than one blank line of white-space in code. 2) Look into `Action` & `AbstractAction`. 3) Don't extend `JFrame` just use an instance. 4) See this [simple example](http://stackoverflow.com/a/7441804/418556) code for a start. – Andrew Thompson Mar 14 '13 at 01:23
  • 2
    As pointed out Action is better, however, it should work as presented as well?! – D.R. Mar 14 '13 at 01:27
  • D.R. How might I implement that a button is clicked through the Control class? Im stuck what to put in actionPerformed? Andrew Thompson, its required I use these and extend JFrame. I will clear out the white space too for it, thanks for your input – reZach Mar 14 '13 at 01:29
  • @JimRilye `Action` is a type of controller – MadProgrammer Mar 14 '13 at 01:31
  • I haven't used Action before, our teacher hadn't shown us this yet, we aren't supposed to use controllers, just extends and implements – reZach Mar 14 '13 at 01:33

1 Answers1

1

In a series of if statements, check for the "Action Command":

public void actionPerformed(ActionEvent evt) {
    String cmd = evt.getActionCommand();

    if("1".equals(cmd))
        // button1 was pressed
    else if("2".equals(cmd))
        // button2 was pressed
    . . .

As suggested, an Action would keep everything separately, thus more readable. Here's how you could use an Action (or it's subclass AbstractAction):

Action button1 = new AbstractAction("1") {
    public void actionPerformed(ActionEvent evt){
        // button one pressed
    }
};
Action button2 = new AbstractAction("2") {
    public void actionPerformed(ActionEvent evt){
        // button two pressed
    }
};

southPanel.add(button1);
southPanel.add(button2);

The add method, will convert it to a JButton, and set it as the listener.

Mordechai
  • 15,437
  • 2
  • 41
  • 82