JFormDesigner / FlatLaf

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

JPanel Rounded Corners #367

Closed shaydarebuar closed 2 years ago

shaydarebuar commented 3 years ago

It is great JPanel have rounded property like other components as well...

Thanks for such amazing LAF.

DevCharly commented 2 years ago

implemented in main branch for v2

There are several ways to use it.

Set panel background color and arc diameter:

panel.setBackground( Color.magenta );
panel.putClientProperty( FlatClientProperties.STYLE, "arc: 8" );

image

Use FlatLineBorder, which also allows specifying insets:

panel.setBackground( Color.magenta );
panel.setBorder( new FlatLineBorder( new Insets( 16, 16, 16, 16 ), Color.blue, 1, 8 ) );

image

Or use styling and let FlatLaf compute colors:

panel.putClientProperty( FlatClientProperties.STYLE,
    "background: tint(@background,50%);" +
    "border: 16,16,16,16,shade(@background,10%),,8" );

image

To support light and dark themes:

panel.putClientProperty( FlatClientProperties.STYLE,
    "[light]background: tint(@background,50%);" +
    "[dark]background: shade(@background,15%);" +
    "[light]border: 16,16,16,16,shade(@background,10%),,8;" +
    "[dark]border: 16,16,16,16,tint(@background,10%),,8" );

image

If you have many places to use same panel style, define it once (after FlatLaf.setup()):

UIManager.put( "[style]Panel.myRoundPanel",
    "[light]background: tint(@background,50%);" +
    "[dark]background: shade(@background,15%);" +
    "[light]border: 16,16,16,16,shade(@background,10%),,8;" +
    "[dark]border: 16,16,16,16,tint(@background,10%),,8" );

and use it:

panel.putClientProperty( FlatClientProperties.STYLE_CLASS, "myRoundPanel" );

Or use own FlatLaf properties files to define such styles. See Application properties files.

Create FlatLightLaf.properties:

[style]Panel.myRoundPanel= \
    background: tint(@background,50%); \
    border: 16,16,16,16,shade(@background,10%),,8

And create FlatDarkLaf.properties:

[style]Panel.myRoundPanel= \
    background: shade(@background,15%); \
    border: 16,16,16,16,tint(@background,10%),,8
Langelot0210 commented 7 months ago

How to apply gradient color on a Jpanel with FlatLaf?

DevCharly commented 7 months ago

How to apply gradient color on a Jpanel with FlatLaf?

@Langelot0210 FlatLaf does not support gradient backgrounds.

You have to implement this yourself. Simply create a subclass of JPanel, override paintComponent(Graphics) method and paint the gradient