📜 ⬆️ ⬇️

Java skins

There are several commercial and free LookAndFeel libraries for Swing. Using LAF allows you to change the appearance of applications and add support for themes with little or no coding.

One of the highest quality LAF libraries is Substance. It includes well-traced standard components and over a dozen skins:

image
')
Consider how you can add the ability to select skins to the application by the user using Substance.

Substance connection

- you need to initialize Substance when starting the application like this:
SubstanceLookAndFeel laf = new SubstanceGeminiLookAndFeel(); UIManager.setLookAndFeel(laf); 

- you need to disable the system window decoration using code
 JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); 

- you need to allow Substance to show the list of installed themes in the system window menu:
 UIManager.put(SubstanceLookAndFeel.SHOW_EXTRA_WIDGETS, Boolean.TRUE); 

As a result, in the running application, the user can click the window icon and select a theme from the list:

image

Storage settings

You can install the Substance skin by the name of the theme class using the setSkin method. You can get the class name of the current theme by calling SubstanceLookAndFeel.getCurrentSkin (). GetClass (). GetCanonicalName (). It remains only to save this information to a file at the end of the application.
To save, you can use the standard Properties class, the settings file can be placed in the user's home directory, and the save function itself should preferably be added to the hook on shutdown so that it will be automatically called when the application terminates.

As a result, the following file will be saved in the home folder:

image

Full skins support code

 package insubstantial; import javax.swing.*; import java.awt.*; import org.pushingpixels.substance.api.*; import org.pushingpixels.substance.api.skin.*; import java.util.*; import java.io.*; public class Insubstantial { public static void setupSubstance() { try { final String fileName = System.getProperty("user.home") + System.getProperty("file.separator") + "insubstantial.txt"; final Properties properties = new Properties(); org.pushingpixels.substance.api.SubstanceLookAndFeel laf = new SubstanceGeminiLookAndFeel(); UIManager.setLookAndFeel(laf); UIManager.put(SubstanceLookAndFeel.SHOW_EXTRA_WIDGETS, Boolean.TRUE); JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { String skinClassName = SubstanceLookAndFeel.getCurrentSkin().getClass().getCanonicalName(); properties.setProperty("skinClassName", skinClassName); properties.store(new FileOutputStream(fileName), fileName); } catch (Throwable t) { t.printStackTrace(); } } }); properties.load(new FileInputStream(fileName)); String skinClassName = properties.getProperty("skinClassName"); laf.setSkin(skinClassName); } catch (Throwable t) { t.printStackTrace(); } } public static void main(String[] args) { setupSubstance(); EventQueue.invokeLater(new Runnable() { @Override public void run() { new TestGUI().setVisible(true); } }); } } 


As you can see, the code is minimal and practically does not require changes in the application.

The compiled example with the Substance library and the source code can be downloaded here .
The source code for Substance and all related libraries can be downloaded here .

Additionally

Why should users allow themselves to choose skins?
- to increase loyalty. Usually, people like it when they have the opportunity to customize something “for themselves”.

Should the interface look native?
- so say by inertia. For example, Microsoft Office, each version has its own themes that do not coincide with the system windows:
image

Source: https://habr.com/ru/post/128755/


All Articles