JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.42k stars 272 forks source link

Setting Foreground Colour as per Style Class #570

Closed manticore-projects closed 2 years ago

manticore-projects commented 2 years ago

Greetings!

First of all, thank you for this amazing software. We do love it.

Although I have a question. We want to used JLabels and assign style classes H00, H0, H1 as Captions and want to show them in a different color. This colour should be customisable (in order to reflect our customer's corporate design).

In short: Some JLables with style classses H00, H0, H1 should be Blue, all other labels Black.

How can I achieve that with a Property File? I would like to avoid setting the Blue Colour in the Java Code itself if possible.

My property file so far looks like this:

@background = #E6E6E6FF
@foreground = lighten(#000,32%)
@accentColor = #FF420EFF

@baseTheme = light

defaultFont = 11  Roboto, Arial

[style].h00 = font: 36 Roboto bold
[style].h0 = font: 28 Roboto semibold
[style].h1 = font: 28 Roboto
@background = #E6E6E6FF
@foreground = lighten(#000,32%)
@accentColor = #FF420EFF

@baseTheme = light

defaultFont = 11  Roboto, Arial

[style].h00 = font: 36 Roboto bold
[style].h0 = font: 28 Roboto semibold
[style].h1 = font: 28 Roboto

Label.foreground= #030146FF

Table.background = #ffffff
Table.alternateRowColor = darken($Table.background, 16%)
Table.selectionBackground = lighten(@accentColor,16%)
Table.selectionForeground = #ffffff
Table.lightSelectionBackground = @accentColor
Table.lightSelectionForeground = #ffffff
Table.focusCellBackground = @accentColor
Table.focusCellForeground = #ffffff
Table.dropLineColor= #f57900
Table.dropLineShortColor= #f57900

fillsViewportHeight=true

Label.foreground= #030146FF

Table.background = #ffffff
Table.alternateRowColor = darken($Table.background, 16%)
Table.selectionBackground = lighten(@accentColor,16%)
Table.selectionForeground = #ffffff
Table.lightSelectionBackground = @accentColor
Table.lightSelectionForeground = #ffffff
Table.focusCellBackground = @accentColor
Table.focusCellForeground = #ffffff
Table.dropLineColor= #f57900
Table.dropLineShortColor= #f57900

fillsViewportHeight=true
DevCharly commented 2 years ago

Some JLables with style classses H00, H0, H1 should be Blue, all other labels Black.

Then you should not change the style classses h00, h0, h1.

Instead create a new style class for the color and also assign it to the labels.

[style].myColoredTitle = foreground: #00f
myLabel.putClientProperty( "FlatLaf.styleClass", "h1 myColoredTitle" );
manticore-projects commented 2 years ago

Thank you so much! It makes a lot of sense, when I see this now. I did not catch that possibility from the Documentation or Samples.

manticore-projects commented 2 years ago

Works like a charm, but leads to more questions:

1) as far as I can see it, the Properties file would be read only from the Classpath. Is there a way, to read it from a plain File also?

Explanation: We would like to deploy a distinct Java SwingUI JAR, but customize as per customer as per configuration file only. Of course I understand, that I could deploy a customConfig.jar and put it into the classpath -- but that's rather cumbersome.

2) working around this challenge, I have defined Command Line Options ACCENT_COLOR and CAPTION_COLOR and then I try to overwrite the FlatLaf properties. This works well for CAPTION_COLOR, but it does not work for ACCENT_COLOR and derived colors of the components. Am I doing something wrong?

@background = lighten(#E6E6E6FF,16%)
@foreground = lighten(#000,32%)
@accentColor = #FF420EFF
if ( Settings.hasKey("ACCENT_COLOR") ) {
          FlatLaf.setGlobalExtraDefaults( Collections.singletonMap( "@accentColor", Settings.getString("ACCENT_COLOR") ) );
        }
        if ( Settings.hasKey("CAPTION_COLOR") ) {
          FlatLaf.setGlobalExtraDefaults( Collections.singletonMap( "[style].caption", "foreground: " + Settings.getString("CAPTION_COLOR") ) );
        }

        FlatLightLaf.registerCustomDefaultsSource( "com/manticore/themes" );
        FlatLightLaf.setup(  );

Accent color stays Orange #FF420EFF even when Settings.getString("ACCENT_COLOR") returns #fbb018. Although Labels of [style].caption show the correct light blue according to Settings.getString("CAPTION_COLOR") = #007cb7

manticore-projects commented 2 years ago

Please how can I access the @accentColor from Java?

laf.getExtraDefaults().get("@accentColor");

returns null (even when the accentColor is set and effective in the properties file. Same for

UIManager.getLookAndFeelDefaults().getColor("@accentColor");
UIManager.getLookAndFeelDefaults().getColor("accentColor");

In the meantime I work around by getting UI properties set to the accentColor, e. g.

UIManager.getLookAndFeelDefaults().getColor("List.selectionBackground");
DevCharly commented 2 years ago
  1. as far as I can see it, the Properties file would be read only from the Classpath. Is there a way, to read it from a plain File also?

Use FlatLaf.registerCustomDefaultsSource( File folder ), which registers a folder where FlatLaf searches for properties files with custom UI defaults. https://www.formdev.com/flatlaf/how-to-customize/#application_properties

  1. ...

    if ( Settings.hasKey("ACCENT_COLOR") ) {
          FlatLaf.setGlobalExtraDefaults( Collections.singletonMap( "@accentColor", Settings.getString("ACCENT_COLOR") ) );
        }
        if ( Settings.hasKey("CAPTION_COLOR") ) {
          FlatLaf.setGlobalExtraDefaults( Collections.singletonMap( "[style].caption", "foreground: " + Settings.getString("CAPTION_COLOR") ) );
        }
    
        FlatLightLaf.registerCustomDefaultsSource( "com/manticore/themes" );
        FlatLightLaf.setup(  );

The problem with this code is that you invoke FlatLaf.setGlobalExtraDefaults() twice and the second one replaces the first one. Invoke FlatLaf.setGlobalExtraDefaults() only once and put all values into one map.

manticore-projects commented 2 years ago

Thank you very much. This makes it clear and I appreciate.

DevCharly commented 2 years ago

Please how can I access the @accentColor from Java?

Keys starting with @ are variables with are only used while processing properties files, but are now available in UI defaults. See https://www.formdev.com/flatlaf/properties-files/#variables

There are two workarounds:

  1. add something like accentColor = @accentColor to your FlatLaf.properties. Then you can use UIManager.getColor( "accentColor" ).
  2. use FlatLaf.parseDefaultsValue( "accentColor", "@accentColor", null )