Java Swing Look and Feel setLookAndFeel(Component comp, LookAndFeel lf)

Here you can find the source of setLookAndFeel(Component comp, LookAndFeel lf)

Description

set Look And Feel

License

Open Source License

Declaration

public static final void setLookAndFeel(Component comp, LookAndFeel lf) 

Method Source Code

//package com.java2s;
/*/* w  w  w.j  a v  a  2s . c o m*/
 Copyright (C) 1996, 1997, 1998 State of California, Department of
 Water Resources.

 VISTA : A VISualization Tool and Analyzer.
 Version 1.0beta
 by Nicky Sandhu
 California Dept. of Water Resources
 Division of Planning, Delta Modeling Section
 1416 Ninth Street
 Sacramento, CA 95814
 (916)-653-7552
 nsandhu@water.ca.gov

 Send bug reports to nsandhu@water.ca.gov

 This program is licensed to you under the terms of the GNU General
 Public License, version 2, as published by the Free Software
 Foundation.

 You should have received a copy of the GNU General Public License
 along with this program; if not, contact Dr. Francis Chung, below,
 or the Free Software Foundation, 675 Mass Ave, Cambridge, MA
 02139, USA.

 THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED BY THE CALIFORNIA
 DEPARTMENT OF WATER RESOURCES AND CONTRIBUTORS "AS IS" AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE CALIFORNIA
 DEPARTMENT OF WATER RESOURCES OR ITS CONTRIBUTORS BE LIABLE FOR
 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 OR SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA OR PROFITS; OR
 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 DAMAGE.

 For more information about VISTA, contact:

 Dr. Francis Chung
 California Dept. of Water Resources
 Division of Planning, Delta Modeling Section
 1416 Ninth Street
 Sacramento, CA  95814
 916-653-5601
 chung@water.ca.gov

 or see our home page: http://wwwdelmod.water.ca.gov/

 Send bug reports to nsandhu@water.ca.gov or call (916)-653-7552

 */

import java.awt.Component;

import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Main {
    /**
     * sets look and feel on given component
     */
    public static final void setLookAndFeel(Component comp, String str) {
        String swingPackage = "javax.swing.plaf.";
        boolean propAccessException = false;
        java.util.Properties props = null;
        String osname = null;
        try {
            props = System.getProperties();
            osname = (String) props.getProperty("os.name");
            if (str.equals("windows"))
                props.put("os.name", "Windows");
            else if (str.equals("mac"))
                props.put("os.name", "Macintosh");
        } catch (Exception e) {
            propAccessException = true;
        }
        try {
            if (str.equals("system")) {
                UIManager.setLookAndFeel(UIManager
                        .getSystemLookAndFeelClassName());
                SwingUtilities.updateComponentTreeUI(comp);
            } else if (str.equals("xplaf")) {
                UIManager.setLookAndFeel(UIManager
                        .getCrossPlatformLookAndFeelClassName());
                SwingUtilities.updateComponentTreeUI(comp);
            } else if (str.equals("motif")) {
                UIManager
                        .setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                SwingUtilities.updateComponentTreeUI(comp);
            } else if (str.equals("windows")) {
                UIManager
                        .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                SwingUtilities.updateComponentTreeUI(comp);
            } else if (str.equals("mac")) {
                UIManager
                        .setLookAndFeel("com.sun.java.swing.plaf.mac.MacLookAndFeel");
                SwingUtilities.updateComponentTreeUI(comp);
            } else {
                UIManager.setLookAndFeel(UIManager
                        .getCrossPlatformLookAndFeelClassName());
                SwingUtilities.updateComponentTreeUI(comp);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        } finally {
            if (!propAccessException && props != null)
                props.put("os.name", osname);
        }
    }

    /**
     *
     */
    public static final void setLookAndFeel(Component comp, LookAndFeel lf) {
        try {
            UIManager.setLookAndFeel(lf);
            SwingUtilities.updateComponentTreeUI(comp);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

Related

  1. setLookAndFeel()
  2. setLookAndFeel()
  3. setLookAndFeel()
  4. setLookAndFeel()
  5. setLookAndFeel(Component comp)
  6. setLookAndFeel(final Class lookAndFeel)
  7. setLookAndFeel(String laf)
  8. setLookAndFeel(String lafName)
  9. setLookAndFeel(String look)