views:

333

answers:

8

Is it possible to use Java to create apps that look native on Windows? I don't care if the solution is portable or not, because I only plan to target windows users. I am using Scala if that matters.

Sorry for the lack of details, but I have never used Java before so I'm not even sure if this is possible.

A: 

Yes, Java does have a Windows-native look and feel available on Windows. Look up how to change your look-and-feel and that should take you in the right direction.

Michael E
+12  A: 
try {
    // Set the Look and Feel of the application to the operating
    // system's look and feel.
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException e) {
}
catch (InstantiationException e) {
}
catch (IllegalAccessException e) {
}
catch (UnsupportedLookAndFeelException e) {
}

That should set the Look and Feel to the system look and feel. You would do this before any of your GUI code. For example, in your main method.

If you want to learn more about Look and Feels, I would check out the Java Tutorial on them, as suggested by carwash.

Thomas Owens
Implied in this answer is *use Swing for controls*. This is the conventional Java wisdom and the official Sun package, and no one can be blamed for advising it. However, you will not get native controls and the faked-up ones will not fool anyone. Sun should have accepted SWT when Eclipse offered it to them for free. As it is, Sun only offers widgets (Swing and AWT) that are not suited for desktop client apps, and as a result they have almost none and seem to have kind of dropped Swing other than keeping it limping along.
DigitalRoss
I use Swing and I'm "fooled by the faked-up" controls. Explain how Swing does not look and act like Windows if you use this code.
Thomas Owens
It sucks on Macs, quite badly, in fact.
AlBlue
@Thomas Owens, OK, how about the file chooser on windows. It looks different, does keyboard focus different, doesn't create directories the same way, doesn't cd to a created directory, selects the file when I type return rather than entering the selected directory, does selection differently, doesn't respect system font configuration, and doesn't follow the system icon convention.
DigitalRoss
DigitalRoss: I don't see a difference between the two, when you use the system look and feel.
Thomas Owens
A: 

You'd do something like:

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

(Which of course works only on Windows.) The result looks and feels reasonably native. More info e.g. here.

Jonik
This would only work on Windows. If you were on a different system, an `UnsupportedLookAndFeelException` would be thrown.
Thomas Owens
Note that OP said "I only plan to target windows users". And yeah, I did omit the try-catch to focus on the main point. (It's a checked exception so compiler would remind anyone who forgot it from real code :)
Jonik
+4  A: 

See here: Java™ Tutorials: How to Set the Look and Feel

try {
    // Set System L&F
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} 
catch (UnsupportedLookAndFeelException e) {
   // handle exception
}
carwash
+2  A: 

You have to use Windows look and feel.

You can specify it at the command line:

java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel MyApp

Or in code

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Here are the details: How to set the look and feel

OscarRyz
A: 

Your should use native Look&Feel

St.Shadow
+7  A: 

Everyone else has posted Swing things, so I'm going to play Devil's advocate and mention SWT.

SWT is a widget toolkit produced by the Eclipse foundation. It is a thin wrapper over the system's native GUI... for Windows, OSX, and various flavors of *nix (Linux, AIX, BSDs?, etc...).

This is the opposite route that Sun's JFC/Swing took, which draws its own components.

R. Bemrose
I'm curious as to what the advantage of using SWT over Swing is, outside of Eclipse RCP development. I believe most OSes are covered by UIManager.getSystemLookAndFeelClassName(), so why would I want to bring in an external library for this purpose?
Thomas Owens
I haven't done a lot of desktop app development, but the major advantage is that it uses the OS's native rendering library rather than drawing its own components like Swing does.
R. Bemrose
Good high level SWT vs Swing here: http://www.developer.com/java/other/article.php/10936_2179061_2/Swing-and-SWT-A-Tale-of-Two-Java-GUI-Libraries.htm
Smalltown2000
Well, why would anyone want Swing? It comes with all the bugs particular to Swing, and none of the advantages specific to each OS. When you use SWT, you make optimal use of the OS GUI facilities, and your applications don't "look" like they belong in the OS. They _belong_ in the OS.
Daniel
Hmm, let's see. I could use Swing, which entirely tries to fake up a native-appearing widget, but never works exactly right and sometimes doesn't even work close to right. It usually looks like the last release of the native OS, except when it looks like two releases past. OR, I could use SWT, which would, what-an-amazing-idea, ACTUALLY CALL THE NATIVE WIDGET, which is faster, more reliable, what the user is expecting, and always looks exactly like the running OS release, because it IS the running OS release.
DigitalRoss
+1  A: 

It's strange no one has mentioned JGoodies yet.

The JGoodies Windows look&feel focuses on a precise emulation on Windows 95/98/NT/ME/2000 in the following areas: menus, icons, colors, borders, fonts, font sizes, insets, and widget dimensions. It honors the screen resolution (96dpi vs. 120 dpi) to adjust sizes, insets, and widget dimensions. (Source)

antispam