Source:
private void selectTable(String tableName) { if (worker != null && !worker.isDone()) { worker.cancel(false); worker = null; } worker = new SwingWorker<Integer, Void>() { @Override protected Integer doInBackground() throws Exception { Statement statement = null; ResultSet resultSet = null; try { statement = connection.createStatement(); resultSet = statement.executeQuery( "SELECT count (*) as count FROM " + tableName); return resultSet.getInt("count"); } catch (SQLException ex) { LOGGER.log(Level.SEVERE, "Failed to get row count for table " + tableName, ex); throw ex; } finally { if (null != resultSet) { resultSet.close(); } if (null != statement) { statement.close(); } } } @Override protected void done() { super.done(); try { numRows = get(); numEntriesField.setText(numRows + " entries"); currPage = 1; currPageLabel.setText(Integer.toString(currPage)); numPagesLabel.setText(Integer.toString((numRows / ROWS_PER_PAGE) + 1)); prevPageButton.setEnabled(false); if (numRows > 0) { nextPageButton.setEnabled(((numRows > ROWS_PER_PAGE))); readTable(tableName, (currPage - 1) * ROWS_PER_PAGE + 1, ROWS_PER_PAGE); } else { nextPageButton.setEnabled(false); selectedTableView.setupTable(Collections.emptyList()); } } catch (InterruptedException ex ) { LOGGER.log(Level.SEVERE, "Interrupted while getting row count from table " + tableName, ex); JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), ex.getMessage(), Bundle.SQLiteViewer_selectTable_errorText(tableName), JOptionPane.ERROR_MESSAGE); } catch (ExecutionException ex) { LOGGER.log(Level.SEVERE, "Unexpected exception while getting row count from table " + tableName, ex); JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), ex.getCause().getMessage(), Bundle.SQLiteViewer_selectTable_errorText(tableName), JOptionPane.ERROR_MESSAGE); } } }; worker.execute(); }

Target:
private void selectTable(String tableName) { if (worker != null && !worker.isDone()) { worker.cancel(false); worker = null; } worker = new SwingWorker<Integer, Void>() { @Override protected Integer doInBackground() throws Exception { Statement statement = null; ResultSet resultSet = null; try { statement = connection.createStatement(); resultSet = statement.executeQuery( "SELECT count (*) as count FROM " + tableName); return resultSet.getInt("count"); } catch (SQLException ex) { throw ex; } finally { if (null != resultSet) { resultSet.close(); } if (null != statement) { statement.close(); } } } @Override protected void done() { super.done(); try { numRows = get(); numEntriesField.setText(numRows + " entries"); currPage = 1; currPageLabel.setText(Integer.toString(currPage)); numPagesLabel.setText(Integer.toString((numRows / ROWS_PER_PAGE) + 1)); prevPageButton.setEnabled(false); if (numRows > 0) { nextPageButton.setEnabled(((numRows > ROWS_PER_PAGE))); readTable(tableName, (currPage - 1) * ROWS_PER_PAGE + 1, ROWS_PER_PAGE); } else { nextPageButton.setEnabled(false); selectedTableView.setupTable(Collections.emptyList()); } } catch (InterruptedException ex ) { LOGGER.log(Level.SEVERE, "Interrupted while getting row count from table " + tableName, ex); JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), ex.getMessage(), Bundle.SQLiteViewer_selectTable_errorText(tableName), JOptionPane.ERROR_MESSAGE); } catch (ExecutionException ex) { LOGGER.log(Level.SEVERE, "Unexpected exception while getting row count from table " + tableName, ex); JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), ex.getCause().getMessage(), Bundle.SQLiteViewer_selectTable_errorText(tableName), JOptionPane.ERROR_MESSAGE); } } }; worker.execute(); }