Java Swing Look and Feel setLookAndFeelByName(final String lookAndFeel)

Here you can find the source of setLookAndFeelByName(final String lookAndFeel)

Description

Sets the entire L&F based on "simple" name.

License

Apache License

Parameter

Parameter Description
lookAndFeel the simple name or null for the system default

Declaration

public static void setLookAndFeelByName(final String lookAndFeel) 

Method Source Code

//package com.java2s;
/*//ww  w. j  ava  2s  . c o  m
 * Copyright 2014 dorkbox, llc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Locale;

import javax.swing.UIManager;

public class Main {
    /**
     * Sets the entire L&F based on "simple" name. Null to set to the system L&F. If this is not called (or set), Swing will use the
     * default CrossPlatform L&F, which is 'Metal'.
     *
     * NOTE: On Linux + swing if the SystemLookAndFeel is the GtkLookAndFeel, this will cause GTK2 to get first which
     * will cause conflicts if one tries to use GTK3
     *
     * @param lookAndFeel the simple name or null for the system default
     */
    public static void setLookAndFeelByName(final String lookAndFeel) {
        if (lookAndFeel == null) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }

        // register a different L&F
        String specified = lookAndFeel.toLowerCase(Locale.US).trim();
        String current = UIManager.getLookAndFeel().getName().toLowerCase(Locale.US);

        if (!specified.equals(current)) {
            try {
                for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    String name = info.getName().toLowerCase(Locale.US);
                    String className = info.getClassName().toLowerCase(Locale.US);
                    if (specified.equals(name) || specified.equals(className)) {
                        UIManager.setLookAndFeel(info.getClassName());
                        return;
                    }
                }
            } catch (Exception e) {
                // whoops. something isn't right!
                e.printStackTrace();
            }
        }

        //        // display available look and feels by name
        //        for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        //            System.err.println(info.getClassName());
        //        }
        //
        //        // display all properties for the specified look and feel
        //        Set<Map.Entry<Object, Object>> entries = UIManager.getLookAndFeelDefaults()
        //                                                          .entrySet();
        //        for (Map.Entry<Object, Object> e : entries) {
        //            String key;
        //
        //
        //            if (e.getKey() instanceof StringBuffer) {
        //                StringBuffer s = (StringBuffer) e.getKey();
        //                key = s.toString();
        //            }
        //            else {
        //                key = (String) e.getKey();
        //            }
        //
        //            //Print all the keys available in UI manager
        //            if (e.getValue() != null && (key.toLowerCase().contains("icon") || key.toLowerCase().contains("image"))) {
        //                System.out.println("Key:  " + key);
        //            }
        //
        //            if (key.endsWith("TabbedPane.font")) {
        //                Font font = UIManager.getFont(key);
        //                Font biggerFont = font.deriveFont(2 * font.getSize2D());
        //                // change ui default to bigger font
        //                UIManager.put(key, biggerFont);
        //            }
        //            else if (key.endsWith("Tree.rowHeight")) {
        //                int rowHeight = UIManager.getInt(key);
        //                int bigRowHeight = 2 * rowHeight;
        //                UIManager.put(key, bigRowHeight);
        //            }
        //        }

        // this means we couldn't find our L&F
        new Exception("Could not load " + lookAndFeel + ", it was not available.").printStackTrace();
    }

    /**
     * Sets the entire L&F based on "simple" name. Null to set to the system L&F. If this is not called (or set), Swing will use the
     * default CrossPlatform L&F, which is 'Metal'.
     *
     * NOTE: On Linux + swing if the SystemLookAndFeel is the GtkLookAndFeel, this will cause GTK2 to get first which
     * will cause conflicts if one tries to use GTK3
     *
     * @param lookAndFeel the class or null for the system default
     */
    public static void setLookAndFeel(final Class<?> lookAndFeel) {
        if (lookAndFeel == null) {
            setLookAndFeelByName(null);
        } else {
            setLookAndFeelByName(lookAndFeel.getName());
        }
    }
}

Related

  1. setLookAndFeel(final Class lookAndFeel)
  2. setLookAndFeel(String laf)
  3. setLookAndFeel(String lafName)
  4. setLookAndFeel(String look)
  5. setLookAndFeel(String lookAndFeelClass)
  6. setLookAndTheme(LookAndFeel laf, Object theme)
  7. setMetalLAF()
  8. setMotifLookAndFeel()
  9. setMotifLookAndFeel()