How to set jframe look and feel

52,456

Solution 1

Most common place to put this, is right inside your static void main(String[] args) method. Like so:

public static void main(String[] args) {
    try { 
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
    } catch(Exception ignored){}

    new YourClass(); //start your application
}  

for more info look at this site: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

Solution 2

Note: this is not an answer to the question (which was where to set the LAF). Instead, it's answering the question how-to set an LAF in a manner that's independent on its package name. Simplifies life in case the class is moved, as f.i. Nimbus from com.sun* to javax.swing.

The basic approach is to query the UIManager for its installed LAFs, loop through them until a match is found and set that. Here'r such methods as implemented in SwingX:

/**
 * Returns the class name of the installed LookAndFeel with a name
 * containing the name snippet or null if none found.
 * 
 * @param nameSnippet a snippet contained in the Laf's name
 * @return the class name if installed, or null
 */
public static String getLookAndFeelClassName(String nameSnippet) {
    LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
    for (LookAndFeelInfo info : plafs) {
        if (info.getName().contains(nameSnippet)) {
            return info.getClassName();
        }
    }
    return null;
}

Usage (here without exception handling)

String className = getLookAndFeelClassName("Nimbus");
UIManager.setLookAndFeel(className); 

Solution 3

UIManager.setLookAndFeel() will not work on components that are already created. Here is a good way to set the Look And Feel for every window in your application. This will set it on all open Windows in your program. Any new windows created will use what was set by the UIManager.

    UIManager.setLookAndFeel(lookModel.getLookAndFeels().get(getLookAndFeel()));
    for(Window window : JFrame.getWindows()) {
        SwingUtilities.updateComponentTreeUI(window);
    }

Solution 4

You can put this block in your main method after you have created the JFrame, or in the constructor of a class that extends JFrame.


    try
    {
        //Set the required look and feel
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        //Update the component tree - associate the look and feel with the given frame.
        SwingUtilities.updateComponentTreeUI(frame);
    }//end try
    catch(Exception ex)
    {
        ex.printStackTrace();
    }//end catch

Share:
52,456
Yayatcm
Author by

Yayatcm

Updated on July 09, 2022

Comments

  • Yayatcm
    Yayatcm almost 2 years

    I am kind of confused on where to put this :

    try {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch(Exception e){
    
    }
    

    I did not extend the JFrame class but used JFrame f = new JFrame(); Thanks :D

  • kleopatra
    kleopatra over 12 years
    basically correct, but not recommended for Nimbus :) it started its life in com.sun.* in jdk6 with the certainty of being moved into javax.swing in jdk7. So instead of hard-coding the class name, query the UIManager for the installed lookAndFeels and loop through them until a class containing "Nimbus" is found
  • Byron Voorbach
    Byron Voorbach over 12 years
    To be honest, I never use any Look and Feel for my java programs. But if I ever will, I'll use your snippet!! many thanks
  • Eric Duminil
    Eric Duminil almost 3 years
    Thanks, it's working fine. SonarLint mentions that it's possible to use Window.getWindows() instead.