Cortney Reagle Cortney Reagle - 1 month ago 15
Java Question

ActionListener is abstract and does not override abstract method actionPerformed -- despite containing that very method

So I'm getting the error

Class is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener


I am aware of the origin of this error but what confuses me is that I have that method.

public static ActionListener taskPerformer = new ActionListener(){
public void actionPerformed(ActionEvent e){
Clock cl = new Clock();
if(seconds<59){
seconds++;
}else{
seconds=0;
if(minutes<59){
minutes++;
}else{
minutes=0;
if(hours<12){
hours++;
}else{
hours=0;
}
}
}

cl.repaint();
}
};'


Not quite sure what's going on. Any help? Thanks in advance.

EDIT: context including imports and related methods

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Object.*;
import java.text.DecimalFormat;
import java.awt.geom.*;


public class Clock extends JFrame implements ActionListener{
public static int seconds = 0;
public static int minutes = 0;
public static int hours = 9;

public static ActionListener taskPerformer = new ActionListener(){
public void actionPerformed(ActionEvent e){
Clock cl = new Clock();
if(seconds<59){
seconds++;
}else{
seconds=0;
if(minutes<59){
minutes++;
}else{
minutes=0;
if(hours<12){
hours++;
}else{
hours=0;
}
}
}

cl.repaint();
}
};
public static Timer timer = new Timer(1000, taskPerformer);

public static void main(String[] args){
Clock cl = new Clock();
init();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGUI();
}
});
}
public static void init(){
timer.start();
}
public Clock() {
super("Clock");
timer.addActionListener(this);

}'

Answer

The problem wasn't in the original code snippet you posted. Your class Clock does not implement actionPerformed(). The taskPerformer object does. But the code is complaining that taskPerformer is an ActionListener but Clock is not.

Also, you don't appear to use taskPerformer anywhere so why do you need the anonymous class. You could just define actionPerformed() in Clock and be done with it.

I see two ways to deal with this. (I've omitted some irrelevant code to make the technique clearer).

Option 1 - Use clock as the listener

    public class Clock extends JFrame implements ActionListener {
    public static int seconds = 0;
    public static int minutes = 0;
    public static int hours = 9;

    public void actionPerformed(ActionEvent e) {
        // code
    }

    public Timer timer;

    public Clock() {
        super("Clock");
        timer = new Timer(1000, this);
        timer.addActionListener(this);    
    }
}

Option 2 - Use the anonymous listener

    public class Clock extends JFrame {
    public static int seconds = 0;
    public static int minutes = 0;
    public static int hours = 9;

    public static ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // insert code here
        }
    };

    public static Timer timer = new Timer(1000, taskPerformer);

    public Clock() {
        super("Clock");
        timer.addActionListener(taskPerformer);    
    }
}
Comments