943,846 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5277
  • Java RSS
Mar 10th, 2008
0

Java actionlistener problem

Expand Post »
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)
  1. /*
  2.  * NewJFrame.java
  3.  *
  4.  * Created on March 10, 2008, 2:47 PM
  5.  */
  6.  
  7. package javaapplication5;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import javax.swing.JOptionPane;
  11. /**
  12.  *
  13.  * @author Curtis
  14.  */
  15. public class NewJFrame extends javax.swing.JFrame {
  16.  
  17. /** Creates new form NewJFrame */
  18. public NewJFrame() {
  19. initComponents();
  20. }
  21.  
  22. /** This method is called from within the constructor to
  23.   * initialize the form.
  24.   * WARNING: Do NOT modify this code. The content of this method is
  25.   * always regenerated by the Form Editor.
  26.   */
  27. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  28. private void initComponents() {
  29.  
  30. jMenuBar1 = new javax.swing.JMenuBar();
  31. jMenu1 = new javax.swing.JMenu();
  32. jMenuItem1 = new javax.swing.JMenuItem();
  33. jMenuItem2 = new javax.swing.JMenuItem();
  34. jMenuItem3 = new javax.swing.JMenuItem();
  35. jMenuItem4 = new javax.swing.JMenuItem();
  36. jMenuItem5 = new javax.swing.JMenuItem();
  37. jMenu2 = new javax.swing.JMenu();
  38. jMenuItem6 = new javax.swing.JMenuItem();
  39.  
  40. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  41. setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  42. setIconImages(null);
  43.  
  44. jMenu1.setText("File");
  45.  
  46. jMenuItem1.setText("New");
  47. jMenu1.add(jMenuItem1);
  48. jMenu1.addActionListener(new ActionListener()
  49. {
  50. public void actionPerformed(ActionEvent e)
  51. {
  52. final JOptionPane optionPane = new JOptionPane(
  53. "You pressed New.",
  54. JOptionPane.INFORMATION_MESSAGE,
  55. JOptionPane.OK_OPTION);
  56. }
  57. });
  58.  
  59. jMenuItem2.setText("Open");
  60. jMenu1.add(jMenuItem2);
  61.  
  62. jMenuItem3.setText("Save");
  63. jMenu1.add(jMenuItem3);
  64.  
  65. jMenuItem4.setText("Save As");
  66. jMenu1.add(jMenuItem4);
  67.  
  68. jMenuItem5.setText("Exit");
  69. jMenu1.add(jMenuItem5);
  70.  
  71. jMenuBar1.add(jMenu1);
  72.  
  73. jMenu2.setText("Help");
  74.  
  75. jMenuItem6.setText("About");
  76. jMenu2.add(jMenuItem6);
  77.  
  78. jMenuBar1.add(jMenu2);
  79.  
  80. setJMenuBar(jMenuBar1);
  81.  
  82. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  83. getContentPane().setLayout(layout);
  84. layout.setHorizontalGroup(
  85. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  86. .addGap(0, 369, Short.MAX_VALUE)
  87. );
  88. layout.setVerticalGroup(
  89. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  90. .addGap(0, 281, Short.MAX_VALUE)
  91. );
  92.  
  93. pack();
  94. }// </editor-fold>
  95.  
  96. /**
  97.   * @param args the command line arguments
  98.   */
  99. public static void main(String args[]) {
  100. java.awt.EventQueue.invokeLater(new Runnable() {
  101. public void run() {
  102. new NewJFrame().setVisible(true);
  103. }
  104. });
  105. }
  106.  
  107.  
  108. // Variables declaration - do not modify
  109. private javax.swing.JMenu jMenu1;
  110. private javax.swing.JMenu jMenu2;
  111. private javax.swing.JMenuBar jMenuBar1;
  112. private javax.swing.JMenuItem jMenuItem1;
  113. private javax.swing.JMenuItem jMenuItem2;
  114. private javax.swing.JMenuItem jMenuItem3;
  115. private javax.swing.JMenuItem jMenuItem4;
  116. private javax.swing.JMenuItem jMenuItem5;
  117. private javax.swing.JMenuItem jMenuItem6;
  118. // End of variables declaration
  119. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007
Mar 10th, 2008
0

Re: Java actionlistener problem

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)
  1. JOptionPane.showMessageDialog(NewJFrame.this, "You pressed New.");
.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Mar 10th, 2008
0

Re: Java actionlistener problem

whoops I ment to add it to jMenuItem1 . Thanks.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: not sure what is wrong with this program....pleaseeee helppp confused:
Next Thread in Java Forum Timeline: String Tokenizer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC