I think my question is quite objective: how to remove the hover effect from menu items and buttons? Here is an image of the hover effect that I want to remove (button and menu items):
This depends a lot on the Look and Feel (LAF) that is being used in the application, since it is usually the one that defines this type of feature of the components.
If you have not modified what java defines by default (which is called "Metal"), the simplest way would be to change the specific properties of this LAF:
UIManager.put("MenuItem.selectionBackground", UIManager.getColor("MenuItem.background"));
UIManager.put("MenuItem.selectionForeground", UIManager.getColor("MenuItem.foreground"));
Actually, this is a "workaround" where I set the same background colors and foreground colors for the colors that would be applied when JMenuItem
is selected. This causes the effect below:
ItisalsopossibletocreatetheUIforJMenuItem
manually,butIpersonallyfinditquitecomplextodo,anditispossibletochangewithonlytwolines.
IfyouusethesystemLAFwheretheapplicationisrunning,oruseanotheronesuchas < Nimbus , there you have no choice but to do what I quoted in the previous paragraph, as the suggested changes will not work for these two cases.
And for JButtons
, you can use the setRolloverEnabled()
, but it will end up falling into the same problem, because depending on the LAF used, it can simply ignore this command and not remove the effect.