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:
}
}