James
James

Reputation: 1

Reset button for a GUI counter

What must I put in button2.addActionListener in order for my code to reset the count to zero. I am very stuck on this. I want to call on ActionReset, which is a method defined below, however I just don't know how to do it in this instance. Seriously hurting my brain to figure this one out.

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

public class GUI implements ActionListener
{
    private int count = 0;
    private JLabel label;
    private JFrame frame;
    private JPanel panel;
    public GUI()
    {
        frame = new JFrame();

        JButton button = new JButton("Click Me");
        button.addActionListener(this);
        JButton button2 = new JButton("Reset");
        button2.addActionListener(actionReset);

        label = new JLabel("Number of Clicks: 0");
        panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(60, 60, 20, 30));
        panel.setLayout(new GridLayout(0, 2));
        panel.add(button);
        panel.add(button2);
        panel.add(label);

        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("MY GUI");
        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        count++;
        label.setText("Number of Clicks: " +count);
    }

    public void actionReset(ActionEvent d)
    {
        count = 0;
    }

    public static void main(String[] args)
    {
        new GUI();
    }
}

Upvotes: 0

Views: 638

Answers (2)

Polygnome
Polygnome

Reputation: 7795

Since Java 8, ActionListener is a functional interface. Therefore, you can pass the following three things to JButton#addActionListener:

  • An instance of a class implementing ActionListener. You did that with button.addActionListener(this);.
  • A lambda with the type ActionEvent -> void, e.g. button.addActionListener(event -> System.out.println("Clicked"))
  • A method reference of the type ActionEvent -> void. You tried to do this with button2.addActionListener(actionReset);. This, however, is a compile error, since method references are referenced via object::method. In your case, this would be button2.addActionListener(this::actionReset); (@DanW's comment that this is not possible is incorrect).

I would also suggest resetting the label description when you reset the counter, otherwise the label text will only change when you increase the counter again.

public void actionReset(ActionEvent d)
{
    count = 0;
    label.setText("Number of Clicks: " +count);
}

Upvotes: 1

Marek Puchalski
Marek Puchalski

Reputation: 3659

Example:

    button2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            count = 0;
            label.setText("Number of Clicks: " +count);
        }
    });

or with lambda:

    button2.addActionListener(e -> {
        count = 0;
        label.setText("Number of Clicks: " +count);
    });

Upvotes: 0

Related Questions