4

This seems like it should be straightforward, but I haven't found a "good" way to do it...

While using the Swing Nimbus L&F, I want to give my controls (JButtons, JTextField, etc...) a yellow background color when they have focus. Other than the yellow background color, I want them to keep all the usual Nimbus styling.

When not focused, I want them to be drawn with the normal Nimbus styling.

The only way I've found to do this is to rewrite the control[Focused].backgroundPainter for every single control (which would amount to rewriting a large part of Nimbus from scratch).

Am I missing something? Thanks!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1422004
  • 41
  • 1
  • 2

1 Answers1

8

Nimbus Default provide simple matrix for Nimbus Look and Feel, but required to override all related Mouse and Focus events, without overriding ... could be only,

enter image description hereenter image description hereenter image description here

from code

import com.sun.java.swing.Painter;
import java.awt.*;
import javax.swing.*;

public class NimbusJPanelBackGround {

    public NimbusJPanelBackGround() {
        JButton btn = new JButton("  Whatever  ");
        JButton btn1 = new JButton("  Whatever  ");
        JPanel p = new JPanel();
        p.add(btn);
        p.add(btn1);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.add(p, BorderLayout.CENTER);
        f.pack();
        f.setLocation(150, 150);
        f.setVisible(true);
    }

    public static void main(String[] args) {

        try {
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                    UIManager.getLookAndFeelDefaults().put("Panel.background", Color.white);
                    UIManager.getLookAndFeelDefaults().put("nimbusFocus", Color.blue);
                    UIManager.getLookAndFeelDefaults().put("Button[Focused+MouseOver].backgroundPainter",
                            new FillPainter(new Color(127, 255, 191)));
                    UIManager.getLookAndFeelDefaults().put("Button[MouseOver].backgroundPainter",
                            new FillPainter(new Color(127, 255, 191)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                NimbusJPanelBackGround nimbusJPanelBackGround = new NimbusJPanelBackGround();
            }
        });
    }
}

class FillPainter implements Painter<JComponent> {

    private final Color color;

    public FillPainter1(Color c) {
        color = c;
    }

    @Override
    public void paint(Graphics2D g, JComponent object, int width, int height) {
        g.setColor(color);
        g.fillRect(0, 0, width - 1, height - 1);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    Thanks, but I'm afraid this does what I'm trying to avoid: Namely, it eliminates all the usual Nimbus styling. I want the JButton to keep the normal size, rounded corners, blue focus-border, etc... I just want its body to be yellow-filled when it has focus instead of the standard grey gradient. – user1422004 May 28 '12 at 19:49
  • @user1422004 I'm not going to coding that, about that is my comment, but if you need Nimbus, then there are some Custom Look and Feels based on Nimbus with Color themes and/or some customizations – mKorbel May 28 '12 at 20:07