Java Swing Look and Feel setLookAndFeel(final Class lookAndFeel)

Here you can find the source of setLookAndFeel(final Class lookAndFeel)

Description

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

License

Apache License

Parameter

Parameter Description
lookAndFeel the class or null for the system default

Declaration

public static void setLookAndFeel(final Class<?> lookAndFeel) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  . ja v a 2s  .  c om
 * 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 class or null for the system default
     */
    public static void setLookAndFeel(final Class<?> lookAndFeel) {
        if (lookAndFeel == null) {
            setLookAndFeelByName(null);
        } else {
            setLookAndFeelByName(lookAndFeel.getName());
        }
    }

    /**
     * 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();
    }
}

Related

  1. setLookAndFeel()
  2. setLookAndFeel()
  3. setLookAndFeel()
  4. setLookAndFeel(Component comp)
  5. setLookAndFeel(Component comp, LookAndFeel lf)
  6. setLookAndFeel(String laf)
  7. setLookAndFeel(String lafName)
  8. setLookAndFeel(String look)
  9. setLookAndFeel(String lookAndFeelClass)