1

I have a JButton in my Nimbus L&F setting with MouseListener. I was able to change the text color when mouse enter but cannot change the color back or to another color when mouse exit. From print out I am sure the mouseExit is called.

Anybody can help me figure out why?

I am thinking after the mouseEntered() is called, the btn_f is not Nimbus L&F anymore. I don't know my guess makes sense or not.

The code likes this:

@Override
    public void mouseEntered(MouseEvent e) {
        Object resource = e.getSource();
        if(resource == btn_f)
        {
            System.out.println("@@@@@@@@@@@@@====btn_f is enterred");
            UIDefaults defaults = new UIDefaults();
            defaults.put("Button.textForeground", Color.BLUE);
            btn_f.putClientProperty("Nimbus.Overrides", defaults);
            //btn_f.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
            //SwingUtilities.updateComponentTreeUI(btn_f);

        }
    }

    @Override
    public void mouseExited(MouseEvent e) {

        Object resource = e.getSource();
        if(resource == btn_f)
        {
            System.out.println("@@@@@@@@@@@@@====btn_f is exited");
            UIDefaults defaults = new UIDefaults();
            defaults.put("Button.textForeground", Color.RED);
            btn_f.putClientProperty("Nimbus.Overrides", defaults);
            //btn_f.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
            //SwingUtilities.updateComponentTreeUI(btn_f);
        }
    }
peterboston
  • 877
  • 1
  • 12
  • 24
  • [use ButtonModel and events from ChangeListener](http://stackoverflow.com/a/5755124/714968), have to check if is required to call [UIManager.getLookAndFeel().uninitialize();](http://stackoverflow.com/a/10686898/714968) – mKorbel Jul 11 '15 at 11:30

1 Answers1

0

A simple btn_f.setForeground( theColor ); works in both enter and mouseEntered() and mouseExited().

FredK
  • 4,094
  • 1
  • 9
  • 11