2

Hey I'm new to netbeans and I noticed a lot of applications (from textbooks) have a default style/appearance to their controls (buttons etc) as shown below.

Valid XHTML
(source: iforce.co.nz)
.

the appearance when I'm creating a GUI is just the standard windows xp or 7 button style. Is there a way to change this to the style shown in the image above?

Here is the appearance I am currently getting:

Valid XHTML
(source: iforce.co.nz)
.

Thanks in advance.

Community
  • 1
  • 1
Ari
  • 1,026
  • 6
  • 20
  • 31

4 Answers4

4

Yes, you can give Swing a Windows like look and feel with the following code:

try{
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e){
        System.out.println("UIManager Exception : "+e);
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
gprathour
  • 14,813
  • 5
  • 66
  • 90
  • 2
    That won't work on Mac or *nix, use [`UIManager.getSystemLookAndFeelClassName()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/UIManager.html#getSystemLookAndFeelClassName%28%29). – Andrew Thompson Nov 23 '11 at 05:34
  • this was for Windows only. :) – gprathour Nov 23 '11 at 05:35
  • You seem to have missed the point. Does it work using other JREs for Windows? Will it work when Oracle decides to change the class name to `com.oracle.java.swing.plaf.windows.WindowsLookAndFeel`? ***Don't use magic numbers!*** – Andrew Thompson Nov 23 '11 at 05:42
  • And while I am here. Note that `e.printStacktrace()` is both shorter to type, and more informative than, `System.out.println(e.toString())`. – Andrew Thompson Nov 23 '11 at 05:44
  • 1
    I thought the point was that the asker **didn't** want the Windows L&F. – BitFiber Nov 23 '11 at 05:45
2

NetBeans will automatically choose a Look and Feel depending on your JDK and operating system. NB generated some code to set the L&F when you created the JFrame which made everything look like Windows components. If you want to change the L&F, look at the source for your JFrame and look for a collapsed bit of code that says something like "Look and feel setting code." If you expand it you can change it as you like, or even delete it, which will cause it to simply use the default L&F ("Metal"), which is the one in your picture. Bear in mind that you really shouldn't really just delete generated code, but I'm just trying to make a point here. If you're new to swing in general, I'd recommend writing some applications by hand, and they should just use the "Metal" L&F by default. This will allow you to get comfortable with working with swing. See here for more information.

BitFiber
  • 476
  • 1
  • 5
  • 13
2

See the nested layout example for code that offers a combo containing the available PLAFs, and allows the user to change the PLAF at run-time.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

You can add Look and Feels. There are some free great looking ones which can be downloaded freely. If you only want Windows look and feel you can just add

try{
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e){
        e.printStacktrace();
}

Hope this answers your question.

Krishan
  • 641
  • 4
  • 18
  • That won't work on Mac or *nix, use [`UIManager.getSystemLookAndFeelClassName()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/UIManager.html#getSystemLookAndFeelClassName%28%29). – Andrew Thompson Nov 23 '11 at 05:34
  • And while I am here. Note that `e.printStacktrace()` is both shorter to type, and more informative than, `System.out.println(e.toString())`. – Andrew Thompson Nov 23 '11 at 05:44
  • *"Yes, thanks"* No worries, but, edit your answer and give me a chance to reverse the down-vote (is one suggestion). – Andrew Thompson Nov 23 '11 at 08:35