Search code examples
javaswingjmenubar

Menu bar not showing in JFrame


I've added a a custom JPane and a custom JMenuBar to my JFrame. The JPane shows up just fine, but not the JMenuBar. I've defined the frame and the menu bar in different classes

Here's the frame class: public class pixelFrame { //This frame will hold the a pixelPane for art creation. This will also hold a menu bar, defined in another file. pixelPane editingArea; pixelMenuBar menuBar; public pixelFrame() { EventQueue.invokeLater(new Runnable() { //create a new thread at runtime

        @Override
        public void run() {  //when the program runs, do the following
            JFrame frame = new JFrame("Pixel Art Creator");  //create a new JFrame (similar to a regular window), and give it the following title
            editingArea = new pixelPane();
            menuBar = new pixelMenuBar();
            frame.add(editingArea);  //add a pixelPane named editingArea to the JFrame
            frame.setJMenuBar(menuBar); //adds a menu to the JFrame
            frame.pack(); //make the window big enough to fit all components (in this case, editingArea)
            frame.setLocationRelativeTo(null); //set window to the center of the screen
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Stop the thread and terminate the program.
            frame.validate();
            frame.setVisible(true); //You can see the window.

            menuBar.setVisible(true);
        }

    });
}

public static void main(String[] args)
{
    pixelPane.gridEnabled = true; //changing another variable from pixelPane just for testing.
    new pixelFrame(); //create an instance of pixelFrame.
}

}

And here's the menu bar:

public class pixelMenuBar extends JMenuBar {
/**
 * 
 */
private static final long serialVersionUID = 1L;
JMenuBar menuBar;
JMenu file, tool;
JMenuItem save, load, changeColor;
JCheckBoxMenuItem gLines;
public pixelMenuBar() {
    menuBar = new JMenuBar();

    //creates a "File" menu in the menu bar
    file= new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    menuBar.add(file); //add the menu to the menu bar

    //creates a "Tools" menu in the menu bar
    tool = new JMenu("Tools");
    tool.setMnemonic(KeyEvent.VK_T);
    menuBar.add(tool);


    //Menu items that go under the File menu
    save = new JMenuItem("Save File"); //save and export the image as an .svg file
    save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_S)); //a Ctrl+S hotkey. Handy dandy!
    file.add(save);//adds the Save button to the File menu

    load = new JMenuItem("Load File"); //load the file into BufferedImage, make it into a JLabel and add it to the panel.
    load.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_L));//a Ctrl+L hotkey. Also handy dandy!
    file.add(load);//adds the Load button to the File menu


    //Menu items that will go under the Tools menu
    gLines = new JCheckBoxMenuItem("Gridlines"); //creates a new checkbox for enabling grid lines. will toggle pixelPane.gridEnabled
    gLines.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_G));
    tool.add(gLines);//adds the gridlines tool to the Tools menu
    tool.addSeparator(); //adds a separating line in the Tools menu. Organization.

    changeColor = new JMenuItem("Change Color");//creates a new menu item that will let the user the color of the pixel. Uses a color picker
    tool.add(changeColor);//adds Change Color to the Tools menu
}

}

Am I not adding something to the UI correctly? I double checked that I'm using addJMenuBar() and not addMenuBar().


Solution

  • First of all class names SHOULD start with an upper case character. Have you ever seen a class in the Java API that doesn't? Follow Java conventions. Learn by example.

    public class pixelMenuBar extends JMenuBar 
    {
    ...
    public pixelMenuBar() 
    {
        menuBar = new JMenuBar();
    

    Your class "is a " JMenuBar, but the first thing you do in the class is create a JMenuBar.

    Don't create the JMenuBar!

    Just add the menu items to the JMenuBar class itself:

    //menuBar.add(file); //add the menu to the menu bar
    add(file); //add the menu to the menu bar
    

    In reality there is no need to even have a PixelMenuBar class, since you are not adding any new functionality to the JMenuBar. Just add a method to your main class like createMenuBar(...) that creates the JMenuBar and adds the JMenu/JMenuItem objects.