Substance API

View all API methods.

View all client properties.


API method

public static boolean setCurrentTheme(ThemeInfo themeInfo)

Description

Sets new theme. In order for the theme to be correctly set, you need to call SwingUtilities.updateComponentTreeUI(java.awt.Component) on all open frames (call Frame.getFrames() to get a list of all open frames).

Parameters:

  • themeInfo - Theme information.

Returns:

  • The status of the theme change.

See also


Sample code

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.jvnet.substance.SubstanceLookAndFeel;

/**
 * Test application that shows the use of the
 {@link SubstanceLookAndFeel#setCurrentTheme(org.jvnet.substance.theme.ThemeInfo)}
 * API.
 
 @author Kirill Grouchnikov
 @see SubstanceLookAndFeel#setCurrentTheme(org.jvnet.substance.theme.ThemeInfo)
 */
public class SetCurrentTheme_InfoObject extends JFrame {
  /**
   * Creates the main frame for <code>this</code> sample.
   */
  public SetCurrentTheme_InfoObject() {
    super("Set current theme");

    this.setLayout(new BorderLayout());

    JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    final JButton changeTheme = new JButton("Change theme");
    changeTheme.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            changeTheme.setEnabled(false);
            // set new theme by info object
            SubstanceLookAndFeel
                .setCurrentTheme(SubstanceLookAndFeel
                    .getAllThemes().get("Barby Pink"));
            SwingUtilities
                .updateComponentTreeUI(SetCurrentTheme_InfoObject.this);
            repaint();
          }
        });
      }
    });
    controls.add(changeTheme);

    JButton getThemeName = new JButton("Get current theme name");
    getThemeName.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(SetCurrentTheme_InfoObject.this,
            "Current theme name is "
                + SubstanceLookAndFeel.getCurrentThemeName());
      }
    });

    controls.add(getThemeName);

    this.add(controls, BorderLayout.SOUTH);

    this.setSize(400200);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  /**
   * The main method for <code>this</code> sample. The arguments are
   * ignored.
   
   @param args
   *            Ignored.
   @throws Exception
   *             If some exception occured. Note that there is no special
   *             treatment of exception conditions in <code>this</code>
   *             sample code.
   */
  public static void main(String[] argsthrows Exception {
    UIManager.setLookAndFeel(new SubstanceLookAndFeel());
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        new SetCurrentTheme_InfoObject().setVisible(true);
      }
    });
  }
}

The screenshot below shows application frame under default theme (Aqua):

The screenshot below shows the result of calling this API with info on Barby Pink theme taken from result of getAllThemes() call: