How to change color of JMenuItem when mouse is over it
Title says it all, I want to change background and foreground color's of JMenuItem so that normal background is blue and when I put my mouse over it, it turns to green. once I move my mouse away it moves back to blue. I tried with Mouse Listener and with MouseDraggedListener and neither have worked.
If there is a solution please let me know.
Also another question is a similar one involving tabs. How do I change background and foreground of selected tab. I managed to change background of selected tab with this command
Code :
UIManager.put("TabbedPane.selected", Color.Green);
but can't seem to change the Foreground.
Any help is appreciated
Regards,
Re: How to change color of JMenuItem when mouse is over it
I'm just thinking out loud here, but does any generic JMenuItem react in any way when you just hover your mouse over it (highlights or something)? If so, that would be an indicator that JMenuItem has a built in listener to process a mouse hovering over it. If it has a built in listener by default, we just need to figure out what type of listener it is an override it to do what you want.
Re: How to change color of JMenuItem when mouse is over it
When mouse hovers over background color and foreground color change. Text becomes black, background becomes very light_blueish. This is the default and I have no idea how to overwrite that. With mouse listeners I tried using print statements but nothing happened.
Edit: Ok using mouse listener I managed to get color to change, I added so that color changes when mouse enters. While mouse hovers color is the same old but when mouse moves away it goes to green. So i'm assuming I need to over-wright hover over mouse function somehow, any ideas?
Re: How to change color of JMenuItem when mouse is over it
If you wish this to apply to all JMenuItems across your application, use the UIManager to set the look and feel defaults for MenuItem.selectionBackground and MenuItem.background.
...otherwise, JMenuItem extends JComponent so anything applies there (such as add listeners and setting its background/foreground)
Re: How to change color of JMenuItem when mouse is over it
Solution:
Code :
UIManager.put("TabbedPane.selected", Color.GREEN);
UIManager.put("MenuItem.selectionBackground", Color.GREEN);
UIManager.put("MenuItem.selectionForeground", Color.BLUE);
UIManager.put("Menu.selectionBackground", Color.GREEN);
UIManager.put("Menu.selectionForeground", Color.BLUE);
UIManager.put("MenuBar.selectionBackground", Color.GREEN);
UIManager.put("MenuBar.selectionForeground", Color.BLUE);
Hope this helps people who search for same problem, just note that you have to implement UIManager before you initialize your JMenu and its components.
On final note what would the command be to change TabbedPane selected Foreground? I couldn't figure that one out.
Re: How to change color of JMenuItem when mouse is over it
Quote:
Originally Posted by
Bagzli
On final note what would the command be to change TabbedPane selected Foreground? I couldn't figure that one out.
For future reference, you can iterate over the UIDefaults to check the UI options available and search for the specific key you need to set the UI default value
Code :
UIDefaults ui = UIManager.getLookAndFeel().getDefaults();
for ( Object o : ui.keySet() ){
System.out.println(o.toString());
}
The above code will print the keys available, which you can then search for the appropriate syntax to use for the value you need