Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (7k points)

I am new to Java and I was wondering how to add functionality to menu item?

What I would like it to do, is to set two values to 0.

This is the code I have currently:

JMenuItem clear = new JMenuItem("Clear");

Options.add(clear);

1 Answer

0 votes
by (13.1k points)

You will need to add an ActionListener. This is an interface that implement a method called actionPerformed.

E.g.

clear.addActionListener(new ActionListener() {

    @Override

    public void actionPerformed(ActionEvent actionEvent) {

      // Clear two values.

    }

});

This will add an anonymous ActionListener that is invoked once the JMenuItem is clicked.

Related questions

Browse Categories

...