How can I change the default look and feel of Jframe? (Not theme of Netbeans)
Asked Answered
S

3

12

I want to change the default look and feel of all the jframe forms I'll create from here on out instead of having to manually edit every look and feel code of every jframe I create from 'Nimbus' to 'Windows'.

So what I want to happen is that from when I startup Netbeans to when I create a new Jframe, the code for the look and feel of that Jframe I just created will automatically be set to "Windows" rather than "Nimbus".

I want the look and feel code to look like this right after I click 'New > Jframe form':

        try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Windows".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    }

Note: I am not trying to theme Netbeans itself, I just want the Jframe that I create to have the windows look and feel by default so I don't have to go through the source tab and change Nimbus to Windows for every Jframe I create.

Saavedra answered 27/2, 2014 at 11:6 Comment(0)
A
18

First of all take a look to this topic: The Use of Multiple JFrames, Good/Bad Practice?

Note: I am not trying to theme Netbeans itself, I just want the Jframe that I create to have the windows look and feel by default so I don't have to go through the source tab and change Nimbus to Windows for every Jframe I create.

What you want to change is called template: every file you can create through the New File wizard has an associated template. Having said this NetBeans gives the developers the ability to update/create default templates. Go to Tools -> Templates and look for Swing GUI Forms -> JFrame

enter image description here

You have two options here:

  1. Open the template in the editor and modify it there.
  2. Create a duplicate template and modify this last one.

I'd go with Option 2 just to keep the original template unmodified..

enter image description here

When you edit the template just modify this line (or watherever you want actually):

enter image description here

Finally to create a new "custom" JFrame just find your template in Swing GUI Forms -> MyJFrameTemplate as shown below:

enter image description here


In addition

Reading @Misgevolution's comment below I think there's something to be clarified. This auto-generated main method is there just for test purposes making developers be able to "run" top-level containers. A Java application only needs one main class so this test-only main methods should be deleted when you will deploy your application. As suggested in other answers the L&F should be established only once at the start-up, not in every top-level container.

Alexis answered 27/2, 2014 at 11:30 Comment(5)
WooHoo! I love tutorials :-)Fults
Haha! Believe it or not I've started to use templates pretty recently. It's a great tool for repetitive code like hand-written GUIs may have (you know, the code which is tipically inside a createAndShowGUI() method: a panel, a dialog, etc). @peeskilletAlexis
Yeah templates help in trying to be the first to answer a question with code ;)Fults
I think the question was not how to set the look and feel when the program starts, But anywhere in the middle of the application's session. So,How would this answer solve the above ?Phantasy
The problem is question's title is not accurate. If you see the bold part in my answer the question is really about the auto-generated code when you create a new JFrame through the New File wizard. OP even stated this: I want the look and feel code to look like this right after I click 'New > Jframe form':. Of course it's better set L&F at the start. But for test purposes you may want to "run" a particular JFrame and default L&F will be Nimbus which is what OP wants to change. @PhantasyAlexis
C
4
1. try {
2.        for (javax.swing.UIManager.LookAndFeelInfo info :  javax.swing.UIManager.getInstalledLookAndFeels()) {
3.            if ("*Windows*".equals(info.getName())) {
4.               javax.swing.UIManager.setLookAndFeel(info.getClassName());
5.                break;
6.             }
7.         }
8.    }

at line '3' just replace "windows" by "Nimbus" and put this code in main frame of application and call another frames in nested form it will automatically apply nimbus theam for all the forms.

Cinemascope answered 27/2, 2014 at 11:15 Comment(0)
P
3

See Changing the Look and Feel After Startup section of the java tutorial. You have to call like:

UIManager.setLookAndFeel(lnfName);    
SwingUtilities.updateComponentTreeUI(frame);    
frame.pack();

Where lnfName is the LookAndFeel name and frame is your JFrame Object.

Phantasy answered 27/2, 2014 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.