Hello,

I'm trying to add a JMenuBar to a JFrame and having alot of difficulties with it. I've searched the net and gone through a book I have looking for a solution to the problem, but everywhere tells me I'm doing it right, yet I've no JMenuBar at the top of my program.

If anyone can see what I'm doing wrong it would be greatly appreciated. I'm a bit stumped at the moment. I've just included the first Class and Method as the rest is very long and doesn't relate to the problem.

Thanks,

Crisko

class MainWindow extends JFrame implements ActionListener {
    
    JFrame window = new JFrame();
    
    JPanel slider = new JPanel(new GridLayout(3, 4, 3, 3));

    JMenuBar topMenu = new JMenuBar();
    JMenu gameMenu = new JMenu();
    JMenu soundMenu = new JMenu();
    JMenu helpMenu = new JMenu();
    JMenuItem restartItem = new JMenuItem("Restart Game", KeyEvent.VK_T);
    JRadioButtonMenuItem rbsoundonItem = new JRadioButtonMenuItem("Sound On");
    JRadioButtonMenuItem rbsoundoffItem = new JRadioButtonMenuItem("Sound Off");
    ButtonGroup sounds = new ButtonGroup();
    JMenuItem helpItem = new JMenuItem("Help", KeyEvent.VK_T);
    JMenuItem aboutItem = new JMenuItem("About", KeyEvent.VK_T);
    
     
    
    JButton one = new JButton("1");
    JButton two = new JButton("2");
    JButton three = new JButton("3");    
    JButton four = new JButton("4");    
    JButton five = new JButton("5");    
    JButton six = new JButton("6");    
    JButton seven = new JButton("7");    
    JButton eight = new JButton("8");
    JButton blank = new JButton();
    

    public MainWindow() {
        
        super("Ciaran's Slider Game, Yo");
        window.setSize(300,300);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        one.addActionListener(this);
        two.addActionListener(this);
        three.addActionListener(this);
        four.addActionListener(this);
        five.addActionListener(this);
        six.addActionListener(this);
        seven.addActionListener(this);
        eight.addActionListener(this);
        
        slider.add(one);
        slider.add(two);
        slider.add(three);
        slider.add(four);
        slider.add(five);
        slider.add(six);
        slider.add(seven);
        slider.add(eight);
        slider.add(blank);
        
        helpMenu.add(helpItem);
        helpItem.addActionListener(this);
        helpMenu.add(aboutItem);
        aboutItem.addActionListener(this);
        
        rbsoundonItem.setSelected(true);
        rbsoundonItem.addActionListener(this);
        rbsoundoffItem.addActionListener(this);
        sounds.add(rbsoundoffItem);
        sounds.add(rbsoundonItem);
        soundMenu.add(rbsoundonItem);
        soundMenu.add(rbsoundoffItem);
        
        restartItem.addActionListener(this);
        gameMenu.add(restartItem);
        
        topMenu.add(gameMenu);
        topMenu.add(soundMenu);
        topMenu.add(helpMenu);
        
        window.getContentPane().add(topMenu);
        
        window.setJMenuBar(topMenu);
        
        window.getContentPane().add(slider);
        
        window.setVisible(true);
        
        
    }

Once created and initialized your coponent you need to add menu item to menu and then menu to menu bar
Just quick cut from your code

JMenuBar topMenu = new JMenuBar();
JMenu gameMenu = new JMenu();
JMenu soundMenu = new JMenu();
JMenu helpMenu = new JMenu();
JMenuItem helpItem = new JMenuItem("Help", KeyEvent.VK_T);

//other component initialized

helpMenu.add(helpItem); //add item to menu
topMenu.add(helpMenu); //add menu to menu bar
window.setJMenuBar(topMenu);  //add menu bar to frame

You haven't specified any text for your menus. Use the constructor

JMenu gameMenu = new JMenu("Game");

and you will have a top level "Game" menu. Add the text for the other items and you will be set.

Also, you can ditch the getContentPane().add(topMenu). The setJMenuBar is sufficient.

Thanks for the help to both of you.

Ezzaral, it was the lack of text that was the problem. Can't believe I made that mistake. Thanks for your help.

Crisko.

This question has already been answered. Start a new discussion instead.