| | |
Menu Not Displaying
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2005
Posts: 180
Reputation:
Solved Threads: 0
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)
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); } }
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!
Cause if you ain't what you is, you is what you ain't!
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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() :
you can then initialize this variable in the class constructor:
then add MenuBar to notepadFrame before you call method buildMenus():
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
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)
JMenuBar MenuBar;
you can then initialize this variable in the class constructor:
java Syntax (Toggle Plain Text)
MenuBar = new JMenuBar()
then add MenuBar to notepadFrame before you call method buildMenus():
java Syntax (Toggle Plain Text)
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.
•
•
Join Date: Sep 2005
Posts: 180
Reputation:
Solved Threads: 0
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:
Here are the changes I made:
Java Syntax (Toggle Plain Text)
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() { 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(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); } }
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!
Cause if you ain't what you is, you is what you ain't!
You need to add:
before you set notepadFrame.setVisible(true);. This adds the MenuBar to the Frame.
java Syntax (Toggle Plain Text)
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.
•
•
Join Date: Sep 2005
Posts: 180
Reputation:
Solved Threads: 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 Syntax (Toggle Plain Text)
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); } }
...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)
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)
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!
Cause if you ain't what you is, you is what you ain't!
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 :
to this :
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 :
from method buildMenus() as you don't want to initialize MenuBar twice!
java Syntax (Toggle Plain Text)
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); }
to this :
java Syntax (Toggle Plain Text)
public NotepadInJava() { MenuBar = new JMenuBar(); notepadFrame = new JFrame(); notepadFrame.add(MenuBar); buildMenus(); notepadFrame.addWindowListener(null); notepadFrame.setSize(600,600); notepadFrame.setLocation(40,20); notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); notepadFrame.setVisible(true); }
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)
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.
![]() |
Similar Threads
- SpeedUp Your Window XP Never Than Before (Windows tips 'n' tweaks)
- Customized menu displaying (ASP.NET)
- Navigation dropdown menu (HTML and CSS)
- Menu help please - Newbie (C)
- Flash menu is displaying over HTML (HTML and CSS)
- DRAM Timing causing POST error? (Troubleshooting Dead Machines)
- Help with dynamic menu (HTML and CSS)
Other Threads in the Java Forum
- Previous Thread: Incrementing row
- Next Thread: Help, no main classes found
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) calculator chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






