Search code examples
javaswingbackgroundlook-and-feeljcomponent

Customize theme color in Flatlaf Look and Feel


I'm using the Flatlaf Look and Feel for my GUI in Java swing. There's a pre-made theme in Flatlaf which I imported, but I'd like to change one of the colors in the color scheme slightly. It's the color that is used to highlight JComponents and selected tabs. Is there any way to do this?

The standard way to customize a L&F is:

try {
        UIManager.setLookAndFeel(new FlatSolarizedLightIJTheme());
        UIManager.put("choose JComponent here","choose value here");

} catch( Exception ex ) { 
}


Solution

  • I don't know how to do this in FlatLaf, but there is another library called material-ui-swing that gives you more power on the material theme, and after it is the library that works with the UIManager.

    You can do somethings like that:

      try {
          JDialog.setDefaultLookAndFeelDecorated(true);
          JFrame.setDefaultLookAndFeelDecorated(false);
          MaterialTheme theme = new MaterialLiteTheme();
          // here your custom config theme.setXXX();
          MaterialLookAndFeel material = new MaterialLookAndFeel(theme);
          UIManager.setLookAndFeel(material);
      } catch (UnsupportedLookAndFeelException e) {
          e.printStackTrace();
      }
    

    On MaterialTheme you can call the following API https://vincenzopalazzo.github.io/material-ui-swing to change any type of color, or implement a custom theme as external library as the following theme DarkStackOverflowTheme