Results 1 to 13 of 13
Thread: Menus Not Displaying
- 09-22-2009, 07:55 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 12
- Rep Power
- 0
Menus Not Displaying
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class NotepadInJava //implements ActionListener { //preparing container JFrame notepadFrame; JTextArea theTextArea; JScrollPane scrollPane = new JScrollPane(theTextArea); //File menu items JMenu file; JMenuItem open; JMenuItem save; JMenuItem exit; //Edit menu items JMenu edit; JMenuItem copy; JMenuItem cut; JMenuItem selectAll; JMenuItem paste; public static void main(String[] args) { NotepadInJava app = new NotepadInJava(); } public NotepadInJava() { notepadFrame = new JFrame(); notepadFrame.addWindowListener(null); notepadFrame.setSize(600,600); notepadFrame.setLocation(40,20); notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); notepadFrame.setVisible(true); buildMenus(); } private void buildMenus() { //create menu bar JMenuBar MenuBar = new JMenuBar(); //notepadFrame.setJMenuBar(FileMenuBar); //build File menu file = new JMenu("File"); MenuBar.add(file); //add Open to File menu open = new JMenuItem("Open"); open.addActionListener(null); MenuBar.add(open); //add Save to File category save = new JMenuItem("Save"); save.addActionListener(null); MenuBar.add(save); //add Exit to File category exit = new JMenuItem("Exit"); exit.addActionListener(null); MenuBar.add(exit); //build Edit menu edit = new JMenu("Edit"); MenuBar.add(edit); //add Copy to Edit category copy = new JMenuItem("Copy"); copy.addActionListener(null); MenuBar.add(copy); //add Cut to Edit category cut = new JMenuItem("Cut"); cut.addActionListener(null); MenuBar.add(cut); //add Select All to Edit category selectAll = new JMenuItem("Select All"); selectAll.addActionListener(null); MenuBar.add(selectAll); //add Paste to Edit category paste = new JMenuItem("Paste"); paste.addActionListener(null); MenuBar.add(paste); } }Last edited by Fubarable; 09-22-2009 at 10:06 PM. Reason: Code tags added to aid in code visibility
- 09-22-2009, 08:09 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Please use code tags if you have to post code.
You never set the Menubar to the frame
That means you will need to make your MuneBar be visible in the method where you create the frame. Also only setVisible(true) after you have finished dressing the frame.Java Code:notepadFrame.setJMenuBar(MenuBar);
- 09-22-2009, 09:38 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 12
- Rep Power
- 0
Sorry, I hit Enter before putting the code tags or even posting my problem.
I'm trying to learn Java from the Sun website. The menus aren't showing on this simple app.
Inside of
I commented outJava Code:private void buildMenus()
after realizing I had done several things incorrectly.Java Code://notepadFrame.setJMenuBar(FileMenuBar);
I just changed this line to
inside ofJava Code:notepadFrame.setJMenuBar(MenuBar);
Unfortunately, the menus are still not showing up.Java Code:private void buildMenus()
- 09-22-2009, 09:46 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
I'm not certain about this but in your constructor, you're actually displaying the frame before you call the buildMenus method. Try calling this method before your setVisible(true) statement.
- 09-22-2009, 10:08 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 12
- Rep Power
- 0
Here's the code I have so far:
...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 Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class NotepadInJava //implements ActionListener { //preparing container JFrame notepadFrame; JMenuBar MenuBar; JTextArea theTextArea; JScrollPane scrollPane = new JScrollPane(theTextArea); //File menu items JMenu file; JMenuItem open; JMenuItem save; JMenuItem exit; //Edit menu items JMenu edit; JMenuItem copy; JMenuItem cut; JMenuItem selectAll; JMenuItem paste; public static void main(String[] args) { NotepadInJava app = new NotepadInJava(); } public NotepadInJava() { buildMenus(); notepadFrame = new JFrame(); notepadFrame.add(MenuBar); notepadFrame.addWindowListener(null); notepadFrame.setSize(600,600); notepadFrame.setLocation(40,20); notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); notepadFrame.setVisible(true); } private void buildMenus() { //create menu bar JMenuBar MenuBar = new JMenuBar(); notepadFrame.setJMenuBar(MenuBar); //build File menu file = new JMenu("File"); MenuBar.add(file); //add Open to File menu open = new JMenuItem("Open"); open.addActionListener(null); MenuBar.add(open); //add Save to File category save = new JMenuItem("Save"); save.addActionListener(null); MenuBar.add(save); //add Exit to File category exit = new JMenuItem("Exit"); exit.addActionListener(null); MenuBar.add(exit); //build Edit menu edit = new JMenu("Edit"); MenuBar.add(edit); //add Copy to Edit category copy = new JMenuItem("Copy"); copy.addActionListener(null); MenuBar.add(copy); //add Cut to Edit category cut = new JMenuItem("Cut"); cut.addActionListener(null); MenuBar.add(cut); //add Select All to Edit category selectAll = new JMenuItem("Select All"); selectAll.addActionListener(null); MenuBar.add(selectAll); //add Paste to Edit category paste = new JMenuItem("Paste"); paste.addActionListener(null); MenuBar.add(paste); } }
Java Code:run: Exception in thread "main" java.lang.NullPointerException at NotepadInJava.buildMenus(NotepadInJava.java:50) at NotepadInJava.<init>(NotepadInJava.java:36) at NotepadInJava.main(NotepadInJava.java:31) Java Result: 1 BUILD SUCCESSFUL (total time: 2 seconds)
-
-
Now you're calling buildMenus before you have initialized the JFrame. When you try to add the menus to the currently null JFrame, you are causing a NPE. Solution, create the JFrame first.
-
Also, you've got two JMenuBar objects you know. You'll likely want to have only one.
- 09-22-2009, 10:25 PM #9
Member
- Join Date
- Sep 2009
- Posts
- 12
- Rep Power
- 0
Sorry, but I'm brand new to Java. Trying to take a stab and see what I can do with it...
I thought that I created the JFrame right off at class level with:
Java Code:JFrame notepadFrame;
Also, what is an "NPE"?
-
NPE means Null Pointer Exception or NullPointerException. It happens when you try to call a method on an variable that does not reference an object.
-
[QUOTE=kahaj;83415]I thought that I created the JFrame right off at class level with:
This code creates a JFrame variable, but the variable doesn't refer to an object just yet but rather has nothing or null. To initialize a JFrame object you should call new JFrame(), for instance:Java Code:JFrame notepadFrame;
Java Code:JFrame notepadFrame = new JFrame("My JFrame");
- 09-23-2009, 04:23 AM #12
Member
- Join Date
- Sep 2009
- Posts
- 12
- Rep Power
- 0
Okay, I have it all figured out now. It works fine. Thanks so much for everyone's help w/ this.
-
Similar Threads
-
applet linking menus
By lordbob75 in forum Java AppletsReplies: 0Last Post: 01-27-2009, 10:49 PM -
Displaying image in same jsp
By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-08-2008, 06:01 AM -
Displaying a table in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:56 PM -
Creating popup menus with Swing
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:49 PM -
displaying string
By jamborta in forum AWT / SwingReplies: 6Last Post: 01-23-2008, 07:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks