Java Swing Look and Feel applyStartupLookAndFeel(String desiredLafClassName)

Here you can find the source of applyStartupLookAndFeel(String desiredLafClassName)

Description

Try to apply the specified look and feel.

License

Open Source License

Parameter

Parameter Description
desiredLafClassName the class name of the desired look and feel, or null

Return

the class name of the applied look and feel, or null

Declaration

public static String applyStartupLookAndFeel(String desiredLafClassName) 

Method Source Code

//package com.java2s;
/*/*from w  w  w  .j  a va  2  s .c  o m*/
 * Copyright ? Mihai Borobocea 2009, 2010
 * 
 * This file is part of SimpleSwing.
 * 
 * SimpleSwing is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SimpleSwing is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SimpleSwing.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class Main {
    private static final LookAndFeelInfo[] availableLookAndFeels = UIManager.getInstalledLookAndFeels();

    /**
     * Try to apply the specified look and feel. If not successful, try setting
     * the System look and feel (on Windows and Mac) or Nimbus (on everything
     * else). If these classes can't be found, try the cross-platform look and
     * feel (currently Metal).
     * 
     * @param desiredLafClassName
     *            the class name of the desired look and feel, or null
     * @return the class name of the applied look and feel, or null
     */
    public static String applyStartupLookAndFeel(String desiredLafClassName) {
        try {
            String clsName = desiredLafClassName;
            UIManager.setLookAndFeel(clsName);
            return clsName;
        } catch (Exception e) {
        }

        try {
            String clsName;
            if (isWindows() || isMacOS())
                clsName = UIManager.getSystemLookAndFeelClassName();
            else
                clsName = getLafClassName("Nimbus");
            if (clsName == null)
                UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel(clsName);
            return clsName;
        } catch (Exception e) {
        }

        return null;
    }

    /**
     * Detects if the OS is Windows using the <code>os.name</code> system
     * property
     * 
     * @return true if the <code>os.name</code> property starts with "windows",
     *         false otherwise
     */
    // TODO: make reference to the antlr sources
    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().startsWith("windows");
    }

    /**
     * Detects if the OS is Mac OS using the <code>os.name</code> system
     * property
     * 
     * @return true if the <code>os.name</code> property starts with "mac os",
     *         false otherwise
     */
    public static boolean isMacOS() {
        return System.getProperty("os.name").toLowerCase().startsWith("mac os");
    }

    /**
     * Returns the class name of the LookAndFile class with the specified name,
     * or null if no such class if found.
     * 
     * @param lafName
     *            the user name for the look and feel (e.g. Metal)
     * @return the class name of the implementing class if found, otherwise null
     */
    public static String getLafClassName(String lafName) {
        if (lafName == null)
            return null;

        for (LookAndFeelInfo lafInfo : availableLookAndFeels)
            if (lafName.equalsIgnoreCase(lafInfo.getName()))
                return lafInfo.getClassName();

        return null;
    }
}

Related

  1. applyNimbusLaF()
  2. checkGTKLookAndFeel()
  3. checkThemeLiscense(LookAndFeelInfo i)
  4. computeIsLafAqua()
  5. defaultLookAndFeel()