Hi everyone, I'm having trouble with this code. It compiles fine, but clicking the menu item I just added the actionlistener to doesn't work. I'm new to java and I can't figure it out.

/*
 * NewJFrame.java
 *
 * Created on March 10, 2008, 2:47 PM
 */

package javaapplication5;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
/**
 *
 * @author  Curtis
 */
public class NewJFrame extends javax.swing.JFrame {
    
    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImages(null);

        jMenu1.setText("File");

        jMenuItem1.setText("New");
        jMenu1.add(jMenuItem1);
        jMenu1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    final JOptionPane optionPane = new JOptionPane(
                        "You pressed New.",
                        JOptionPane.INFORMATION_MESSAGE,
                        JOptionPane.OK_OPTION);
                }
            });

            jMenuItem2.setText("Open");
            jMenu1.add(jMenuItem2);

            jMenuItem3.setText("Save");
            jMenu1.add(jMenuItem3);

            jMenuItem4.setText("Save As");
            jMenu1.add(jMenuItem4);

            jMenuItem5.setText("Exit");
            jMenu1.add(jMenuItem5);

            jMenuBar1.add(jMenu1);

            jMenu2.setText("Help");

            jMenuItem6.setText("About");
            jMenu2.add(jMenuItem6);

            jMenuBar1.add(jMenu2);

            setJMenuBar(jMenuBar1);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 369, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 281, Short.MAX_VALUE)
            );

            pack();
        }// </editor-fold>
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    
    // Variables declaration - do not modify
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    // End of variables declaration
}

Dani AI

Generated

Nice catch by — the symptom in 's snippet comes from attaching behavior to the menu container instead of the actual command item. A few practical points that go beyond the immediate fix and help avoid brittle changes when using a GUI builder.

JMenu is primarily a container that opens a submenu; many look-and-feels do not fire a command-style action when the menu header is clicked. Command logic belongs on JMenuItem (or an Action) so it behaves consistently across platforms and can be reused on toolbars or accelerators.

When using NetBeans (Matisse) avoid editing the generated initComponents() block directly. Add listeners or Actions after initComponents() in the constructor, or use the GUI builder’s Events tab so the IDE generates the handler in the safe area. A better pattern for shared behavior is to use an Action implementation so the same code can be bound to a menu item, toolbar button, and keyboard accelerator without duplication:

jMenuItem1.setAction(new AbstractAction("New") {
    public void actionPerformed(ActionEvent e) {
        openNewFile(); // centralize the work here
    }
});

Quick troubleshooting checklist if a menu item still doesn’t respond: confirm the JMenuItem reference isn’t null, ensure it’s been added to the menu bar, verify setEnabled(true), run on the EDT (the usual Swing threading rules), and watch the console for swallowed exceptions. For documentation and details about menu behavior, see the Swing menus tutorial and the JMenu javadoc: Swing Menus tutorial and JMenu javadoc.

Recommended Answers

All 2 Replies

You want to add that action listener to jMenuItem1 instead of jMenu1. Also, you aren't showing the dialog. I would recommend using the static JOptionPane method to show the dialog

JOptionPane.showMessageDialog(NewJFrame.this, "You pressed New.");

.

whoops I ment to add it to jMenuItem1:$ . Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.