setJMenuBar not working

Viewed 639

After extensive googling I've found that this issue is generally case-specific. I've tried many of the solutions found and none of them have worked for me, so I felt it was appropriate to create a post on it.

I'm using setJMenuBar and the bar is never appearing, here is my code:

NOTE: I believe the cause is that I update the frame, however the solution is still unknown.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

@SuppressWarnings("serial")
public class testclass extends JFrame { 


    public int updateRate = 60;

    public int screenWidth;
    public int screenHeight;

    public boolean finishedInitialPaint;

    private Graphics bufferGraphics;
    private BufferStrategy bufferStrategy;

    public int gridSize = 10;

    JMenuBar engineBar;
    JMenu fileMenu, editMenu, createMenu, toolsMenu, panelsMenu;
    JMenuItem fileNewScene, fileOpenScene, fileSaveScene, fileExit;
    JMenuItem editPreferences, editEngine;
    JMenuItem createEmpty, createStandard;
    JMenuItem toolsDebug;
    JMenuItem panelView2D, panelEditor2D, panelHierarchy, panelInspector, panelExplorer;

    public static void main(String[] args) {
        testclass testClassv = new testclass ();
    }

    public testclass () {
        Start ();

        while(true) {
            Update ();
            ClearBackBuffer ();
            try {
                Thread.sleep(1000 / updateRate);
            } catch (InterruptedException e) {

            }
        }

    }

    private void Start () {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        screenWidth = screenSize.width;
        screenHeight = screenSize.height;

        CreateDefaultEngineMenu ();

        setTitle("Test Window");
        setSize(Math.round(screenWidth * 0.99f), Math.round(screenHeight * 0.9f));
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void CreateDefaultEngineMenu () {
        engineBar = new JMenuBar ();

        /*
         * Building initial menus
         */

    // Build the file menu
        fileMenu = new JMenu("File");
        engineBar.add(fileMenu);

    // Build the edit menu
        editMenu = new JMenu("Edit");
        engineBar.add(editMenu);

    // Build the create menu
        createMenu = new JMenu("Create");
        engineBar.add(createMenu);

    // Build the tools menu
        toolsMenu = new JMenu("Tools");
        engineBar.add(toolsMenu);

    // Build the panels menu
        panelsMenu = new JMenu("Panels");
        engineBar.add(panelsMenu);

        /*
         *  Building menu options
         */

    // File menu options
        fileNewScene = new JMenuItem("New Scene");
        fileMenu.add(fileNewScene);
        fileOpenScene = new JMenuItem("Open Scene");
        fileMenu.add(fileOpenScene);
        fileSaveScene = new JMenuItem("Save Scene");
        fileMenu.add(fileSaveScene);
        fileExit = new JMenuItem("Exit");
        fileMenu.add(fileExit);

    // Edit menu options
        editPreferences = new JMenuItem ("Preferences");
        editMenu.add(editPreferences);
        editEngine = new JMenuItem ("Engine Settings");
        editMenu.add(editEngine);

    // Create menu options
        createEmpty = new JMenuItem("New Empty GameObject");
        createMenu.add(createEmpty);
        createStandard = new JMenuItem("Standard GameObjects");
        createMenu.add(createStandard);

    // Tools menu options
        toolsDebug = new JMenuItem ("Debug");
        toolsMenu.add(toolsDebug);

    // Panels menu options
        panelView2D = new JMenuItem("View 2D");
        panelsMenu.add(panelView2D);
        panelEditor2D = new JMenuItem("Editor 2D");
        panelsMenu.add(panelEditor2D);
        panelHierarchy = new JMenuItem("Hierarchy");
        panelsMenu.add(panelHierarchy);
        panelInspector = new JMenuItem("Inspector");
        panelsMenu.add(panelInspector);
        panelExplorer = new JMenuItem("Explorer");
        panelsMenu.add(panelExplorer);


        // Set the frame to use the bar
        setJMenuBar(engineBar);
    }

    private void Update () {
        //sceneObjects.activeObjects.get(0).transform.position.x += 0.1f;
        initialize ();
        ClearBackBuffer ();
        repaint ();
        DrawBackBufferToScreen ();
    }

    private void initialize () {
        if(bufferStrategy == null) {
            this.createBufferStrategy(2);
            bufferStrategy = this.getBufferStrategy();
            bufferGraphics = bufferStrategy.getDrawGraphics();
        }
    }

    public void paint(Graphics g) {
        // Something to go here
    }

    private void ClearBackBuffer () {
        bufferGraphics = bufferStrategy.getDrawGraphics();
        try {
            bufferGraphics.clearRect(0, 0, this.getSize().width, this.getSize().height);
            Graphics2D g2d = (Graphics2D)bufferGraphics;
            paint2D(g2d);
        } catch (Exception e) {
            //Debug.EngineLogError("Engine", "Failed to clear rect for frame buffer (Failed to draw frame) - Exception");
        } finally {
            bufferGraphics.dispose();
        }
    }

    private void DrawBackBufferToScreen () {
        bufferStrategy.show();
        Toolkit.getDefaultToolkit().sync();
    }

    private void paint2D (Graphics2D g2d) {
        // Multi-Used Variables
        int canvasWidth = Math.round(screenWidth / 2);
        int canvasHeight = Math.round(screenHeight / 2);

        // Draw panel borders
        // View2D
        g2d.setColor(Color.black);
        g2d.drawRect((canvasWidth) - (canvasWidth / 2) - 1, canvasHeight - (Math.round(canvasHeight / 1.35f)) - 1, canvasWidth + 1, canvasHeight + 1);
    }

}

Any and all relevant answers are greatly appreciated.

EDIT: Fixed code to be able to be tested raw

1 Answers

There's a lot wrong in your code, but the biggest problem is here:

public void paint(Graphics g) {
    // Something to go here
}

By overriding paint, you're preventing the JFrame from doing its necessary painting of itself, its borders, and relevant to your problem, its child components. So your menu never draws!

You should never draw directly in the JFrame, but if you absolutely must (and you don't), at least call the super's painting method:

public void paint(Graphics g) {
    super.paint(g);
    // Something to go here
}

Better still-- never override paint but instead draw inside a JPanel's paintComponent method (still calling the super's paintComponent method within) and display that JPanel within your JFrame.

Key links:


I'm not sure what the rest of your code is trying to do, but you've got big threading issues, other painting issues,... do read the tutorials to avoid guessing. Also you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.

Related