Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Look and Feel

I am using Netbeans on a Windows machine, what happens is that if I run the main java file the look and feel I get is different than in the case I run the whole program. Meaning if I do this: enter image description here

I get

enter image description here

But if I do this

enter image description here

I get enter image description here

Did you see the difference in the look and feel of both the java outputs? Why is it there? I want that when I export it to Jar it should open like the 1st case, nice looking buttons. How?

Update 1: I found the following code in the starting of my main:

public static void main(String args[]) {
    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 ex) {
        java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

    //Some code here...
}

So it should set the nimbus look and feel but the problem is that when I open the jar the Nimbus look and feel is not there

Update 2: A look of my Navigator

enter image description here

Update 3: File Structure

enter image description here

like image 540
Daksh Shah Avatar asked Mar 31 '14 09:03

Daksh Shah


People also ask

What is look and feel in Java?

The architecture of Swing is designed so that you may change the "look and feel" (L&F) of your application's GUI (see A Swing Architecture Overview). "Look" refers to the appearance of GUI widgets (more formally, JComponents ) and "feel" refers to the way the widgets behave.

How do I change the 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.


1 Answers

I am using Netbeans on a Windows machine, what happens is that if I run the main java file the look and feel I get is different than in the case I run the whole program.

When you run a Swing application the default Look and Feel is set to a cross-platform L&F also called Metal. On the other hand when you create a new JFrame from NetBeans New file wizard it also includes a main method just for test purposes, making developers able to "run" the top-level container. Within this main method the Look and Feel is set to Nimbus as you have included in your Update 1.

This is well explained in this Q&A: How can I change the default look and feel of Jframe? (Not theme of Netbeans). As stated there you can modify the template associated to JFrame form to set the L&F you wish. However be aware of this line:

A Java application only needs one main class so this test-only main methods should be deleted when you will deploy your application. [...] the L&F should be established only once at the start-up, not in every top-level container (JFrame, JDialog...).

You also might to take a look to Programatically Setting the Look and Feel of How to Set the Look and Feel article.

Edit

I just did not understand one thing which test-only main methods do i need to delete and if i delete them how will my prg run properly?

A Java application must have only one main method that inits the execution. The class which has this main method is defined within MANIFEST.MF file when you deploy your JAR. So, having a main method in each top-level container (JFrame or JDialog) is not needed and it's not a good practice.

However sometimes you don't want to run the whole application to test how a particular frame/dialog works. That's why NetBeans includes this main method on JFrame or JDialog creation. But as stated above when you will deploy your JAR you should delete those "extra" main methods.

and yah, in that you have given how to do it when i create new jframes, but i already have 20s of them

A Swing application tipically has a single JFrame and multiple JDialog's. Take a look to this topic for further details: The Use of Multiple JFrames, Good/Bad Practice?

And anyways it is nimbus in there and it is what i want, but that is not what is opening

You just need to programatically set the L&F to Nimbus in your main class (the one that is executed when you run the whole application). You can copy-paste the code you've included in your Update 1 there.

Edit 2

When you create a new project in NetBeans it ask you for create a main class too. Let's say I create a new project called Test, it will ask me for create a main class like this:

enter image description here

This generated Test class will have the main method that triggers the application execution:

enter image description here

Within this main method you have to put the code you've included in your Update 1:

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        try {
                            javax.swing.UIManager.setLookAndFeel(info.getClassName());
                            break;
                        } catch (ClassNotFoundException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (InstantiationException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IllegalAccessException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (UnsupportedLookAndFeelException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }                                       
            }
        });
    }

}

Then when you run your application the L&F will be set to Nimbus overriding the default cross-platform L&F. Henceforth all created Swing components will have Nimbus as L&F.

Note: The reason of SwingUtilities.invokeLater() call is explained in Initial Threads article.

like image 120
dic19 Avatar answered Oct 07 '22 17:10

dic19