Results 1 to 13 of 13
  1. #1
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default 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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Please use code tags if you have to post code.
    You never set the Menubar to the frame
    Java Code:
    notepadFrame.setJMenuBar(MenuBar);
    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.

  3. #3
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default

    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
    Java Code:
    private void buildMenus()
    I commented out
    Java Code:
    //notepadFrame.setJMenuBar(FileMenuBar);
    after realizing I had done several things incorrectly.

    I just changed this line to
    Java Code:
    notepadFrame.setJMenuBar(MenuBar);
    inside of
    Java Code:
    private void buildMenus()
    Unfortunately, the menus are still not showing up.

  4. #4
    mcashe is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    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.

  5. #5
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default

    Here's the code I have so far:
    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);
        }
    }
    ...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:
    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)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by kahaj View Post
    Sorry, I hit Enter before putting the code tags or even posting my problem.
    You can still edit your previous posts. I have taken the liberty of adding code tags.
    ...Unfortunately, the menus are still not showing up.
    Let's see your most recent attempt at this project, but please use code tags. Thanks

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default

    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"?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    [QUOTE=kahaj;83415]I thought that I created the JFrame right off at class level with:
    Java Code:
    JFrame notepadFrame;
    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 = new JFrame("My JFrame");

  12. #12
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default

    Okay, I have it all figured out now. It works fine. Thanks so much for everyone's help w/ this.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. applet linking menus
    By lordbob75 in forum Java Applets
    Replies: 0
    Last Post: 01-27-2009, 10:49 PM
  2. Displaying image in same jsp
    By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-08-2008, 06:01 AM
  3. Displaying a table in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:56 PM
  4. Creating popup menus with Swing
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:49 PM
  5. displaying string
    By jamborta in forum AWT / Swing
    Replies: 6
    Last Post: 01-23-2008, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •