0

I am trying to add a subclass of JMenuBar to a user interface but for some reason it never shows up. I have tried using JFrame.setJMenubar() and JFrame.add(), i have tried adding it from within a SwingUtilities.invokeLater() call etc... It still worked when in was using JMenuBar itself instead of a subclass so i suspect it has something to do with that.

This is the code that initializes the application window:

public DramaSimWindow() {
    initializeSelf();
    initializeContainers();
    this.setVisible(true);
}

private void initializeSelf() {
    initializeContentPane();
    this.setBounds(100, 100, 800, 500);
    this.setJMenuBar(new DramaSimMenuBar());
    this.setResizable(false);
}

This is the subclass of JMenuBar which is located inside the main window class as a private class:

private class DramaSimMenuBar extends JMenuBar {

    private static final long serialVersionUID = 1L;

    public DramaSimMenuBar() {
        initializeSelf();
    }

    private void initializeSelf() {
        menuBar = new JMenuBar();
        initializeFileMenu();
        initializeEditMenu();
    }

    private void initializeFileMenu() {
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(new JMenuItem("New"));
        fileMenu.add(new JMenuItem("Open"));
        fileMenu.add(new JMenuItem("Save"));
        fileMenu.add(new JMenuItem("Save as"));
        fileMenu.add(new JMenuItem("Exit"));
        menuBar.add(fileMenu);
    }

    private void initializeEditMenu() {
        JMenu editMenu = new JMenu("Edit");
        editMenu.add(new JMenuItem("Copy"));
        editMenu.add(new JMenuItem("Cut"));
        editMenu.add(new JMenuItem("Paste"));
        menuBar.add(editMenu);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Exevan
  • 335
  • 1
  • 3
  • 24

2 Answers2

6

You shouldn't extend JMenuBar in the first place. Just create and use a JMenuBar instead of extending it. BTW, the problem is that your JMenuBar subclass doesn't add menus to itself, but to another JMenuBar it creates:

private void initializeSelf() {
    menuBar = new JMenuBar();
    ...
    menuBar.add(fileMenu);

should be

private void initializeSelf() {
    ...
    this.add(fileMenu);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

Why should you create an JMenuBar, if your base class is extends from JMenuBar, so the correct code below: private class DramaSimMenuBar extends JMenuBar {

private static final long serialVersionUID = 1L;

public DramaSimMenuBar() {
    initializeSelf();
}

private void initializeSelf() {
    initializeFileMenu();
    initializeEditMenu();
}

private void initializeFileMenu() {
    JMenu fileMenu = new JMenu("File");
    fileMenu.add(new JMenuItem("New"));
    fileMenu.add(new JMenuItem("Open"));
    fileMenu.add(new JMenuItem("Save"));
    fileMenu.add(new JMenuItem("Save as"));
    fileMenu.add(new JMenuItem("Exit"));
    add(fileMenu);
}

private void initializeEditMenu() {
    JMenu editMenu = new JMenu("Edit");
    editMenu.add(new JMenuItem("Copy"));
    editMenu.add(new JMenuItem("Cut"));
    editMenu.add(new JMenuItem("Paste"));
    add(editMenu);
}

}

Igor Grishkov
  • 83
  • 1
  • 8