4

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'

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Primm
  • 1,347
  • 4
  • 19
  • 32

2 Answers2

6

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);

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    JMenuBar#setBackground does not set the background color of a JMenuBar. – FThompson Aug 09 '12 at 23:22
  • @Vulcan well, what'd you know, learning something every day :) – MadProgrammer Aug 09 '12 at 23:25
  • @user1332495 Just be careful, it might not work an all Look and Feel implementations, I think that's what Vulcan was trying to say – MadProgrammer Aug 09 '12 at 23:27
  • 1
    It didn't work in a test I just ran, but correct, I was using a custom L&F. I will remove the downvote as soon as I can. Sorry about that. – FThompson Aug 09 '12 at 23:28
  • @Vulcan, I couldn't get it take change under the Windows L&F so you're not entirely incorrect ;) – MadProgrammer Aug 09 '12 at 23:29
  • @MadProgrammer Ah, I just tested with default Windows 7 L&F and it worked, but your second solution is far safer and works in more cases. – FThompson Aug 09 '12 at 23:33
  • Glad that was solved! (by that i mean both the question and dispute) – Primm Aug 09 '12 at 23:33
  • 1
    BasicMenuUI returns array of Colors, UImanager.put() or Painter<> – mKorbel Aug 10 '12 at 08:13
  • @mKorbel I was trying to look into that. Better idea – MadProgrammer Aug 10 '12 at 08:19
  • @MadProgrammer do not to take my comments as attack to your person, I leaving my comments for most posters here, sure and I know how to down_vote too, but your progress (on this forum) is excellent – mKorbel Aug 10 '12 at 08:24
  • @mKorbel Never do and I appreciate the feedback, your suggestion on the BasicMenuUI is an excellent one, takes less effort and is backwards compatible, what's to dislike ;) - I also like to get a different perspective on these problems as well – MadProgrammer Aug 10 '12 at 08:26
  • be sure I hate that too (Painter in only one way how to do for Nimbus), this is reason why a few good custom look and feels is there, why bothering with that :-) – mKorbel Aug 10 '12 at 08:28
6

With MadProgrammer's approach you will get menubar background painted twice - once by the UI (it could be gradient on Windows for example, which takes some time to paint) and once by your code in paintComponent method (atop of the old background).

Better replace menubar UI by your own one based on BasicMenuBarUI:

    menuBar.setUI ( new BasicMenuBarUI ()
    {
        public void paint ( Graphics g, JComponent c )
        {
            g.setColor ( Color.RED );
            g.fillRect ( 0, 0, c.getWidth (), c.getHeight () );
        }
    } );

You can also set that UI globally for all menubars so that you don't need to use your specific component each time you create menubar:

UIManager.put ( "MenuBarUI", MyMenuBarUI.class.getCanonicalName () );

MyMenuBarUI class here is your specific UI for all menubars.

Mikle Garin
  • 10,083
  • 37
  • 59
  • wooohooo till now I saw Xxx.class.getCanonicalName() second time, great desing +1 – mKorbel Aug 10 '12 at 16:10
  • @mKorbel thanks, and i really don't understand why most of the developers hardcode the class names (even if they re not going to be changed ever). – Mikle Garin Aug 10 '12 at 20:53
  • I've tried your second approach but somehow doesn't have any effect. Where should I look for the problem? – Czechnology Aug 04 '13 at 11:44
  • 1
    @Czechnology You might not have a special static method "createUI" added in your UI class so it doesn't affect the UI at all. This is the most common mistake when creating a custom UI. – Mikle Garin Aug 04 '13 at 20:42