Ruben Laguna's blog

Jan 13, 2010 - 4 minute read - Comments - beans beansbinding binding database db java jtable lazy loading pagination swing

JTable bound to a database with lazy loading

I’ve been doing experiments with JTables bound to database tables in the past. But I was not satisfied with the result to be honest. The pagination helps but there is still noticeable pauses each time a database query has to be issued. So I started looking into other possibilities.

I come up with the idea of doing lazy loading and presenting some fake data until the data is really retrieved from the database, I did a small proof of concept and seems to work ok.

The solution goes like this. It uses Beans Binding to bind a JTable to a custom List like in the previous post. This custom list (ResultListDerby in the example below) returns Beans (the Bean is called Customer in the example) that are initially empty. By empty I mean that all its properties return something like “<data not retrieved from the database yet>”. So the JTable will be initially full of “<data not retrieved yet>”.

The picture above shows the JTable showing the Listof Customers. Each row represents a Customer instance.

Having empty/fake Customer beans allow a fast drawing to screen but it’s not very useful. Now, if at the same point as each Customer is created we create SwingWorker and schedule it to run in a Executor then we can get something more meaningful. So what we do in the SwingWorker? The SwingWorker, you guessed it, will retrieve the data from the database and update the bean (look at ResultListDerby.getItem() method). When the bean ( is updated it will notify its listeners of the changes, automatically by means of bound properties , in this case JTableBinding will be notified and the JTable that will repaint the cells to reflect the changes in the Bean. (Check how bound properties are implemented with of PropertyChangeSupport in below). The rows will be updated one by one, as soon as the Executor completes a SwingWorker task a row will be updated.

This solution is free from annoying GUI freezes. The beans are always created fast because there is no need to go to the database to create them and that keeps the GUI liveness high. By moving the database access to SwingWorkers we keep the EDT (event dispatch thread) lightweight. The solution also avoids having too many running SwingWorkers at the same time by using a ExecutorService instead of executing them directly via the SwingWorker.execute() method (Check ResultListDerby.getItem() to see how it’s done).

package jtablepagination;

import java.lang.ref.WeakReference;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;

* @author Ruben Laguna <ruben.laguna at>
public class MainWindow extends javax.swing.JFrame {

  /**** Creates new form MainWindow */
  public MainWindow() {

  /**** This method is called from within the constructor to
  * initialize the form.
  * WARNING: Do NOT modify this code. The content of this method is
  * always regenerated by the Form Editor.
  private void initComponents() {
    bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

    list1 = getList();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();


    org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list1, jTable1);
    org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${firstName}"));
    columnBinding.setColumnName("first name");
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${lastName}"));
    columnBinding.setColumnName("last name");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)



  private List<Customer> getList() {
    List<Customer> toReturn = new ResultListDerby();
    return toReturn;

  * @param args the command line arguments
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new MainWindow().setVisible(true);

  // Variables declaration - do not modify
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTable jTable1;
  private java.util.List<Customer> list1;
  private org.jdesktop.beansbinding.BindingGroup bindingGroup;
  // End of variables declaration

class ResultListDerby extends AbstractList {

  private Connection connection;
  private final ExecutorService ex = Executors.newSingleThreadExecutor();
  private int size = -1;
  //maintain a cache with the Customer instances already created and alive
  private Map<Integer, WeakReference<Customer>> cache = new HashMap<Integer, WeakReference<Customer>>();

  ResultListDerby() {
    try {
      this.connection = DriverManager.getConnection("jdbc:derby://localhost:1527/customer", "nbuser", "nbuser");
    } catch (Exception ex) {
      Logger.getLogger(ResultListDerby.class.getName()).log(Level.SEVERE, null, ex);
      throw new RuntimeException(ex);

  public int size() {
    if (this.size == -1) {
      try {
        final ResultSet resultset = connection.createStatement().executeQuery("SELECT COUNT (ID) FROM CUSTOMERS");;
        final int toReturn = resultset.getInt(1);
        this.size = toReturn;
      } catch (SQLException ex) {
        Logger.getLogger(ResultListDerby.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    return this.size;

  public Customer get(int rowIndex) {
    //this way we ensure that we don't create several Customer instances
    //for the same id. Otherwise it would be confusing for beansbindings.
    Customer toReturn = null;
    if (null != this.cache.get(rowIndex)) {
      toReturn = this.cache.get(rowIndex).get();
    if (null == toReturn) {
      toReturn = getItem(rowIndex);
      this.cache.put(rowIndex, new WeakReference<Customer>(toReturn));

    return toReturn;

  private Customer getItem(final int j) {
    final Customer customer = new Customer(j);

    Runnable task = new SwingWorker() {

    private String firstNameValue;
    private String lastNameValue;

    protected Object doInBackground() throws Exception {
      //this is always executed in a different thread from the current thread
      //it doesn't matter if the current thread is the EDT or a thread in the Executor
      final java.sql.Statement stmt = connection.createStatement();
      firstNameValue = executeQuery.getString(2);
      lastNameValue = executeQuery.getString(3);
      return null;

    protected void done() {
      //this in the other hand will always be executed on the EDT.
      //This has to be done in the EDT because currently JTableBinding
      //is not smart enough to realize that the notification comes in another
      //thread and do a SwingUtilities.invokeLater. So we are force to execute this
      // in the EDT. Seee and

      Logger.getLogger(ResultListDerby.class.getName()).info("updating customer " + customer);

  //NOTE that we don do task.execute()
  //posting the task to an Executor gives more control on
  //how many threads are created.
  return customer;


package jtablepagination;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.logging.Logger;

* @author Ruben Laguna <ruben.laguna at>
public class Customer {

  private static final Logger LOG = Logger.getLogger(Customer.class.getName());
  private static final String NOT_RETRIEVED_YET = "<not retrieved yet>";
  private int id = 0;
  public static final String PROP_ID = "id";
  protected String firstName = NOT_RETRIEVED_YET;
  public static final String PROP_FIRSTNAME = "firstName";
  protected String lastName = NOT_RETRIEVED_YET;
  public static final String PROP_LASTNAME = "lastName";
  private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

  Customer(int j) { = j;

  public int getId() {
    return id;

  public String getFirstName() {
    return firstName;

  public void setFirstName(String firstName) {
    String oldFirstName = this.firstName;
    this.firstName = firstName;
    propertyChangeSupport.firePropertyChange(PROP_FIRSTNAME, oldFirstName, firstName);

  public String getLastName() {
    return lastName;

  public void setLastName(String lastName) {
    String oldLastName = this.lastName;
    this.lastName = lastName;
    if (propertyChangeSupport.hasListeners(PROP_LASTNAME)) {"notifying!!!");
    } else {"there is no listeners for the property");

    propertyChangeSupport.firePropertyChange(PROP_LASTNAME, oldLastName, lastName);

  public void addPropertyChangeListener(PropertyChangeListener listener) {"add listener to customer " +;

  public void removePropertyChangeListener(PropertyChangeListener listener) {

  public String toString() {
    return "id: " + id + " firstName: " + firstName;