0

Despite instantiating my custom JMenubar, using myFrame.setJMenuBar(customJMenuBar);, myFrame.validate(); and myFrame.repaint(); my custom menu bar is not displaying.

I want to display the same menu bar component across multiple frames, so I created a custom menu bar that can be displayed (and modified in the future) as required. However, when I debug I can see that the custom menu bar has been added to the JFrame but it is not displaying as either the standard or preferred size.

My frame class: MainMenuScreen

public class MainMenuScreen extends javax.swing.JFrame {
/**
 * Creates new form MainMenuScreen
 */
public MainMenuScreen() {
    initComponents();
    this.displayFileMenuBar();
} 

 /**
* Create new GenerationMenuBar
* Attach to MainMenuScreen
*/ 
private void displayFileMenuBar()
{
    createdMenuBar = new GenerationMenuBar(this.designMenu);
    this.setJMenuBar(createdMenuBar);
    this.validate();
    this.repaint();
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MainMenuScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MainMenuScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainMenuScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainMenuScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    //</editor-fold>

    /* Create and display the form */
   java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new MainMenuScreen().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton DesignExperimentButton;
private javax.swing.JPanel MainMenuPanel;
private javax.swing.JButton PresentExperimentButton;
private javax.swing.JMenuBar designMenu;
// End of variables declaration                   
}

My custom JMenuBar class: GenerationMenuBar

import java.awt.Dimension;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

/**
 *
 * @author Marion Grenfell-Essam
 */
public class GenerationMenuBar extends JMenuBar {

// GUI Attributes
private JMenuBar generationMenu;
private JMenu editDesignMenu;
private JMenu fileDesignMenu;
private JMenuItem newDesignMenuItem;
private JMenuItem openDesignMenuItem;
private JMenuItem saveAsMenuItem;
private JMenuItem saveDesignMenuItem;
private JMenuItem undoDesignMenuItem;

public GenerationMenuBar(JMenuBar designMenu){
    initComponents(designMenu);
}

private void initComponents(JMenuBar sentMenuBar) {
    this.setMenuBar(sentMenuBar);
    this.getMenuBar().setPreferredSize(new Dimension(1024, 25));
    fileDesignMenu = new JMenu();
    newDesignMenuItem = new JMenuItem();
    openDesignMenuItem = new JMenuItem();
    saveDesignMenuItem = new JMenuItem();
    saveAsMenuItem = new JMenuItem();
    editDesignMenu = new JMenu();
    undoDesignMenuItem = new JMenuItem();

    fileDesignMenu.setText("File");

    newDesignMenuItem.setText("New");
    newDesignMenuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            newDesignMenuItemActionPerformed(evt);
        }
    });
    fileDesignMenu.add(newDesignMenuItem);

    openDesignMenuItem.setText("Open");
    fileDesignMenu.add(openDesignMenuItem);

    saveDesignMenuItem.setText("Save");
    fileDesignMenu.add(saveDesignMenuItem);

    saveAsMenuItem.setText("SaveAs");
    fileDesignMenu.add(saveAsMenuItem);

    this.getMenuBar().add(fileDesignMenu);

    editDesignMenu.setText("Edit");

    undoDesignMenuItem.setText("Undo");
    editDesignMenu.add(undoDesignMenuItem);

    this.getMenuBar().add(editDesignMenu);
}

// Getters
public JMenuBar getMenuBar()
{
    return generationMenu;
}

// Setters
public void setMenuBar(JMenuBar sentMenuBar)
{
    generationMenu = sentMenuBar;
}

// Event handlers
private void newDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    // TODO add your handling code here:
}   

private void openDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    // TODO add your handling code here:
}                                                  

private void saveDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    // TODO add your handling code here:
}                                                  

private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                               
    // TODO add your handling code here:
}                                              

private void undoDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    // TODO add your handling code here:
}      
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • The initComponents method is missing. The code posted in your question is not runnable, so I can't copy and paste the code into my Eclipse and see what's wrong. – Gilbert Le Blanc Aug 21 '16 at 12:08
  • 1
    `public class GenerationMenuBar extends JMenuBar {` If there is a reason for extending `JMenuBar` in that code, I missed it. Just use a standard instance of a menu bar. More generally: For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 21 '16 at 12:12
  • 2
    *"I want to display the same JMenuBar component across multiple frames"* 1) Just use a static factory method, but.. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Aug 21 '16 at 12:13

1 Answers1

2

You're creating two JMenuBars, not one, one you fill with menus, and the other you don't -- and guess which one you're adding to your JFrame?

Note that your class extends JMenuBar -- that's JMenuBar one, and that's the one you're adding to the JFrame.

The other JMenuBar is a field within this same class called generationMenu, and that's the one you're adding all the menus to but are not adding to the JFrame.

Solution: don't do this. Use one and only one JMenuBar. Myself, I'd make the class not extend JMenuBar, and then change this line:

this.setJMenuBar(createdMenuBar);

to:

this.setJMenuBar(createdMenuBar.getMenuBar());
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373