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:
I get
But if I do this
I get
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
Update 3: File Structure
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.
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.
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-onlymain
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With