Java menu item enabling within event listener

30,295

Solution 1

I hope this small example will help you clear your doubts...

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

public class MenuExample extends JFrame {
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    JMenu menu1 = new JMenu("Edit");
    JMenuItem item1 = new JMenuItem("New");
    JMenuItem item2 = new JMenuItem("Open");

    public MenuExample() {
        setJMenuBar(menuBar);
        setVisible(true);
        setSize(400, 200);
        menuBar.add(menu);
        menuBar.add(menu1);
        menu.add(item1);
        menu.add(item2);
        item1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.showOpenDialog(null);

            }

        });
        item2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.showOpenDialog(null);

            }

        });

    }

    public static void main(String[] args) {
        MenuExample ex = new MenuExample();
    }

}

Solution 2

Don't declare these menu items (fileitem1 and fileitem2) in the same method. Just Declare your fileitem1 and fileitem2 outside of your method.

This solves your problem...

Solution 3

Delcare the JMenuItems you are trying to access as final to be accessed from within an inner class (addActionListener(new ActionListener() {...}).

Inner class needs JMenuItems accessed within ActionListener to be final. To avoid strange side-effects with closures in java variables referenced by an anonymous delegates.

Here is an example:

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

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents(frame);
        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(JFrame frame) {
        JMenuBar menuBar = new JMenuBar();

        JMenu menu1 = new JMenu("File");

        JMenuItem itemLoad = new JMenuItem("Load");
        //delcare them as final to be accessed from within an inner class
        final JMenuItem itemAdd = new JMenuItem("Add");
        final JMenuItem itemDel = new JMenuItem("Del");

        itemAdd.setEnabled(false);
        itemDel.setEnabled(false);

        itemLoad.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                itemAdd.setEnabled(true);
                itemDel.setEnabled(true);
            }
        });

        menu1.add(itemLoad);
        menu1.add(itemAdd);
        menu1.add(itemDel);
        menuBar.add(menu1);
        frame.setJMenuBar(menuBar);
    }
}
Share:
30,295
Daniel Del Core
Author by

Daniel Del Core

Updated on September 23, 2020

Comments

  • Daniel Del Core
    Daniel Del Core over 3 years

    Hello im trying to enable my JMenuItem from within an event listener but it seems to be out of scope. im new to java so how would i properly go about this. the said event listener will change to a new view and enable the disabled menu items.

        //Create and add MenuItems
        JMenuItem fileItem0 = new JMenuItem("Load");
        collMenu.add(fileItem0);
    
        JMenuItem fileItem1 = new JMenuItem("Add");
        fileItem1.setEnabled(false);
        collMenu.add(fileItem1);
    
        JMenuItem fileItem2 = new JMenuItem("Delete");
        fileItem2.setEnabled(false);
        collMenu.add(fileItem2);
    
        //Add Menu bar to frame
        menubar.add(collMenu);
    
        //Menu Item Functions
        fileItem0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                JOptionPane.showMessageDialog(null,"You selected: Load.");
                //Enable fileitem1 & 2 here ?? 
            }
        });
    
  • Daniel Del Core
    Daniel Del Core over 11 years
    should i be setting everything in that scope?