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

Different theme same component #363

Closed FSelene closed 3 years ago

FSelene commented 3 years ago

Hi, my goal is to set a different thema for the same component. For example two button with different thema. How can I do? I thought it was possible to extend the JButton base component and create two different type JButton1 and JButton2 and assign different value for same properties in json file. Is it possible?

Thank you very much!

Bios-Marcel commented 3 years ago

The button is styled by it's UI-Implementation, which is FlatButtonUI. Since that class accesses the UI manager, which is a shared key value map, you can't have to different styles in it. May I ask what your usecase is? Maybe it's enough to manually restyle the button outside of the look and feel?

DevCharly commented 3 years ago

As soon as PR #341 is complete and merged, it should be possible 😄

DevCharly commented 3 years ago

Since FlatLaf 1.6 there is a possibility to use multiple themes:

image

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf;

public class MultiThemeDemo
{
    public static void main( String[] args ) {
        SwingUtilities.invokeLater( () -> {
            // setup primary theme
            FlatLightLaf.setup();

            // create secondary theme
            UIDefaults darkDefaults = new FlatDarkLaf().getDefaults();

            JPanel panel = new JPanel( new GridLayout( 2, 1 ) );
            panel.setBorder( new EmptyBorder( 20, 20, 20, 20 ) );

            // create panel using primary theme
            panel.add( createPanel() );

            // create panel using secondary theme
            FlatLaf.runWithUIDefaultsGetter( key -> {
                Object value = darkDefaults.get( key );
                return (value != null) ? value : FlatLaf.NULL_VALUE;
            }, () -> {
                panel.add( createPanel() );
            } );

            // show frame
            JFrame mainFrame = new JFrame( "FlatLaf MultiThemeDemo" );
            mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            mainFrame.getContentPane().add( panel );
            mainFrame.pack();
            mainFrame.setLocationRelativeTo( null );
            mainFrame.setVisible( true );
        } );
    }

    private static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setBorder( new EmptyBorder( 20, 20, 20, 20 ) );
        panel.add( new JButton( "Button" ) );
        panel.add( new JComboBox<>( new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" } ) );
        panel.add( new JTextField( "text", 5 ) );
        panel.add( new JCheckBox( "CheckBox", true ) );
        panel.add( new JRadioButton( "RadioButton", true ) );
        return panel;
    }
}

Known limitations: