Question

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

Was it helpful?

Solution

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.

OTHER TIPS

Possible problem.

If you have a main class that you launch your application from, say a Main class. If the look and feel is not set in that class, the result will be what you are experiencing.

Say you have Main where you launch MyFrame from. (forgive the second main method, this is only for example, as in the OP's case)

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new MyFrame().setVisible(true);
            }
        });
    }
}

public class MyFrame extends JFrame {
    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());
         ...

    }

You will get the result (even though the look and feel is set the MyFrame:

enter image description here


If I just run MyFrame I get:

enter image description here


So if you add the look and feel to you Main, it will get this result

public class 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());
         ...

        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new MyFrame().setVisible(true);
            }
        });
    }
}

enter image description here

I would recommend to try to change your LAF in your main method, like:

public static void main(String[] args){
  Frame f; // Your (J)Frame or GUI-Class.
  // some code here...
  try{
    UIManager.setLookAndFeel(new NimbusLookAndFeel()) // Because it seems to be Nimbus what you want.
    SwingUtilities.updateComponentTreeUI(f); // To refresh your GUI
  }catch(Exception e){
    // Do whatever you want if a problem occurs like LAF not supported.
  }
}

Or you could do it without SwingUtilities.updateComponentTreeUI(f); if you call

f.setVisible(true);

after the change of your LAF.

Maybe you can try these ones ?

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

edit:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

If the app. is running in Java 1.6 on the system (outside the IDE) it would not have access to Nimbus (scroll to the bottom for the @since element). Nimbus only became available in 1.7.

to determine the look and feel you can read the current used Look And Feel with UIManager.getSystemLookAndFeelClassName()

If you know your preferred look and feel you can set it with UIManager.setLookAndFeel(look) (prior creating your GUI elements!)

But remember: you should check if this look and feel is available (prior setting it on another operating system with other look and feels installed)

Further information:

EDIT: To determine installed look and feels:

UIManager.LookAndFeelInfo plaf[] = UIManager.getInstalledLookAndFeels();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top