Monthly Archives: April 2010

How to add a Gradient Background to any JComponent

Almost every “web 2.0″ application have a cool linear gradient background instead of the plain old boring single color solid background. Adding a gradient background to your application is very simple with only a few lines of additional code. In the example program, I am using Java2D and the GradientPaint class to draw the linear gradient background.

Below is the program that I used to create the image above. We override the paintComponent of the JPanel (or any JComponent) and fill the background with a gradient paint. The begin and end colors for the gradient paint are derived from the component’s background color using the brighter and darker variations.

READ MORE »

Command Chaining: From JQuery to Swing

JQuery is my favorite Javascript library for developing web-based applications. One of the things I really like is the ability chain commands together to reduce the code size. Here is an example from JQuery to hide and add an event handler to an HTML element.

$(document).ready(function() {
   $('#faq').find('dd').hide().end().find('dt').click(function() {
     $(this).next().slideToggle();
   });
 });

Inspired by the JQuery, I started to use the same command chaining technique in my Swing programs and libraries. I been spending some time developing an animation library designed with command chaining at the core.
READ MORE »

How to create a Callout Border

Using the power of Java2D, I was able to create more advance and stylish borders. Using a combination of a round rectangle and a triangle shape, I created the callout border shown on the left. This is also very similar to the balloon tips on the Windows OS. Decorate a JLabel with a callout border and you got an awesome tooltip replacement.

Here is how I did it…

READ MORE »