Question

I've been trying to implement this action listener as stated above but keep receiving two errors:

-Cannot instantiate the type ActionListener
-void is an invalid type for the variable incrementAction

I've been looking up similar examples but they all seem to point to the same method of implementing it. This is where I've got to.

        increment.addActionListener(new ActionListener());{
        public void incrementAction(ActionEvent e){
            this.incrementCount();
            this.setTextField();
        }
    }
Was it helpful?

Solution

The signature of the ActionListener method is:

public void actionPerformed(ActionEvent e)

JButton increment  = new JButton();
increment.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("ActionEvent received! ");
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top