Stop hardcoding column numbers in your TableModel

If you always been using column numbers when implementing your TableModel, you’ll know that it a burden when you have to change the order of the columns.

If you normally do the following in your TableModel implementation:

    @Override public String getColumnName(int column) {
        switch(column) {
            case 0: return "Closing Date";
            case 1: return "Open Price";
            case 2: return "Day High";
            case 3: return "Day Low";
            case 4: return "Close< Price";
        return null;

You'll have to go through the source code and re-number each column whenever you need to change the order of the columns. There will be multiple places to update, such as the getColumnName and getValueAt methods. If you do complex rendering then you will also need to update the TableCellRenderer implementation as well.

To avoid hardcoding the column numbers, you can use enums. Enums are both more descriptive and have ordinal values.

Continue reading

An easier way to parse a CSV file

I personally use the opencsv library to parse csv (comma separated value) files. It is a simple and powerful library with a commercial-friendly license. You can use it to directly parse a CSV file, or use its JavaBean binding feature. I prefer to use the JavaBean binding approach as it removes the mundane work of converting text to Java native types.

Below is an example of how to use JavaBeans binding feature of the opencsv library.

I will parse the historical stock prices of Apple Inc. downloaded from Yahoo! Finance.

Continue reading

Opening files with native applications across multiple platforms

I was working on a project, which required the ability to export the contents of the table as a Microsoft Excel file. After saving the file, the file had to be open immediately by Microsoft Excel. However this is multi-platform application and needed to support at least Windows, Mac and Linux. What if the user didn’t have Microsoft Excel installed, but they did have another program capable of editing/viewing Excel files? Users may have installed alternatives such as Open Office, Libre Office, and Microsoft Office for Mac. How can a Java Swing program invoke an unknown native program without having to program to every native executable? After searching the web, there was an elegant solution.

Continue reading

Do more with JPopupMenu

Java Swing’s JPopupMenu is very versatile for displaying any type of information. It can be used for more than just display a popup menu. JPopupMenu inherits all the features of java.awt.Container including the ability to set a LayoutManager and add one or more arbitrary java.awt.Component.

Below are two examples demonstrating how to use a JPopupMenu to “popup” more complex components. The first show a popup with an array of buttons and the second example shows a popup whose content is a table.

Continue reading

Display a JList with a Background Image

Ever wanted to use an image as a background of your JList component? Unfortunately the Swing library does not provide a simple method to set a image as a background. In order to do so, you are force to do some custom painting inside the paintComponent method. In the case of a JList, our custom painting has to be performed inside a custom implementation of a ListCellRenderer. Check out the video below to see it in action.

Continue reading

Free commercial friendly icon library

Open Icon Library is a collection of free commercial software friendly icons in PNG and SVG formats. The collection have over 5000 unique icons. I can’t want to get my hands on the SVG version and convert them into Java classes using Java 2D to draw the icons. Come back soon for an update.

68 Vector-based Java Icons

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.

Continue reading

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"} );
		final JTable table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			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);

Continue reading