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
}

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.