How can I set custom background color for JMenuBar
and JMenu
objects inside it? I tried .setBackgroundColor
and it does not work!
Create a new class that extends JMenuBar
:
public class BackgroundMenuBar extends JMenuBar {
Color bgColor=Color.WHITE;
public void setColor(Color color) {
bgColor=color;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(bgColor);
g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
}
}
Now you use this class instead of JMenuBar
and set the background color with setColor()
.
You would probably need to change opacity of menu items, ie:
JMenuItem item= new JMenuItem("Test");
item.setOpaque(true);
item.setBackground(Color.CYAN);
You can also achieve that globally using UIManager
, for example:
UIManager.put("MenuItem.background", Color.CYAN);
UIManager.put("MenuItem.opaque", true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With