-
-
Notifications
You must be signed in to change notification settings - Fork 265
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 individual components #341
Conversation
e.g. `mySlider.putClientProperty( "JComponent.style", "thumbColor: $TextField.background; thumbBorderColor: $Component.borderColor" );`
e.g. `mySlider.putClientProperty( "JComponent.style", Collections.singletonMap( "thumbSize", new Dimension( 8, 24 ) ) );`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked at the code and this is good!
I would only suggest one change: I think all FlatLaf-specific client properties should indicate so: instead of "JComponent.style"
, I think it would be better to use "FlatLaf.style"
.
Something crossed my mind: how would you defined more complex objects, e.g. an icon? myTree.putClientProperty("FlatLaf.style", "expandedIcon: chrriis.expandedIcon; collapsedIcon: chrriis.collapsedIcon");
myTree.putClientProperty("chrriis.expandedIcon", myExpandedIcon);
myTree.putClientProperty("chrriis.collapsedIcon", myCollapsedIcon); |
… boilerplate code)
Good idea. I'll change it.
I will not change the other client properties for various reasons
|
Good question. In the meantime it is possible to pass a Map<String, Object> map = new HashMap<>();
map.put( "expandedIcon", myExpandedIcon );
map.put( "collapsedIcon", myCollapsedIcon );
myTree.putClientProperty( "FlatLaf.style", map ); References to UI default values are already possible in CSS string syntax. mySlider.putClientProperty( "JComponent.style", "thumbColor: $MySlider.someColor" ); Maybe it makes sense to first look into client properties and then into |
I will definitely use a map instead of the CSS syntax for my use cases! One thing I am not sure about is that I cannot simply copy/paste and tweak the definitions from UIManager default values to the client properties map because the keys are different: I have to remove all the prefixes. That being said, styling a specific component is for special cases, so having to remove the prefixes is not a big deal. |
…key prefixes `[win]`, `[mac]`, `[linux]`, `[light]` and `[dark]` e.g. `mySlider.putClientProperty( "FlatLaf.style", "[light]trackColor: #00f; [dark]trackColor: #f00" );`
# Conflicts: # flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java # flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatInternalFrameUI.java # flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java
…s (for future use)
…r and setter methods
# Conflicts: # flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java # flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java
…ators (which use shared UI delegates)
Currently most FlatLaf styling capabilities are limited to change all components at once.
This PR is an attempt to better support styling individual components.
Some "styling" properties are available via Swing API (background, foreground, border, font, etc) for individual components,
but many FlatLaf UI defaults can be defined only for all components in FlatLaf properties files (https://www.formdev.com/flatlaf/properties-files/) or with
UIManager.put(key, value)
.The idea is to use a single client property to modify multiple style properties of a component.
This is similar to what can be done in JavaFX with
Node.setStyle(String style)
.CSS syntax
Therefore I've chosen CSS syntax:
key1: value1; key2: value2; ...
The key is the same as used in UI defaults, but without component type prefix.
E.g. for UI default
Slider.thumbSize
use keythumbSize
.The syntax of the value is the same as used in FlatLaf properties files
(https://www.formdev.com/flatlaf/properties-files/),
but some features are not supported (e.g. variables).
Color functions and references are supported. E.g::
$TextField.foreground
is replaced with the value returned byUIManager.get( "$TextField.foreground" )
.Map
Instead of the CSS string, it is also possible to assign a
java.util.Map
to the client property.In this case the values are not parsed from a string. They must be binary:
Supported components
JButton
andJToggleButton
JCheckBox
andJRadioButton
(including icon)JComboBox
andJSpinner
JInternalFrame
JLabel
JList
,JTable
,JTableHeader
andJTree
JMenuBar
,JMenu
,JMenuItem
,JCheckBoxMenuItem
,JRadioButtonMenuItem
andJPopupMenu
JProgressBar
JSeparator
andJPopupMenu.Separator
JScrollPane
andJScrollBar
JSlider
JSplitPane
JTabbedPane
JTextField
,JFormattedTextField
andJPasswordField
JTextArea
,JTextPane
andJEditorPane
JToolBar
andJToolBar.Separator
Possible "styles"
For possible "styles" take a look at: https://www.formdev.com/flatlaf/components/
(or https://github.com/JFormDesigner/FlatLaf/blob/main/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java)
It is also possible to "style" any JavaBean property (that has public getter and setter methods).
For CSS strings this works only for property types that can be parsed from a string.
Platform specific and light/dark theme specific styles
If a key is preceded by
[win]
,[mac]
or[linux]
, then its value is only used on the given platform.If a key is preceded by
[light]
or[dark]
, then its value is only used if the current FlatLaf theme is light or dark.This is also supported in FlatLaf properties files:
https://www.formdev.com/flatlaf/properties-files/#platform_specific
JSlider examples
Windows 10 style slider:
macOS style slider:
Thicker focus indicator (as in material design):
CC @Chrriis (issue #117); @orange451 (issue #340)