Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

java - Override Swing Nimbus L&F primary color per component instance

i’m trying to override the “nimbusBase” color for specific instance of JButton/JTabbedPane with no luck. only the specific attributes of the component e.g. “Button.background”, are working. any idea?

UIDefaults dialogTheme = new UIDefaults();
// dialogTheme.put(“nimbusBase”, Color.orange);
// dialogTheme.put("nimbusBlueGrey", Color.blue);
dialogTheme.put("Button.background", Color.yellow);
JButton dialogButton = new JButton("North");
dialogButton.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
dialogButton.putClientProperty("Nimbus.Overrides", dialogTheme);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can put the Color to the LookAndFeelDefaults for all Components "nimbusBase"

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("nimbusBase", new ColorUIResource(210, 0, 210));

Or to a specific Component as you wrote it. Perhaps with update the componentTreeUI.

...
dialogTheme.put("Button.background", new ColorUIResource(0, 0, 210));
...
dialogButton.putClientProperty("Nimbus.Overrides", dialogTheme);
SwingUtilities.updateComponentTreeUI(dialogButton); 

I never override "nimbusBase" for a specific Component.

I found a nice Nimbus Theme Creator, which can show the effect of changing a Nimbus Default Color to all Components: http://aephyr.googlecode.com/svn/trunk

EDIT

You can 'copy' the Painter, modify them and set them via UIDefaults:

Example GUI with an nimbusOrange JTabbedPane and a normal one. enter image description here

public class Example {

    static public void main( String[] s ) throws Exception {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }

        EventQueue.invokeLater( new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.getContentPane().setLayout( new BorderLayout() );
                frame.setBounds( 50, 50, 600, 600 );
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

                JTabbedPane pane = new JTabbedPane();
                pane.addTab( "Lorem", new JLabel("Lorem") );
                pane.addTab( "Ipsum", new JLabel("Ipsum") );
                pane.addTab( "Dolor", new JLabel("Dolor") );
                pane.addTab( "Sit", new JLabel("Sit") );
                pane.addTab( "Amet", new JLabel("Amet") );

                UIDefaults dialogTheme = new UIDefaults();
                dialogTheme.put("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter", new Painter(Painter.BACKGROUND_ENABLED));
                dialogTheme.put("TabbedPane:TabbedPaneTab[Disabled].backgroundPainter", new Painter(Painter.BACKGROUND_DISABLED));
                dialogTheme.put("TabbedPane:TabbedPaneTab[Enabled+MouseOver].backgroundPainter", new Painter(Painter.BACKGROUND_ENABLED_MOUSEOVER));
                dialogTheme.put("TabbedPane:TabbedPaneTab[Enabled+Pressed].backgroundPainter", new Painter(Painter.BACKGROUND_ENABLED_PRESSED));
                dialogTheme.put("TabbedPane:TabbedPaneTab[Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED));
                dialogTheme.put("TabbedPane:TabbedPaneTab[Disabled+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_DISABLED));
                dialogTheme.put("TabbedPane:TabbedPaneTab[Focused+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_FOCUSED));
                dialogTheme.put("TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_MOUSEOVER));
                dialogTheme.put("TabbedPane:TabbedPaneTab[Focused+MouseOver+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_MOUSEOVER_FOCUSED));
                dialogTheme.put("TabbedPane:TabbedPaneTab[Pressed+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_PRESSED));
                dialogTheme.put("TabbedPane:TabbedPaneTab[Focused+Pressed+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_PRESSED_FOCUSED));
                dialogTheme.put("TabbedPane:TabbedPaneTabArea[Disabled].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_DISABLED));
                dialogTheme.put("TabbedPane:TabbedPaneTabArea[Enabled+MouseOver].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_ENABLED_MOUSEOVER));
                dialogTheme.put("TabbedPane:TabbedPaneTabArea[Enabled+Pressed].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_ENABLED_PRESSED));
                dialogTheme.put("TabbedPane:TabbedPaneTabArea[Enabled].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_ENABLED));
                pane.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
                pane.putClientProperty("Nimbus.Overrides", dialogTheme);

                JTabbedPane secondPane = new JTabbedPane();
                secondPane.addTab( "Lorem", new JLabel("Lorem") );
                secondPane.addTab( "Ipsum", new JLabel("Ipsum") );
                secondPane.addTab( "Dolor", new JLabel("Dolor") );
                secondPane.addTab( "Sit", new JLabel("Sit") );
                secondPane.addTab( "Amet", new JLabel("Amet") );

                frame.getContentPane().setLayout( new BorderLayout() );
                frame.getContentPane().add( pane, BorderLayout.NORTH );
                frame.getContentPane().add( secondPane, BorderLayout.CENTER );
                frame.setVisible( true );
            }
        });
    }
}

Painter class with nimbusOrange
NOTE: I removed the paint methods in this Painter example. Because this code was to long. Just add them via copy&past from com.sun.java.swing.plaf.nimbus.TabbedPaneTabbedPaneTabPainter.

/*
 * TabbedPaneTabbedPaneTabPainter.java %E%
 *
 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import com.sun.java.swing.plaf.nimbus.AbstractRegionPainter;

/**
 */
public final class Painter extends AbstractRegionPainter {
    //package private integers representing the available states that
    //this painter will paint. These are used when creating a new instance
    //of TabbedPaneTabbedPaneTabPainter to determine which region/state is being painted
    //by that instance.
    static final int BACKGROUND_ENABLED = 1;
    static final int BACKGROUND_ENABLED_MOUSEOVER = 2;
    static final int BACKGROUND_ENABLED_PRESSED = 3;
    static final int BACKGROUND_DISABLED = 4;
    static final int BACKGROUND_SELECTED_DISABLED = 5;
    static final int BACKGROUND_SELECTED = 6;
    static final int BACKGROUND_SELECTED_MOUSEOVER = 7;
    static final int BACKGROUND_SELECTED_PRESSED = 8;
    static final int BACKGROUND_SELECTED_FOCUSED = 9;
    static final int BACKGROUND_SELECTED_MOUSEOVER_FOCUSED = 10;
    static final int BACKGROUND_SELECTED_PRESSED_FOCUSED = 11;


    private int state; //refers to one of the static final ints above
    private PaintContext ctx;

    //the following 4 variables are reused during the painting code of the layers
    private Path2D path = new Path2D.Float();
    private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
    private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
    private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);

    //All Colors used for painting are stored here. Ideally, only those colors being used
    //by a particular instance of TabbedPaneTabbedPaneTabPainter would be created. For the moment at least,
    //however, all are created for each instance.
    private Color color1 = decodeColor("nimbusOrange", 0.032459438f, -0.55535716f, -0.109803945f, 0);
    private Color color2 = decodeColor("nimbusOrange", 0.08801502f, 0.3642857f, -0.4784314f, 0);
    private Color color3 = decodeColor("nimbusOrange", 0.08801502f, -0.63174605f, 0.43921566f, 0);
    private Color color4 = decodeColor("nimbusOrange", 0.05468172f, -0.6145278f, 0.37647057f, 0);
    private Color color5 = decodeColor("nimbusOrange", 0.032459438f, -0.5953556f, 0.32549018f, 0);
    private Color color6 = decodeColor("nimbusOrange", 0.032459438f, -0.54616207f, -0.02352941f, 0);
    private Color color7 = decodeColor("nimbusOrange", 0.08801502f, -0.6317773f, 0.4470588f, 0);
    private Color color8 = decodeColor("nimbusOrange", 0.021348298f, -0.61547136f, 0.41960782f, 0);
    private Color color9 = decodeColor("nimbusOrange", 0.032459438f, -0.5985242f, 0.39999998f, 0);
    private Color color10 = decodeColor("nimbusOrange", 0.08801502f, 0.3642857f, -0.52156866f, 0);
    private Color color11 = decodeColor("nimbusOrange", 0.027408898f, -0.5847884f, 0.2980392f, 0);
    private Color color12 = decodeColor("nimbusOrange", 0.035931647f, -0.5553123f, 0.23137254f, 0);
    private Color color13 = decodeColor("nimbusOrange", 0.029681683f, -0.5281874f, 0.18039215f, 0);
    private Color color14 = decodeColor("nimbusOrange", 0.03801495f, -0.5456242f, 0.3215686f, 0);
    private Color color15 = decodeColor("nimbusOrange", 0.032459438f, -0.59181184f, 0.25490195f, 0);
    private Color color16 = decodeColor("nimbusOrange", 0.05468172f, -0.58308274f, 0.19607842f, 0);
    private Color color17 = decodeColor("nimbusOrange", 0.046348333f, -0.6006266f, 0.34509802f, 0);
    private Color color18 = decodeColor("nimbusOrange", 0.046348333f, -0.60015875f, 0.3333333f, 0);
    private Color color19 = decodeColor("nimbusOrange", 0.004681647f, -0.6197143f, 0.43137252f, 0);
    private Color color20 = decodeColor("nimbusOrange", 7.13408E-4f, -0.543609f, 0.34509802f, 0);
    private Color color21 = decodeColor("nimbusOrange", -0.0020751357f, -0.45610264f, 0.2588235f, 0);
    private Color color22 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.43866998f, 0.24705881f, 0);
    private Color color23 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.44879842f, 0.29019606f, 0);
    private Color color24 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.08776909f, -0.2627451f, 0);
    private Color color25 = decodeColor("nimbusOrange", 0.06332368f, 0.3642857f, -0.4431373f, 0);
    private Color color26 = decodeColor("nimbusOrange", 0.004681647f, -0.6198413f, 0.43921566f, 0);
    private Color color27 = decodeColor("nimbusOrange", -0.0022627711f, -0.5335866f, 0.372549f, 0);
    private Color color28 = decodeColor("nimbusOrange", -0.0017285943f, -0.4608264f, 0.32549018f, 0);
    private Color color29 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.4555341f, 0.3215686f, 0);
    private Color color30 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.46404046f, 0.36470586f, 0);
    private Color color31 = decodeColor("nimbusOrange", -0.57865167f, -0.6357143f, -0.54901963f, 0);
    private Color color32 = decodeColor("nimbusOrange", -4.2033195E-4f, -0.38050595f, 0.20392156f, 0);
    private Color color33 = decodeColor("nimbusOrange", 0.0013483167f, -0.16401619f, 0.0745098f, 0);
    private Color color34 = decodeColor("nimbusOrange", -0.0010001659f, -0.01599598f, 0.007843137f, 0);
    private Color color35 = decodeColor("nimbusOrange", 0.0f, 0.0f, 0.0f, 0);
    private Color color36 = decodeColor("nimbusOrange", 0.0018727183f, -0.038398862f, 0.035294116f, 0);
    private Color color37 = decodeColor("nimbusRed", 0.0f, 0.0f, 0.0f, 0);


    //Array of current component colors, updated in each paint call
    private Object[] componentColors;

    public Painter(int state) {
        super();
        this.state = state;
        Insets insets = null;
        switch( state ) {
            case BACKGROUND_ENABLED:                   insets = new Insets(7, 7, 1, 7); break;
            case BACKGROUND_ENABLED_MOUSEOVER:         insets = new Insets(7, 7, 1, 7); break;
            case BACKGROUND_ENABLED_PRESSED:           i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...