Saturday, September 27, 2008

Setting Look And Feel when start up

This is how you can set the look and feel style for your java pplication.
It'll set the look and feel according to the platform regardless of java default style.
This is very useful when you want to start java application from another application and to make the same style with window default.
This method must have called when start the application. Just call this in main implementation

public static void setLookAndFeel() {
try {
UIManager.setLookAndFeel(getPreferredLookAndFeelForOS());
} catch (ClassNotFoundException e) {

} catch (InstantiationException e) {

} catch (IllegalAccessException e) {

} catch (UnsupportedLookAndFeelException e) {

}
}



private static String getPreferredLookAndFeelForOS() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.indexOf("mac") || osName.indexOf("win"){
return UIManager.getSystemLookAndFeelClassName();
} else {
return UIManager.getCrossPlatformLookAndFeelClassName();
}
}

No comments: