ADS

Set Selection Background, Foreground For Jmenu

 The following illustrates setting selection background Set Selection Background, Foreground for JMenu
The following illustrates setting selection background, foreground for JMenu in java swing. This is simple and involves 2 lines of important code. Here we go.







 import javax.swing.*;
import java.awt.*;
class SelectionBackgroundJMenu extends JFrame
{
JMenuBar mbar;
JMenu menu;
JMenuItem m1,m2,m3,m4;

public SelectionBackgroundJMenu()
{

UIManager.put("Menu.selectionBackground",new Color(245,29,29));
UIManager.put("Menu.selectionForeground",Color.white);

setTitle("Colored Menu");
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 SelectionBackgroundJMenu();
}
}

Output

 The following illustrates setting selection background Set Selection Background, Foreground for JMenu

The respective properties are useful for setting selection backgrounds for JMenu. The first word (before .) indicates the component (Menu here), after that the property. Note that not all components have this property.

UIManager.put("Menu.selectionBackground",new Color(245,29,29)): This sets the selection background with the given color.
UIManager.put("Menu.selectionForeground",Color.white): This sets the selection foreground with given color (white).

Subscribe to receive free email updates:

ADS