Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Styling: support component type specific styles #417

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DevCharly
Copy link
Collaborator

PRs #341 and #388 added basic styling and style classes E.g.

mySlider.putClientProperty( "FlatLaf.style", "thumbColor: #f00" );
or
mySlider.putClientProperty( "FlatLaf.styleClass", "myclass" );

which is comparable to what can be done in HTML:

<p style="background: #f00">
or
<p class="myclass">

This PR is the next step and adds support for component type specific styles,
which are applied to all components of that type.

E.g. in FlatLaf properties file:

[style]ScrollPane = focusedBorderColor: #f00

or in Java code:

UIManager.put( "[style]ScrollPane", "focusedBorderColor: #f00" );

This style is applied to all scrollpanes in the application.

This is comparable to what can be done in CSS. E.g.

p {
    background: #f00;
}

Issues/problems

Because those style rules are applied to all components of a type within the application, this may cause some side effects or unwanted changes.

E.g. some components internally use other components (JButton for arrow button in JComboBox; JTextField in editable JComboBox) and those internal components are changed too.

Some styling properties are applied to borders (e.g. see fields in class FlatBorder annotated with @Styleable). When the border of a component is removed/changed then those style properties no longer can applied and exceptions are logged.

UIManager.put( "[style]ScrollPane", "focusedBorderColor: #f00" );

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBorder( BorderFactory.createEmptyBorder() );

The above code initially works because the style rule is applied within the constructor and the border is changed after it, but if you switch the theme, the style rule focusedBorderColor: #f00 can not applied to the EmptyBorder and an UnknownStyleException is logged.

FlatLaf is very strict (at the moment) and logs any style property that can not applied.
CSS on the other side simply ignores unknown styles...

CC @ebourg (issue #412); @Chrriis (issue #117); @orange451 (issue #340)

I'm a little bit undecided whether this PR is a good idea, or do more harm than good?

…pplied to all components of that type

(e.g. `[style]ScrollPane = focusedBorderColor: #f00`)
…tyle `[style]Spinner = foreground: #f00` and spinner labels are painted (caused by private subclass of JLabel where getForeground() is overridden)
@Chrriis
Copy link
Contributor

Chrriis commented Nov 7, 2021

My original issue was to use a FlatLaf theme but to have a custom component somewhere.
I am not sure which use case the feature you are presenting is trying to solve. Is it a way to have in effect multiple FlatLaf themes? Or is it a way to alter a theme after it is loaded, but then why not alter the theme?

@DevCharly DevCharly marked this pull request as ready for review December 8, 2021 14:38
@DevCharly DevCharly marked this pull request as draft December 8, 2021 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants