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:
This generated Test
class will have the main
method that triggers the application execution:
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.
UIManager.setLookAndFeel(...);
– Atony