Java menu item enabling within event listener

我只是一个虾纸丫 提交于 2021-01-27 11:24:52

问题


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 ?? 
        }
    });

回答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();
    }

}



回答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...




回答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);
    }
}


来源:https://stackoverflow.com/questions/12543206/java-menu-item-enabling-within-event-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!