Showing posts with label GUI. Show all posts
Showing posts with label GUI. Show all posts

Saturday, June 20, 2009

How to set Nimbus as look and feel in Java GUI

Recently,in the Java SE Update N, Nimbus was provided as one of the bundled look and feels. It is a good looking L&F and if someone has worked in the new Solaris then one can understand what Nimbus is all about.
To use Nimbus in your Java application , the below code will work:-
try {

UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e)
{

}
This is a straight way to use Nimbus, however the problem with this code is that though it will work now, its a possibility that in Java 7, this way of using Nimbus can create problem as the location of Nimbus L&F class can change.

So a better way of using Nimbus is to find all the installed L&Fs by using UIManager.getInstalledLookAndFeels() ,then check if Nimbus is present and then use the getClassName to invoke the L&F.

The below steps will make it clear:-

UIManager.LookAndFeelInfo plafinfo[] = UIManager.getInstalledLookAndFeels();
boolean nimbusfound=false;
int nimbusindex=0;

for (int look = 0; look < plafinfo.length; look++) {
if(plafinfo[look].getClassName().toLowerCase().contains("nimbus"))
{
nimbusfound=true;
nimbusindex=look;
}
}

try {

if(nimbusfound)
{
UIManager.setLookAndFeel(plafinfo[nimbusindex].getClassName());
}
else

UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());

}
catch(Exception e)
{
}

By making use of this code, we make sure that our application will be able to use the Nimbus L&F even if the location of Nimbus L&F class changes in future version of Java.
Hope you liked the above information!!

Thursday, June 12, 2008

My Experiences with Java GUI and Swing

My experiences with Java regarding its GUI arose out of some serious coding in Swing. As i went deep into some serious stuff coding in Java, i always wanted to try out new features in Java ,in which i had little knowledge. After trying out basics of working of Java, i jumped into programming of Swing, as GUI coding always looked fascinating to me!!
But certainly, i had no clue about the dangerous territory where i was heading.
This was the fact i realized, when i started coding the Swing stuff.
The elements just never looked to obey my orders in the code. It was a real hard work to really control the looks of GUI. Then learning the Layouts, the rules which the components follow, and combining them with Event Handling Functionalities..
My first real GUI program with a bit of good coding was to display a list of files and directories in a user selected directory from the JFileChooser class, all in a GUI format. Initially i felt whats the big deal in it, but for a beginner, it was certainly a bit hard. But as i got success in it, my confidence grew in coding GUI programs.
Certainly that confidence helped me alot to take up more challenging coding in GUI and Image Handling in Java, which ultimately helped me to learn more about the way Java works.......

Share this Page:-