One of the components of Java Desktop Network Components allows an intuitive — sortable and filterable — display of tabular data with a few lines of code
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
achive 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).
|
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.
javac —classpath
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.*;
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("
"
"
dataConnection.setConnected(true);
An example of using JDBCDataConnection to connect to a
MySQL database using MySQL Connector/J driver is:
JDBCDataConnection
dataConnection =
new
JDBCDataConnection("org.gjt.mm.mysql.Driver",
"jdbc:mysql://localhost/databasename",
"root","secret");
With dataConnection.setConnected(true), the application
will connect to the database.
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);
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);
|
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.
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:
table.setDataProvider(dataProvider);
table.setName("mytable");
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.
dataProvider.load(table);
Adapter and binding
The adapter is created as follows.
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<> {
new PatternFilter(
}
);
this.jdncTable.setFilters(filters);
Substitute
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
http://jdnc.dev.java.net/documentation/index.html
Shekhar Govindarajan, IT4Enterprise