Search code examples
javaswingcolorsbackgroundjmenubar

set JMenuBar's background color?


relatively straight-forward, how can I set the background color of a JMenuBar?

ive tried:

MenuBar m = new MenuBar() {

      void paintComponent(Graphics g) {

  Graphics2D g2 = (Graphics2D)g;
  g2.setBackground(Color.yellow);
  g2.fillRect(0, 0, getWidth(), getHeight());
}

but nothin'


Solution

  • Well, to start with, what you've shown is not a JMenuBar, it's MenuBar, there's a significant difference. Try using a JMenuBarand use setBackground to change the background color

    Updated from feedback from Vulcan

    Okay, in the cases where setBackground doesn't work, this will ;)

    public class MyMenuBar extends JMenuBar {
    
        @Override
        protected void paintComponent(Graphics g) {
    
            super.paintComponent(g);
    
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.RED);
            g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
    
        }
    
    }