2

My JMenuBar is not showing up when i run my App. How can I fix this??

So when I run my JFrame I need to see my JMenuBar on top.

my Layout is Null

Code:

package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.WindowConstants;


public class Home extends JFrame {

    private Container window = getContentPane();

    public Home(){
        initGUI();  
    }

    public void initGUI(){

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        
        setPreferredSize(new Dimension(800, 600));
        setLayout(null);

        JMenuBar menu = new JMenuBar();
        JMenu file = new JMenu("File");
        menu.add(file);
        window.add(menu);



        pack();

    }
    }
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
user1809035
  • 237
  • 1
  • 3
  • 9
  • 1
    unrelated, but important: never-ever do any manual sizing/locating (aka: null Layout) Don't forget to read and understand the tutorial referenced in the swing tag wiki - it'll answer such basic questions :-) – kleopatra Nov 08 '12 at 12:11

2 Answers2

3
  • you not added JMenuBar to the JFrame

  • use setJMenuBar(menu);

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

Instead of add(menu) call

  • setJMenuBar(menu);

Also you better use SwingUtilities.invokeLater()

Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101