Advertisment

Intuitive database display with JNTable

author-image
CIOL Bureau
Updated On
New Update

In a Java application, when it comes to displaying tabular

data -usually from a database-the quickest choice is to use Swing's JTable. This

choice is followed by the struggle to display the result set in the JTable, make

the data render nicely, and make it sortable and filterable. 



Developers working

on Microsoft platform enjoy the concepts of DataSet and DataGrid to achieve all

this quickly. Out of box, they provide a functional output without much coding.

Bringing in the same ease and functionality to Java is a package called JNDC

(Java Desktop Network Components).

































Direct

Hit!
Applies

to:
Java developers
USP:

Use the .NET like concepts of DataSet, data

binding and adapters to display an intuitive data-aware table in a Java

app
Primary

Link:
href="https://jdnc.dev.java.net">https://jdnc.dev.java.net
Google

keywords:
java DataSet

table


JDNC is a set of components like JNTable, JNTree, JNEditor

and  JXLoginPanel that are readily usable

in a Java Swing application. 



In this article we will focus on JNTable which is a

JTable-like Swing component.



 JDNC also provides classes such as DataProvider,

DataSet,  DataTable, DataColumn,

TabularDataModelAdapter (adapter) and DirectTableBinding (for binding) which can

be used to display data from a database in a JNTable. Note that the naming

convention of these classes and also the concept of their use (as we will see

later) is similar to those in a Microsoft .NET application. In place of

DataGrid, we have JNTable from JDNC which provides sorting and filtering with

minimal coding. 



Download JDNC

JDNC is a one of the numerous

open-source projects hosted at www.java.net. You can download the latest (0.7 as
of this writing) JDNC package from https://jdnc.dev.java.net/. Extracting the

downloaded archive will show up a file named jdnc-0_7-all.jar in the lib

subdirectory. This is the JAR file that contains the various JDNC components and

libraries. So an application that uses JDNC must be compiled and run as

follows.

Advertisment

javac —classpath style="mso-spacerun: yes">  jdnc-0_7-all.jar;.

java

—cp jdnc-0_7-all.jar;.

Steps to code

Let's now see how to query a database

and display the results in a JNTable. To achieve this with JDNC, create a JDBC
connection to the database and create a DataProvider. Next set the query to be

executed. Now create a DataSet and a DataTable in the DataSet and then create

columns in the DataTable. In the next step, load the DataTable with data from

the query and create an adapter to the DataTable. In the end bind the adapter to

the JNTable. Note that we will have to import the following classes or packages

of JDNC.

import org.jdesktop.jdnc.JNTable;

import

org.jdesktop.dataset.provider.sql.*;
import org.jdesktop.dataset.*;

import

org.jdesktop.dataset.adapter.*;
import

org.jdesktop.swing.binding.DirectTableBinding;
import

org.jdesktop.swing.decorator.*;


Advertisment

Create a JDBC connection

Since JDBC is the

underlying protocol to connect to any database in Java, we begin with connecting

to a database using the class JDBCDataConnection class (also a part of JDNC).
Below is the syntax for this.

JDBCDataConnection dataConnection

=


          Â

new

JDBCDataConnection("",
face=Verdana size=1>

                                 
face=Verdana size=1>"", style="mso-spacerun: yes"> size=1>

                                  size=1>"","");

dataConnection.setConnected(true);

An example of using JDBCDataConnection to connect to a MySQL

database using MySQL Connector/J driver is:

Advertisment

JDBCDataConnection dataConnection

=   style="mso-tab-count: 1">
           new

JDBCDataConnection("org.gjt.mm.mysql.Driver",
style="mso-spacerun: yes"> size=1>

                                  size=1>"jdbc:mysql://localhost/databasename", style="mso-spacerun: yes"> size=1>

                             size=1>      "root","secret");

With dataConnection.setConnected(true), the application will

connect to the database.



Setting up the query

The DataProvider

A DataProvider abstracts the

underlying data and allows one to write his own DataProvider-even for non-RDBMS

data such as XML, metadata and hierarchical data. When dealing with RDMBS, we
can use the already implemented SQLDataProvider as:

dataProvider = new

SQLDataProvider();

dataProvider.setDataConnection(dataConnection);

Advertisment

Note that in the second statement we set the connection of

the DataProvider to the JDBCDataConnection we created. Now we can forget about
the JDBCDataConnection and use only the SQLDataProvider object (DataProvider in

this case) to query or load data from the database.



Set up the query

To set up the query, issue:

TableCommand cmd = new

TableCommand("
","

");

dataProvider.setTableCommand(cmd);

       
  

 













    src="http://www.pcquest.com/2006/Images/jtable_jan2k6.jpg" width=312 border=0>

The JTable demo showcased at the Sun Microsystems, PCQuest Summit

for Developers held in December 2005


The simplest way to set up a database query to be

issued is to use the TableCommand class. It takes the name of the table and an

optional where clause (say, where id>10) as the arguments and fires a select

query against the database.



The local table

Next using a DataSet we create a

local representation of the table that we want to display.

Advertisment

DataSet ds = new DataSet();

DataTable table =

ds.createTable();
table.createColumn("


Using the createColumn( ) method, we will need to create a

column each (with the same name as the database table's columns). Next, with the

following lines:

size=1>table.setDataProvider(dataProvider);

table.setName("mytable");

Advertisment

Set the DataProvider for the table to dataProvider object, we

created.  We also give a name to the
table (mytable in this case) which will be used later for creating an adapter.

The following statement will load all the resultant data (of the database query)

into the DataTable.

size=1>dataProvider.load(table);

Adapter and binding

The adapter is created as

follows.

Advertisment

TabularDataModelAdapter

dataModelAdapter =

 
new

TabularDataModelAdapter(ds.getTable("mytable"));

Then create a JNTable and bind it with the adapter as

follows.

JNTable jnTable = new JNTable();

binding = new

DirectTableBinding(jnTable.getTable(), dataModelAdapter);

We can now set the size of the jnTable object, as with any

Java swing component (using setSize( ) method) and add it to any container like

a JFrame or Jpanel. Compiling and running the application yields a nice looking

table, which looks slightly better than JTable. Click on the columns to sort the

data. Note that we did not write any explicit code to do this. Also note the

intuitive sort indicators that indicate the order of sorting.



Filtering the data

We can filter the data displayed

in the JNTable using regular expressions as follows.

FilterPipeline filters = new

FilterPipeline(

 
new Filter<> { style="mso-spacerun: yes">

 new PatternFilter(

,java.util.regex.Pattern.CASE_INSENSITIVE,

) style="mso-spacerun: yes">
  style="mso-tab-count: 1">                       } style="mso-spacerun: yes">

          Â

);

this.jdncTable.setFilters(filters);

Substitute with any regular expression and

with the number of the column, on the JNTable, on which we

want to apply the filter. Note that the column number starts from zero (0). For

more refer on JNTable and JDNC refer to documentation at href="http://jdnc.dev.java.net/documentation/index.html">http://jdnc.dev.java.net/documentation/index.html

tech-news