Sunday, August 30, 2009

Swing ResultSet Column Table Component

Hear is a ready to use component for JTable in swing which will also show column titles. By default the JTable only displays the data which is strange although. This code is free and can be used easliy in commercial and opensource applications.
Yet this component does not have alignment facility depending on Integer or String or Date datatypes. Will update once done.

Steps to use the class
1. Create a class in package src.components.
2. Create a JTable and add it to JScrollPane.
JTable tblSuppliers = new JTable();
JScrollPane scrollpane = new JScrollPane(tblSuppliers);
3. Create the ResultSet,Create Model and set Model.
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strQuery);
ResultSetTableModel qtm = new ResultSetTableModel();

Code for is as follows.
package src.components;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class ResultSetTableModel extends AbstractTableModel {
Vector cache;

int colCount;

String[] headers;

String currentURL;

public ResultSetTableModel() {
cache = new Vector();

public String getColumnName(int i) {
return headers[i];

public int getColumnCount() {
return colCount;

public int getRowCount() {
return cache.size();

public Object getValueAt(int row, int col) {
return ((String[]) cache.elementAt(row))[col];

public void setResultSet(ResultSet rs) {
cache = new Vector();
try {
ResultSetMetaData meta = rs.getMetaData();
colCount = meta.getColumnCount();

headers = new String[colCount];
for (int h = 1; h <= colCount; h++) {
headers[h - 1] = meta.getColumnName(h);

while ( {
String[] record = new String[colCount];
for (int i = 0; i < colCount; i++) {
record[i] = rs.getString(i + 1);
} catch (Exception e) {
cache = new Vector();


Saturday, August 29, 2009

Browser Embed In Swing

SWT Browser component is quite handy when you want to include support for platform independent browser support from your swing application.
Its more over important when you want to show modal window with a browser, because in case of non modal window you can anyways start IE instance from Runtime call.

The benefit of using Browser component in Swing is that it gives you enough control over the contents you can change anytime from website.
Also its saves you the time of rewriting the stuff written in Website again in Swing or AWT. Same stuff can be used here by directly pointing to Website Form/Page URL.

You have 2 options to go with.
1. and
2. org.eclipse.swt.browser.Browser

I would suggest you to go with SWT Browser component.
One problem you will face with JWebBrowser is that the component creates problem with java 1.6. Its makes whole application
go crazy and all screens seams to get repaint problems.
Another problem you will face in JWebBrowser is that the component shows Custom window then a call is made in
HTML page. You might like to open it in Default webbrowser of your operating system.
It allthemore becomes unacceptable when you have shown JWebBrowser component in JDialog window and you have clicked on link.
It will show a window which is kind of hung and you cannot scroll or do any thing with the new window until you close the modal window.
One more problem you will encounter with JWebBrowser is that if you want to decide if opening of new window is to be allowed should be allowed
or not on the basis of URL location clicked, you have to
make a hidden JWebBrowser Component,
set it as browser instead of new window browser.
Dispose the new window using arg0.getNewWebBrowser().getWebBrowserWindow().dispose() in windowWillOpen open.
Ensure that you dispose the new window before setting the webbrowser.
Cancel the Opening of URL in locationChanging event and get the URL.
These long steps are to be followed if you want to intercept the opening up of new Window.

Good thing about JWebBrowser is that it is licensed under LGPL. So you can use it in your commercial projects also. You need not publish your code to users in case of LGPL.
A word for Christopher Deckers, author of JWebBrowser. Please make it work under Java 1.6. Just a call to starts the problems of repainting the windows.

However going with SWT Browser is not a eazy path either. You may face problems like
1. JVM Crashes
2. Not fitting easily in JDialog window and fits in JFrame.
3. Browser opens up in JDialog only once and dont come up next time onwards.
4. Your JDialog window shows up but the Browser dont show up.

To avoid JVM Crashes ensure that your window creation and displaying is all in SwingUtilities.invokeLater.
Let the shell loop be out of SwingUtilities.invokeLater. To ensure that JDialog and Browser both shows up,
you set location, size and visible for canvas, shell and browser component.
To ensure that the browser always comes up on window you might have to make use of small sleeps of 1 second outside your SwingUtilities.invokeLater.
This is to ensure that Window is created and shownup on screen before you try to set the browser component on the window.

The code written using SWT Browser component is completely platform independent. You can run vncserver service on your linux box and connect to it using vncclient and see your
application running in Linux environment using MOZILLA browser instead of IE.

If you need any advice or help on the topic please leave a comment here.