How to set jframe look and feel
Asked Answered
N

5

11

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

Neptunian answered 2/3, 2012 at 22:9 Comment(2)
Programatically Setting the Look and FeelFelder
Make sure the Look'n'Feel is configured before initialising the frame.Decline
W
15

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

Write answered 13/3, 2012 at 10:13 Comment(2)
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 foundGrandmother
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 thanksWrite
G
13

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); 
Grandmother answered 13/3, 2012 at 11:46 Comment(0)
I
10

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);
    }
Iglesias answered 26/2, 2013 at 8:34 Comment(1)
Thanks, it's working fine. SonarLint mentions that it's possible to use Window.getWindows() instead.Aldis
H
2

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

Haulm answered 8/7, 2013 at 7:42 Comment(0)
P
0
   try {
        for (javax.swing.UIManager.LookAndFeelInfo info :  javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
     } catch (ClassNotFoundException | InstantiationException || javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(  Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
Permission answered 6/7, 2015 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.