Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Nimbus look and feel in main

Tags:

java

swing

nimbus

I am just learning Java and still have not been able to sort out this little problem I have.

My pop up Calendar uses Nimbus look and feel, but I have panels and container Jtables that use Java's look and feel - I am trying to make every GUI screen/ window use the Nimbus look and feel. It was suggested by Merky to put the following code in my main to make every subsequent screen have the Nimbus look and feel, but I cannot get it to work, so where and how should I put this code?

public static void main(String args[]) {
    SA md = new OptraderSA("Copyright© 2010 Simon Andi");

    Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();

    md.setLocation(sd.width/2-400/2, sd.height/2-400/2);
    md.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    /*Suggested Code*/
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                System.out.println("CHOSEN THIS");
                break;
            } else {
                UIManager.setLookAndFeel  ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
        }
    } catch (Exception e) {
        // If Nimbus is not available, you can set to another look and feel.
        // I can't get it to compile or work.
    }

}
like image 368
Simon Andi Avatar asked Jan 06 '11 17:01

Simon Andi


People also ask

How do I set my Nimbus look and feel?

The first line of code retrieves the list of all installed look and feel implementations for the platform and then iterates through the list to determine if Nimbus is available. If so, Nimbus is set as the look and feel. Version Note: Do not set the Nimbus look and feel explicitly by invoking the UIManager.

How do you set your look and feel?

setLookAndFeel() − To set the look and feel of UI components. JFrame. setDefaultLookAndFeelDecorated(true); − To change the look and feel of the frame.

Can you customize the Nimbus?

One of the biggest advantages of Nimbus is that it is highly customizable. You can change the look in almost any way you can imagine.

How add look and feel in NetBeans?

You can change the of the preview by: Tools-Options Miscellaneous tab Windows tab Look and Feel:Preferred look and feel. With this the look and feel of the IDE changes too. That only changes the IDE.


2 Answers

This is what I do in my main method to enable Nimbus look and feel:

public static void main(String[] args) {
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {
        // If Nimbus is not available, fall back to cross-platform
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception ex) {
            // Not worth my time
        }
    }
    new Controller();
}

You need to be sure to configure the UIManager with the Nimbus look and feel before you start the swing event dispatch thread (before calling view.setVisible(true)).

like image 145
BenjaminLinus Avatar answered Oct 08 '22 16:10

BenjaminLinus


I think try with:

for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
        UIManager.setLookAndFeel(info.getClassName());
        System.out.println("CHOSEN THIS");
        break;
    }
}
like image 41
Rahul Avatar answered Oct 08 '22 14:10

Rahul