Results 1 to 4 of 4

Thread: Need JMenu help

  1. #1
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default Need JMenu help

    Hi, I wonder if anyone can help me with my error?

    here is my code:
    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class menuTutorial extends JFrame {
        JMenuBar menuBar = new JMenuBar();
     
        public menuTutorial() {
            setLayout(new FlowLayout());
    
            JMenu file = new JMenu("File");
            menuBar.add(file);
    
            JMenuItem exit = new JMenuItem("Exit");
            file.add(exit);
    
            JMenu edit = new JMenu("Edit");
            menuBar.add(edit);
            
            JMenuItem undo = new JMenuItem("Undo");
            undo.add(undo);
    
            event e = new event();
            exit.addActionListener(e);
        }
    
        public class event implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }
        
        public static void main (String args[]) {
            menuTutorial gui = new menuTutorial();
            gui.setJMenuBar(menuBar);
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setSize(400,400);
            gui.setVisible(true);
        }
    }
    this bit gives me an error: gui.setJMenuBar(menuBar);

    it says: non-static variable menuBar cannot be referenced from a static context.

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

    Default

    The menuBar variable is an instance variable and if you desire to access it in a static method, it must be associated with a menuTutorial object. But easier still is to just add the menuBar to the JFrame (the menuTutorial object or this) in the class's constructor:
    Java Code:
        public menuTutorial() {
            setLayout(new FlowLayout());
    
            JMenu file = new JMenu("File");
            menuBar.add(file);
    
            JMenuItem exit = new JMenuItem("Exit");
            file.add(exit);
    
            JMenu edit = new JMenu("Edit");
            menuBar.add(edit);
            
            JMenuItem undo = new JMenuItem("Undo");
            undo.add(undo);
    
            event e = new event();
            exit.addActionListener(e);
    
            setJMenuBar(menuBar);
        }
    This way you don't need to access it from the main method.

  3. #3
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    Thanks a bunch :)

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

Similar Threads

  1. JMenu help
    By Kyle227 in forum New To Java
    Replies: 3
    Last Post: 05-02-2010, 01:36 AM
  2. unwanted bug. JMenu
    By ocean in forum New To Java
    Replies: 7
    Last Post: 11-20-2009, 10:29 PM
  3. JMenu with Tabs using swing
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 03-03-2009, 04:29 PM
  4. JMenu to JPopupMenu
    By carderne in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 07:07 PM
  5. JMenu and JRadioButtonMenuItem
    By doron70 in forum AWT / Swing
    Replies: 3
    Last Post: 07-18-2007, 07:13 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
  •