I was searching for some icons to use in my Swing GUI program and came across the Tango Desktop Project. It is an icon library for free and open source software. Instead of just taking the icon images from the library, I took it a step further. I use the SVG version of the icon and created an Icon java class that uses Java 2D to draw the similar icons. The biggest benefit is you can create an icon of any size without losing image quality. You are free to download and use the icons in your GUI programs.
JTable Mastery: convertColumnIndexToModel
Study the code below. There is something wrong with it! Can you guess what it is?
public class JTableViewToModelColumn {
public static void main(String[] args) {
JFrame f = new JFrame("View To Model Column Example");
final DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new String[]{"Col A", "Col B", "Col C"} );
model.setRowCount(10);
final JTable table = new JTable(model);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int col = table.columnAtPoint(e.getPoint());
System.out.println("You click on column " + model.getColumnName(col));
}
});
f.add(new JScrollPane(table), BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
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.
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.
4 Ways to Capture a Screenshot
Java Swing better than JavaFX?
Bjorn Roche wrote about a JavaFX project that was dropped in favor of using Swing inside an Applet. Some reasons for the failures were immature APIs, stability and performance. These are the risk you take when adopting new technologies and hopefully will improve soon. There was one point that stands out and I do agree with:
Swing has a reputation for being ugly, simply because most of the included Look and Feels are ugly
Since Swing components are lightweight, you have more control over the look and feel. You can make it as rich or as ugly. At least you have a choice!
At this point, JavaFX has a long way to go before it can replace Swing.
How to format numbers in a JTable with a NumberCellRenderer
By default, the JTable show the cell values as regular text using a JLabel. To display numbers properly, you are force to pre-format the number as string values before setting them in the table model. There is a better way, by using your own implementation of TableCellRenderer. This will allow you to store the values in their intrinsic form inside the table model.
Why adopt a Rich Client Platform (RCP)?
Ten years ago, I became a founding member of a company on a quest to build a tool for Fixed Income Analysis. Our goal was to build our own rich client desktop application for performing analysis in a niche market. If we had to do it again today, I would highly recommend NetBeans RCP and save ourselves much of the pain of rolling your own desktop platform. Leveraging NetBeans RCP would have allowed us to focus on delivering more of our core values.
How to create a custom Swing Border
Eventually every Swing developer comes to a point where the borders packaged with Java Swing is considered too plain. Either you will be googling for your next border or you become adventurous and create your own border. This tutorial is for the adventurous. I will teach you how to create your own dashing border. Anyone who done web development is already familiar with the border styled with alternating lines and spaces.

