Category Archives: Swing

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 »

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 »

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 »

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 »

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.

READ MORE »

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 »

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 »

4 Ways to Capture a Screenshot

The ability to create a screenshot programmatically in Java code can be very useful. I personally use it to create my animated transitions.

Here are 4 ways to create a screenshot in Java.

READ MORE »