Vincent Roye
Vincent Roye

Reputation: 2851

jPopUpMenu Change rollover background

I would like to change the background color of my jMenuItems on a rollover event :

enter image description here

Now it's blue, I want it white, how can I do ? (I use netbeans GUI)

Upvotes: 0

Views: 1983

Answers (2)

DMM
DMM

Reputation: 11

I wanted to be able to use different colors in different parts of an app. Rather than use UIManager to change the LookAndFeel, I extended javax.swing.plaf.basic.BasicMenuItemUI:-

public class CustomMenuUI extends BasicMenuItemUI {
    public CustomMenuUI(Color color){
        super.selectionBackground = color;
    }
}

Then you just need to set the UI for your JMenuItem:-

CustomMenuUI menuUI = new CustomMenuUI(Color.WHITE);
jMenuItem.setUI(menuUI);

Upvotes: 1

camickr
camickr

Reputation: 324128

You can use something like:

UIManager.put("MenuItem.selectionBackground", Color.WHITE);

Problem is this will change the color for all menu items, even those added to a JMenu.

To control which menu items are changed you will need to create a custom MenuItem UI to replace the default. You will then need to modify the code to use your custom selection background color.

Upvotes: 3

Related Questions