943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 699
  • Java RSS
Sep 22nd, 2009
0

Menu Not Displaying

Expand Post »
I'm using documentation on sun.com to try and learn Java. I was trying a simple Notepad type program but cannot get the menus to display. I'm receiving no errors and cannot figure out what I've done wrong and/or left out.

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5.  
  6.  
  7. public class NotepadInJava //implements ActionListener
  8. {
  9. //preparing container
  10. JFrame notepadFrame;
  11. JTextArea theTextArea;
  12. JScrollPane scrollPane = new JScrollPane(theTextArea);
  13.  
  14.  
  15. //File menu items
  16. JMenu file;
  17. JMenuItem open;
  18. JMenuItem save;
  19. JMenuItem exit;
  20.  
  21. //Edit menu items
  22. JMenu edit;
  23. JMenuItem copy;
  24. JMenuItem cut;
  25. JMenuItem selectAll;
  26. JMenuItem paste;
  27.  
  28. public static void main(String[] args)
  29. {
  30. NotepadInJava app = new NotepadInJava();
  31. }
  32.  
  33. public NotepadInJava()
  34. {
  35. notepadFrame = new JFrame();
  36. notepadFrame.addWindowListener(null);
  37. notepadFrame.setSize(600,600);
  38. notepadFrame.setLocation(40,20);
  39. notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. notepadFrame.setVisible(true);
  41. buildMenus();
  42. }
  43.  
  44. private void buildMenus()
  45. {
  46. //create menu bar
  47. JMenuBar MenuBar = new JMenuBar();
  48. //notepadFrame.setJMenuBar(FileMenuBar);
  49.  
  50. //build File menu
  51. file = new JMenu("File");
  52. MenuBar.add(file);
  53. //add Open to File menu
  54. open = new JMenuItem("Open");
  55. open.addActionListener(null);
  56. MenuBar.add(open);
  57. //add Save to File category
  58. save = new JMenuItem("Save");
  59. save.addActionListener(null);
  60. MenuBar.add(save);
  61. //add Exit to File category
  62. exit = new JMenuItem("Exit");
  63. exit.addActionListener(null);
  64. MenuBar.add(exit);
  65.  
  66. //build Edit menu
  67. edit = new JMenu("Edit");
  68. MenuBar.add(edit);
  69. //add Copy to Edit category
  70. copy = new JMenuItem("Copy");
  71. copy.addActionListener(null);
  72. MenuBar.add(copy);
  73. //add Cut to Edit category
  74. cut = new JMenuItem("Cut");
  75. cut.addActionListener(null);
  76. MenuBar.add(cut);
  77. //add Select All to Edit category
  78. selectAll = new JMenuItem("Select All");
  79. selectAll.addActionListener(null);
  80. MenuBar.add(selectAll);
  81. //add Paste to Edit category
  82. paste = new JMenuItem("Paste");
  83. paste.addActionListener(null);
  84. MenuBar.add(paste);
  85. }
  86. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
kahaj is offline Offline
193 posts
since Sep 2005
Sep 22nd, 2009
0

Re: Menu Not Displaying

You did not added menu bar to your frame setJMenuBar(JMenuBar menubar)
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Sep 22nd, 2009
1

Re: Menu Not Displaying

Peter_budo you beat me to it! Kahaj, what you could do is :

declare and initialize MenuBar as a class field, instead of a local variable in method buildMenus() :

java Syntax (Toggle Plain Text)
  1. JMenuBar MenuBar;

you can then initialize this variable in the class constructor:

java Syntax (Toggle Plain Text)
  1. MenuBar = new JMenuBar()

then add MenuBar to notepadFrame before you call method buildMenus():

java Syntax (Toggle Plain Text)
  1. notepadFrame.add(MenuBar);

Hope that helps.
P.S. you might want to look at layout managers for the menu items, I recomend the tutorials here:
http://java.sun.com/docs/books/tutor...out/using.html
Last edited by majestic0110; Sep 22nd, 2009 at 3:35 pm. Reason: typos, tidy up
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Sep 22nd, 2009
0

Re: Menu Not Displaying

I'm sure once I've dealt with menus more, they'll be easier. But right now, they're really a pain in the butt.

Here are the changes I made:
Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5.  
  6.  
  7. public class NotepadInJava //implements ActionListener
  8. {
  9. //preparing container
  10. JFrame notepadFrame;
  11. JMenuBar MenuBar;
  12. JTextArea theTextArea;
  13. JScrollPane scrollPane = new JScrollPane(theTextArea);
  14.  
  15.  
  16. //File menu items
  17. JMenu file;
  18. JMenuItem open;
  19. JMenuItem save;
  20. JMenuItem exit;
  21.  
  22. //Edit menu items
  23. JMenu edit;
  24. JMenuItem copy;
  25. JMenuItem cut;
  26. JMenuItem selectAll;
  27. JMenuItem paste;
  28.  
  29. public static void main(String[] args)
  30. {
  31. NotepadInJava app = new NotepadInJava();
  32. }
  33.  
  34. public NotepadInJava()
  35. {
  36. notepadFrame = new JFrame();
  37. notepadFrame.addWindowListener(null);
  38. notepadFrame.setSize(600,600);
  39. notepadFrame.setLocation(40,20);
  40. notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. notepadFrame.setVisible(true);
  42. buildMenus();
  43. }
  44.  
  45. private void buildMenus()
  46. {
  47. //create menu bar
  48. JMenuBar MenuBar = new JMenuBar();
  49. notepadFrame.setJMenuBar(MenuBar);
  50.  
  51. //build File menu
  52. file = new JMenu("File");
  53. MenuBar.add(file);
  54. //add Open to File menu
  55. open = new JMenuItem("Open");
  56. open.addActionListener(null);
  57. MenuBar.add(open);
  58. //add Save to File category
  59. save = new JMenuItem("Save");
  60. save.addActionListener(null);
  61. MenuBar.add(save);
  62. //add Exit to File category
  63. exit = new JMenuItem("Exit");
  64. exit.addActionListener(null);
  65. MenuBar.add(exit);
  66.  
  67. //build Edit menu
  68. edit = new JMenu("Edit");
  69. MenuBar.add(edit);
  70. //add Copy to Edit category
  71. copy = new JMenuItem("Copy");
  72. copy.addActionListener(null);
  73. MenuBar.add(copy);
  74. //add Cut to Edit category
  75. cut = new JMenuItem("Cut");
  76. cut.addActionListener(null);
  77. MenuBar.add(cut);
  78. //add Select All to Edit category
  79. selectAll = new JMenuItem("Select All");
  80. selectAll.addActionListener(null);
  81. MenuBar.add(selectAll);
  82. //add Paste to Edit category
  83. paste = new JMenuItem("Paste");
  84. paste.addActionListener(null);
  85. MenuBar.add(paste);
  86. }
  87. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
kahaj is offline Offline
193 posts
since Sep 2005
Sep 22nd, 2009
0

Re: Menu Not Displaying

You need to add:

java Syntax (Toggle Plain Text)
  1. notepadFrame.add(MenuBar);

before you set notepadFrame.setVisible(true);. This adds the MenuBar to the Frame.
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Sep 22nd, 2009
0

Re: Menu Not Displaying

Here's the code I have so far:
Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5.  
  6.  
  7. public class NotepadInJava //implements ActionListener
  8. {
  9. //preparing container
  10. JFrame notepadFrame;
  11. JMenuBar MenuBar;
  12. JTextArea theTextArea;
  13. JScrollPane scrollPane = new JScrollPane(theTextArea);
  14.  
  15.  
  16. //File menu items
  17. JMenu file;
  18. JMenuItem open;
  19. JMenuItem save;
  20. JMenuItem exit;
  21.  
  22. //Edit menu items
  23. JMenu edit;
  24. JMenuItem copy;
  25. JMenuItem cut;
  26. JMenuItem selectAll;
  27. JMenuItem paste;
  28.  
  29. public static void main(String[] args)
  30. {
  31. NotepadInJava app = new NotepadInJava();
  32. }
  33.  
  34. public NotepadInJava()
  35. {
  36. buildMenus();
  37. notepadFrame = new JFrame();
  38. notepadFrame.add(MenuBar);
  39. notepadFrame.addWindowListener(null);
  40. notepadFrame.setSize(600,600);
  41. notepadFrame.setLocation(40,20);
  42. notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43. notepadFrame.setVisible(true);
  44. }
  45.  
  46. private void buildMenus()
  47. {
  48. //create menu bar
  49. JMenuBar MenuBar = new JMenuBar();
  50. notepadFrame.setJMenuBar(MenuBar);
  51.  
  52. //build File menu
  53. file = new JMenu("File");
  54. MenuBar.add(file);
  55. //add Open to File menu
  56. open = new JMenuItem("Open");
  57. open.addActionListener(null);
  58. MenuBar.add(open);
  59. //add Save to File category
  60. save = new JMenuItem("Save");
  61. save.addActionListener(null);
  62. MenuBar.add(save);
  63. //add Exit to File category
  64. exit = new JMenuItem("Exit");
  65. exit.addActionListener(null);
  66. MenuBar.add(exit);
  67.  
  68. //build Edit menu
  69. edit = new JMenu("Edit");
  70. MenuBar.add(edit);
  71. //add Copy to Edit category
  72. copy = new JMenuItem("Copy");
  73. copy.addActionListener(null);
  74. MenuBar.add(copy);
  75. //add Cut to Edit category
  76. cut = new JMenuItem("Cut");
  77. cut.addActionListener(null);
  78. MenuBar.add(cut);
  79. //add Select All to Edit category
  80. selectAll = new JMenuItem("Select All");
  81. selectAll.addActionListener(null);
  82. MenuBar.add(selectAll);
  83. //add Paste to Edit category
  84. paste = new JMenuItem("Paste");
  85. paste.addActionListener(null);
  86. MenuBar.add(paste);
  87. }
  88. }

...and here's what the Debugger is yelling at me. To be totally honest, I'm not grasping what it is not liking about it.
Java Syntax (Toggle Plain Text)
  1. run:
  2. Exception in thread "main" java.lang.NullPointerException
  3. at NotepadInJava.buildMenus(NotepadInJava.java:50)
  4. at NotepadInJava.<init>(NotepadInJava.java:36)
  5. at NotepadInJava.main(NotepadInJava.java:31)
  6. Java Result: 1
  7. BUILD SUCCESSFUL (total time: 2 seconds)
Reputation Points: 10
Solved Threads: 0
Junior Poster
kahaj is offline Offline
193 posts
since Sep 2005
Sep 22nd, 2009
1

Re: Menu Not Displaying

Ok the problem there is that you have not initialized the variable MenuBar before you call the method buildMenus() on it, therefore you get a null pointer exception. You need to initialize MenuBar before you can perform any action on it. Change your constructor from this :

java Syntax (Toggle Plain Text)
  1. public NotepadInJava()
  2. {
  3. buildMenus();
  4. notepadFrame = new JFrame();
  5. notepadFrame.add(MenuBar);
  6. notepadFrame.addWindowListener(null);
  7. notepadFrame.setSize(600,600);
  8. notepadFrame.setLocation(40,20);
  9. notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. notepadFrame.setVisible(true);
  11. }

to this :

java Syntax (Toggle Plain Text)
  1. public NotepadInJava()
  2. {
  3. MenuBar = new JMenuBar();
  4. notepadFrame = new JFrame();
  5. notepadFrame.add(MenuBar);
  6. buildMenus();
  7. notepadFrame.addWindowListener(null);
  8. notepadFrame.setSize(600,600);
  9. notepadFrame.setLocation(40,20);
  10. notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. notepadFrame.setVisible(true);
  12. }

This will solve your null pointer exception. Look here for more information regarding null pointer exceptions :

http://java.sun.com/j2se/1.5.0/docs/...Exception.html


EDIT: Oh and remove this line :

java Syntax (Toggle Plain Text)
  1. JMenuBar MenuBar = new JMenuBar();

from method buildMenus() as you don't want to initialize MenuBar twice!
Last edited by majestic0110; Sep 22nd, 2009 at 6:02 pm. Reason: see edit note
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 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: Incrementing row
Next Thread in Java Forum Timeline: Help, no main classes found





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


Follow us on Twitter


© 2011 DaniWeb® LLC