import javax.swing.*;
import java.awt.*;
class SelectionBackgroundJMenuItem extends JFrame
{
JMenuBar mbar;
JMenu menu;
JMenuItem m1,m2,m3,m4;
public SelectionBackgroundJMenuItem()
{
UIManager.put("MenuItem.selectionBackground",new Color(245,29,29));
UIManager.put("MenuItem.selectionForeground",Color.white);
setTitle("Colored Menu Item");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
mbar=new JMenuBar();
menu=new JMenu("Menu");
for(int i=1;i<=5;i++)
menu.add(new JMenuItem("Menu Item "+i));
mbar.add(menu);
setJMenuBar(mbar);
setSize(400,400);
setLocationRelativeTo(null);
}
public static void main(String args[])
{
new SelectionBackgroundJMenuItem();
}
}
Output
Output Window |
The respective properties are useful for setting backgrounds for MenuItem. The first word (before .) indicates the component (MenuItem here), after that the property. Note that not all components have this property.
UIManager.put("MenuItem.selectionBackground",new Color(245,29,29)):
This sets the selection background with the given color.UIManager.put("MenuItem.selectionForeground",Color.white):
This sets the selection foreground with given color (white).