Menu Not Displaying

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2005
Posts: 180
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Menu Not Displaying

 
0
  #1
Sep 22nd, 2009
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.

  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. }
Be what you is, don't be what you ain't.
Cause if you ain't what you is, you is what you ain't!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,217
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 489
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Menu Not Displaying

 
0
  #2
Sep 22nd, 2009
You did not added menu bar to your frame setJMenuBar(JMenuBar menubar)
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,295
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: Menu Not Displaying

 
1
  #3
Sep 22nd, 2009
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() :

  1. JMenuBar MenuBar;

you can then initialize this variable in the class constructor:

  1. MenuBar = new JMenuBar()

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

  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
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 180
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: Menu Not Displaying

 
0
  #4
Sep 22nd, 2009
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:
  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. }
Be what you is, don't be what you ain't.
Cause if you ain't what you is, you is what you ain't!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,295
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: Menu Not Displaying

 
0
  #5
Sep 22nd, 2009
You need to add:

  1. notepadFrame.add(MenuBar);

before you set notepadFrame.setVisible(true);. This adds the MenuBar to the Frame.
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 180
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: Menu Not Displaying

 
0
  #6
Sep 22nd, 2009
Here's the code I have so far:
  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.
  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)
Be what you is, don't be what you ain't.
Cause if you ain't what you is, you is what you ain't!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,295
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: Menu Not Displaying

 
1
  #7
Sep 22nd, 2009
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 :

  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 :

  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 :

  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
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC