Daniel Ashfall Zhou
Daniel Ashfall Zhou

Reputation: 167

Event Handling Problems - Java

I'm having major trouble right now with my program. I don't understand this error message "MyEventA is not abstract and does not override abstract method actionPerformed(java.awt.event.actionEvent) in java.awt.event.ActionListener." I have tried looking online and in my textbook and I still can't get it working.

I'd really appreciate if someone could help me figure out what is wrong with my code. I have been working on this for and hour and a half and still haven't fixed it. :( Thanks in advance!

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

public class MyEventA extends JFrame implements ActionListener
{
    private JButton plus;
    private JButton minus;
    private JButton reset;
    private JButton quit;

    public MyEventA()
{
    add(new JLabel("Current Value", JLabel.LEFT), BorderLayout.NORTH);
    JTextField jtfCurrent = new JTextField("0");
    add(jtfCurrent, BorderLayout.EAST);
    JPanel jpSouth = new JPanel();
    plus = new JButton("+");
    minus = new JButton("-");
    reset = new JButton("Reset");
    quit = new JButton("Quit");
    ButtonListener b1 = new ButtonListener(jtfCurrent);
    jtfCurrent.addActionListener(b1);
    plus.addActionListener(this);
    minus.addActionListener(this);
    reset.addActionListener(this);
    quit.addActionListener(this);
    jpSouth.add(plus);
    jpSouth.add(minus);
    jpSouth.add(reset);
    jpSouth.add(quit);
    add(jpSouth, BorderLayout.SOUTH);
}

class ButtonListener implements ActionListener {
    private JTextField writeInto;   // text field reference
    private int count = 0;

    public ButtonListener(JTextField tf) {
        writeInto = tf;
        count = 0;
    }

    public void actionPerformed(ActionEvent ae){
        if(ae.getActionCommand().equals("+")){
            count++;
            writeInto.setText("" + count);
        }
        else if(ae.getActionCommand().equals("-")){
            count--;
            writeInto.setText("" + count);
        }
        else if(ae.getActionCommand().equals("Reset")){
            count = 0;
            writeInto.setText("" + count);
        }
        else {
            System.exit(0);    
        }
    }

    public static void main(String[] args){
        MyEventA events = new MyEventA();
        events.setTitle("Part 2 Using getSource");
        events.pack();
        events.setDefaultCloseOperation(EXIT_ON_CLOSE);
        events.setSize(300,100);
        events.setLocation(200,200);
        events.setVisible(true);
    }
}
}

Upvotes: 0

Views: 376

Answers (4)

Visruth
Visruth

Reputation: 3450

Rewrite it as follows, it works for me:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class MyEventA extends JFrame implements ActionListener {
    private JButton plus;
    private JButton minus;
    private JButton reset;
    private JButton quit;

    public MyEventA() {
        add(new JLabel("Current Value", JLabel.LEFT), BorderLayout.NORTH);
        JTextField jtfCurrent = new JTextField("0");
        add(jtfCurrent, BorderLayout.EAST);
        JPanel jpSouth = new JPanel();
        plus = new JButton("+");
        minus = new JButton("-");
        reset = new JButton("Reset");
        quit = new JButton("Quit");
        ButtonListener b1 = new ButtonListener(jtfCurrent);
        jtfCurrent.addActionListener(b1);
        plus.addActionListener(this);
        minus.addActionListener(this);
        reset.addActionListener(this);
        quit.addActionListener(this);
        jpSouth.add(plus);
        jpSouth.add(minus);
        jpSouth.add(reset);
        jpSouth.add(quit);
        add(jpSouth, BorderLayout.SOUTH);
    }

    class ButtonListener implements ActionListener {
        private JTextField writeInto; // text field reference
        private int count = 0;

        public ButtonListener(JTextField tf) {
            writeInto = tf;
            count = 0;
        }

        public void actionPerformed(ActionEvent ae) {
            if (ae.getActionCommand().equals("+")) {
                count++;
                writeInto.setText("" + count);
            } else if (ae.getActionCommand().equals("-")) {
                count--;
                writeInto.setText("" + count);
            } else if (ae.getActionCommand().equals("Reset")) {
                count = 0;
                writeInto.setText("" + count);
            } else {
                System.exit(0);
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {// was to be implemented

        javax.swing.JOptionPane.showMessageDialog(null, "clicked");

    }

    public static void main(String[] args) {// main method should be in the outer class
        MyEventA events = new MyEventA();
        events.setTitle("Part 2 Using getSource");
        events.pack();
        events.setDefaultCloseOperation(EXIT_ON_CLOSE);
        events.setSize(300, 100);
        events.setLocation(200, 200);
        events.setVisible(true);
    }

}

If you want to use the actionPerformed in ButtonListener class, use the below code :

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class MyEventA extends JFrame implements ActionListener {
    private JButton plus;
    private JButton minus;
    private JButton reset;
    private JButton quit;   


    public MyEventA() {
        add(new JLabel("Current Value", JLabel.LEFT), BorderLayout.NORTH);
        JTextField jtfCurrent = new JTextField("0");
        add(jtfCurrent, BorderLayout.EAST);
        JPanel jpSouth = new JPanel();
        plus = new JButton("+");
        minus = new JButton("-");
        reset = new JButton("Reset");
        quit = new JButton("Quit");     


        ButtonListener b1 = new ButtonListener(jtfCurrent);
        jtfCurrent.addActionListener(b1);
        plus.addActionListener(new ButtonListener(jtfCurrent));
        minus.addActionListener(new ButtonListener(jtfCurrent));
        reset.addActionListener(new ButtonListener(jtfCurrent));
        quit.addActionListener(new ButtonListener(jtfCurrent));
        jpSouth.add(plus);
        jpSouth.add(minus);
        jpSouth.add(reset);
        jpSouth.add(quit);
        add(jpSouth, BorderLayout.SOUTH);
    }

    class ButtonListener implements ActionListener {
        private JTextField writeInto; // text field reference
        private int count = 0;
        public ButtonListener() {

        }
        public ButtonListener(JTextField tf) {
            writeInto = tf;
            count = 0;
        }

        public void actionPerformed(ActionEvent ae) {
            if (ae.getActionCommand().equals("+")) {
                count++;
                writeInto.setText("" + count);
            } else if (ae.getActionCommand().equals("-")) {
                count--;
                writeInto.setText("" + count);
            } else if (ae.getActionCommand().equals("Reset")) {
                count = 0;
                writeInto.setText("" + count);
            } else {
                System.exit(0);
            }
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        javax.swing.JOptionPane.showMessageDialog(null, "clicked");

    }

    public static void main(String[] args) {
        MyEventA events = new MyEventA();
        events.setTitle("Part 2 Using getSource");
        events.pack();
        events.setDefaultCloseOperation(EXIT_ON_CLOSE);
        events.setSize(300, 100);
        events.setLocation(200, 200);
        events.setVisible(true);
    }

}

Upvotes: 1

T I
T I

Reputation: 9933

Because MyEventA has implemented ActionListener and because it is not an abstract class it must implement / define the method actionPeformed (ActionEvent) much in the same way you have done in ButtonListener

But I think what you want to do is actually not have MyEventA implement ActionListener and instead pass a new ButtonListener () to your buttons like

plus.addListener (new ButtonListener ());

Upvotes: 1

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

In general, when a (non-abstract) class implements an interface, a contract is made suggesting that this class will implement all methods provided in the interface.

Your class MyEventA implements the interface ActionListener, which requires a method to be overridden (void actionPerformed(ActionEvent)). Since MyEventA is not abstract, the compiler is telling you that you need to implement the method here.

If you are wondering how you can implement the method, you have a good example in your subclass (ButtonListener) of an implementation. Here you can see that ButtonListener implements the same interface, ActionListener, and implements its method.

Upvotes: 2

Jaydeep Rajput
Jaydeep Rajput

Reputation: 3673

You must override actionPerformed(java.awt.event.ActionEvent) method if you are implementing ActionListener interface.The compiler asks the class to be declared as abstarct if the method in interface is not overridden.

Upvotes: 1

Related Questions