How to change the color of a Jmenubar?

Asked

Viewed 1,845 times

1

Is there any way to change the color of background and foreground from the java menu bar?

I searched several forums and tutorials and could not find a way to change the menu bar color.

Following example:

public class menu extends javax.swing.JFrame {


public menu() {
    initComponents();
    jMenuBar2.setBackground(Color.BLACK);
    jMenuBar2.setForeground(Color.WHITE);
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jMenuBar2 = new javax.swing.JMenuBar();
    jMenu3 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jMenuItem2 = new javax.swing.JMenuItem();
    jMenuItem3 = new javax.swing.JMenuItem();
    jMenuItem4 = new javax.swing.JMenuItem();
    jMenuItem5 = new javax.swing.JMenuItem();
    jMenuItem6 = new javax.swing.JMenuItem();
    jMenu4 = new javax.swing.JMenu();
    jMenuItem7 = new javax.swing.JMenuItem();
    jMenuItem8 = new javax.swing.JMenuItem();
    jMenuItem9 = new javax.swing.JMenuItem();
    jMenuItem10 = new javax.swing.JMenuItem();
    jMenuItem11 = new javax.swing.JMenuItem();
    jMenuItem12 = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jMenu3.setText("File");

    jMenuItem1.setText("jMenuItem1");
    jMenu3.add(jMenuItem1);

    jMenuItem2.setText("jMenuItem1");
    jMenu3.add(jMenuItem2);

    jMenuItem3.setText("jMenuItem1");
    jMenu3.add(jMenuItem3);

    jMenuItem4.setText("jMenuItem1");
    jMenu3.add(jMenuItem4);

    jMenuItem5.setText("jMenuItem1");
    jMenu3.add(jMenuItem5);

    jMenuItem6.setText("jMenuItem1");
    jMenu3.add(jMenuItem6);

    jMenuBar2.add(jMenu3);

    jMenu4.setText("Edit");

    jMenuItem7.setText("jMenuItem7");
    jMenu4.add(jMenuItem7);

    jMenuItem8.setText("jMenuItem7");
    jMenu4.add(jMenuItem8);

    jMenuItem9.setText("jMenuItem7");
    jMenu4.add(jMenuItem9);

    jMenuItem10.setText("jMenuItem7");
    jMenu4.add(jMenuItem10);

    jMenuItem11.setText("jMenuItem7");
    jMenu4.add(jMenuItem11);

    jMenuItem12.setText("jMenuItem7");
    jMenu4.add(jMenuItem12);

    jMenuBar2.add(jMenu4);

    setJMenuBar(jMenuBar2);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 548, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 376, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/**
 * @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(menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new menu().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenuBar jMenuBar2;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem10;
private javax.swing.JMenuItem jMenuItem11;
private javax.swing.JMenuItem jMenuItem12;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JMenuItem jMenuItem9;
// End of variables declaration 


}
  • Depends, which LAF you are using?

  • I’m using the LAF Nimbus.

  • Just change the color of the slider, will not touch the colors of the items with and without non ne selection?

  • I would like to put it black, with the white foreground the items in the same way, when I apply the methods nothing happens, I would like to highlight the selection of the item and the selected menu..

  • And when the items are selected?

  • I would like to change so that it is compatible with the color of the bar type a gray in the selected item with the color of the font in black, I do not know if it is possible..

  • Black background with gray selection?

  • Yeah, that’s the way it is...

  • I’ll give you a heads-up, this is a lot of work to do, and you’re going to miss out on some of the appearance of Nemos, like the gradients. The least laborious way to do this is by removing the appearance of Nimbus and changing the opacity, but it will look like those menus in windows 98. It makes a difference to you?

  • I’d need to see this one less laborious to get an idea. Veses don’t pay off..

  • Hold on I’ll post an example using your code.

Show 6 more comments

1 answer

2


The simplest way I found is to overwrite the method paint() class BasicMenuBar, and setting it up for your JMenuBar. To change the desired color, edit the line g.setColor(Color.black);.

This could also be done for each of the menus and menuItens, but to avoid over-repetition, I made a method called customizeMenuBar(), who gets a guy JMenuBar and changes the menu bar, the menus contained in it and its sub-items, according to the colors passed to another method called changeComponentColors().

private void customizeMenuBar(JMenuBar menuBar) {

    menuBar.setUI(new BasicMenuBarUI() {

        @Override
        public void paint(Graphics g, JComponent c) {
            g.setColor(Color.black);
            g.fillRect(0, 0, c.getWidth(), c.getHeight());
        }

    });

    MenuElement[] menus = menuBar.getSubElements();

    for (MenuElement menuElement : menus) {

        JMenu menu = (JMenu) menuElement.getComponent();
        changeComponentColors(menu);
        menu.setOpaque(true);

        MenuElement[] menuElements = menu.getSubElements();

        for (MenuElement popupMenuElement : menuElements) {

            JPopupMenu popupMenu = (JPopupMenu) popupMenuElement.getComponent();
            popupMenu.setBorder(null);

            MenuElement[] menuItens = popupMenuElement.getSubElements();

            for (MenuElement menuItemElement : menuItens) {

                JMenuItem menuItem = (JMenuItem) menuItemElement.getComponent();
                changeComponentColors(menuItem);
                menuItem.setOpaque(true);

            }
        }
    }
}

private void changeComponentColors(Component comp) {
    comp.setBackground(Color.black);
    comp.setForeground(Color.white);
}

Just add the two methods in the class, and call the method customizeMenuBar() passing his JMenuBar, soon after the bar has been built, in your case, after the initComponents():

initComponents();
customizeMenuBar(jMenuBar2);

The result will look like this:

inserir a descrição da imagem aqui

I leave the links below, in case you want a more specific customization of the LAF Nimbus, it is not an easy task, because even the oracle itself guarantees that the defaults are actually the ones described.


References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.