user2777745
user2777745

Reputation: 64

JMenuBar won't show up in JFrame

I know this question has been asked a lot but nothing seems to be working for me so I'll ask again. I'm trying to get a JMenuBar, with a JMenu, to show up in my Window class which extends JFrame. Here is my relevant code:

public class Window extends JFrame {
    //class variables
    JMenuBar menuBar;
    JMenu menu;

    Window() throws IOExcpetion {

    menuBar = new JMenuBar();
    menu = new JMenu("A Menu");
    menuBar.add(menu);
    this.setJMenuBar(menuBar);
    this.add(menuBar); //I've tried with and without this
    menu.setVisible(true);
    menuBar.setVisible(true);

    this.setVisible(true);

    while(true) {
        repaint(); //my paint method doesn't touch the JMenuBar or JMenu
    }
}

Upvotes: 0

Views: 1823

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

Kill...

while(true) {
    repaint(); //my paint method doesn't touch the JMenuBar or JMenu
}

This is blocking the Event Dispatching Thread, making it impossible for the system to paint, well, anything...

Also...

menu.setVisible(true);
menuBar.setVisible(true);

Swing components are visible by default, so the above is pointless, I know, you were scratching at crumbs, but you should beware that this is very rarely the problem

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestWindow extends JFrame {

    //class variables
    JMenuBar menuBar;
    JMenu menu;

    TestWindow() throws IOException {

        menuBar = new JMenuBar();
        menu = new JMenu("A Menu");
        menuBar.add(menu);
        this.setJMenuBar(menuBar);
//        this.add(menuBar); //I've tried with and without this
//        menu.setVisible(true);
//        menuBar.setVisible(true);

        this.setVisible(true);

//        while (true) {
//            repaint(); //my paint method doesn't touch the JMenuBar or JMenu
//        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }

                    TestWindow frame = new TestWindow();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}

Upvotes: 2

Related Questions