Java actionlistener problem

Thread Solved

Join Date: Sep 2007
Posts: 56
Reputation: curt22 is an unknown quantity at this point 
Solved Threads: 0
curt22 curt22 is offline Offline
Junior Poster in Training

Java actionlistener problem

 
0
  #1
Mar 10th, 2008
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,444
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Java actionlistener problem

 
0
  #2
Mar 10th, 2008
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
  1. JOptionPane.showMessageDialog(NewJFrame.this, "You pressed New.");
.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 56
Reputation: curt22 is an unknown quantity at this point 
Solved Threads: 0
curt22 curt22 is offline Offline
Junior Poster in Training

Re: Java actionlistener problem

 
0
  #3
Mar 10th, 2008
whoops I ment to add it to jMenuItem1 . Thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC