| | |
Java actionlistener problem
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 56
Reputation:
Solved Threads: 0
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.
Java Syntax (Toggle Plain Text)
/* * 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 .
Java Syntax (Toggle Plain Text)
JOptionPane.showMessageDialog(NewJFrame.this, "You pressed New.");
![]() |
Similar Threads
- wierd gui/constructor problem (Java)
- user input random amount display average. little problem i have run into (Java)
- Have a ")" problem I can't solve (Java)
- "cannot resolve symbol"problem (Java)
- Don't understand my problem (Java)
- problem with TicTacToe..somebody help me!! (Java)
- I can't implement a word count into my text editor (JAVA) (Java)
- Memory buttons problem (Java)
- Java Applet viewer blinks everytime the init method is used. (Java)
Other Threads in the Java Forum
- Previous Thread: not sure what is wrong with this program....pleaseeee helppp confused:
- Next Thread: String Tokenizer
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api apple applet application arc arguments array arrays automation binary binarytree bluetooth byte chat chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong problem producer program programming project projectideas read recursion replaysolutions rim scanner server set size sms sort sql string swing terminal threads transforms tree ui unicode web windows






. Thanks. 