Search code examples
javaswinguser-interfacejmenubar

Why is the JMenuBar hidden behind the window border at the top?


The problem:

https://i.sstatic.net/KlgME.png

My code

Panel Class:

import javax.swing.*;
import java.awt.*;

public class MainFramePanel extends JPanel {
    public static final int WIDTH = 1700, HEIGHT = 900;

    private final JMenuBar menuBar = new JMenuBar();
    private final JMenu fileMenu = makeFileMenu();

    private MainFramePanel() {
        this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        this.setLayout(new BorderLayout());
        menuBar.add(fileMenu);
        this.add(menuBar, BorderLayout.NORTH);
    }

    private JMenu makeFileMenu() {
        JMenu menu = new JMenu();
        JMenuItem newFile = new JMenuItem("New"),
                openFile = new JMenuItem("Open"),
                saveFile = new JMenuItem("Save"),
                exportFile = new JMenuItem("Export As"),
                exit = new JMenuItem("Exit");
        menu.add(newFile); menu.add(openFile);
        menu.addSeparator();
        menu.add(saveFile); menu.add(exportFile);
        menu.addSeparator();
        menu.add(exit);
        return menu;
    }

    private static final MainFramePanel INSTANCE = new MainFramePanel();
    public static MainFramePanel getInstance() {
        return INSTANCE;
    }
}

Frame Class:

import javax.swing.*;

public class MainFrame extends JFrame {

    private MainFrame() {
        super("My Program");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(MainFramePanel.getInstance());
        this.pack();
        this.setLocationRelativeTo(null);
    }

    private static final MainFrame INSTANCE = new MainFrame();
    public static MainFrame getInstance() {
        return INSTANCE;
    }
}

I tried to set different BorderLayout parameters, I thought if I increased vgap, the menu bar would move down, but this didn't. I tried to do it through setBounds without using layout, but then the menu bar disappeared altogether. I tried using frame.setJMenuBar(), but I ran into the same problem.


Solution

  • By default, the height of the JMenuBar is determined by getPreferredSize which tries to fit all the components as required. Since your JMenu doesn't have a name, the JMenuBar is like - "well since you don't have any text, I'll make myself the minimum size so I don't occupy unnecessary space". So to fix this, give your JMenu a name as shown below.

    private JMenu makeFileMenu() {
        JMenu menu = new JMenu();
        menu.setName("File");
    
        JMenuItem newFile = new JMenuItem("New"),
        openFile = new JMenuItem("Open"),
        saveFile = new JMenuItem("Save"),
        exportFile = new JMenuItem("Export As"),
        exit = new JMenuItem("Exit");
    
        menu.add(newFile);
        menu.add(openFile);
        menu.addSeparator();
        menu.add(saveFile);
        menu.add(exportFile);
        menu.addSeparator();
        menu.add(exit);
            
        return menu;
    }
    

    Learn how to use a JMenu if you're confused.