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
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.
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)
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.
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.
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
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
}