How to display line numbers in JTable

Its unfortunately that Java Swing does not include a feature to show the line number for each row on a JTable. This would be similar to the line numbers shown in a spreadsheet application. Fortunately, Swing’s flexibility and extensibility allows us to create one with ease.

Most of the solutions out there are based on using two tables side-by-side. One to represent the main table and the other to display the line numbers. There are event handlers listening on the main table to update the line number table.

I present another solution which is based on a single JComponent that will render the line numbers. This is lighter and more easily customizable in case you need to add additional features or behaviors.

READ MORE »

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.

READ MORE »

How to create a multi-line column header

If you didn’t already know, you can embed simple HTML inside all Swing text components including JLabel. The default renderer for a the column header is a JLabel component. In order to create a multiple row header, simply use HTML markup with a “<br>” HTML separating the rows.

READ MORE »

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.

READ MORE »

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.

READ MORE »

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.

READ MORE »

Improving the Swing GUI with seamless textured background images

With the simple use of seamless textured background images, you can make a dramatic improvement in the appearance of your Swing GUI. Seamless textures are great because you can tile them horizontally and vertically to fit any component size.

Here are some more examples with different textures:

READ MORE »

Focus Tracker Demo

I just finished writing a library to track the focus of each focusable component. The focus is highlighted with an outline created by four brackets around the focused component. Check out the video to see it in action.

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.

READ MORE »

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.

Web Analytics