0

I can load data from query result in JTable nicely.

SELECT * FROM 'customer info';

Make a result set and place to JTable is easy to handle.

But problem is, as my database table is so big and it takes so much time to load. My application totally freeze until current task complete. I know swing worker can perform a background task. I study this, but i find no any appropriate solution in my case. Because i already write a lot of function in basic way.

So finally what i need?

I need a Class, design with swing worker by which i can easily use it's object anywhere in my application. suppose i send my query string and JTable in this class constructor. Then it automatically starts a background thread to make result set and place it in JTable.

  • 1
    This class extend SwingWorker and in your overrided method doInBackground() you can put your query. Some examples can be found to http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html. Constructor can read JDBC initialization. – Mihai8 Aug 12 '15 at 23:57
  • interesting..thanks @user1929959 – Mahbubur Rahman Khan Aug 13 '15 at 00:00
  • [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) would be a start – MadProgrammer Aug 13 '15 at 00:06
  • thanks. its helpful :) but @MadProgrammer . i need some more help from you. Would you give me an example for my case. Your little effort all-time save my day, and i really thanks for you. – Mahbubur Rahman Khan Aug 13 '15 at 00:15
  • For [example](http://stackoverflow.com/questions/20944719/how-to-use-swingworker/20945255#20945255) – MadProgrammer Aug 13 '15 at 00:25
  • its awesome man...dashing example ... you are really a good programmer and analyzer .... @MadProgrammer – Mahbubur Rahman Khan Aug 13 '15 at 00:29
  • @MahbuburRahmanKhan I just steal other peoples ideas and make them work for my requirements ;) – MadProgrammer Aug 13 '15 at 00:29

1 Answers1

0

ok now its work in my case :) here is my own solution ....

public class UsableSwingWorkerThread extends SwingWorker<ResultSet, Object> {

JTable table;
String query;


public UsableSwingWorkerThread(JTable table, String query) {
    this.table = table;

    this.query = query;

}

@Override
protected ResultSet doInBackground() throws Exception {

    return DatabaseFunctionClass.con.prepareStatement(query).executeQuery();
}

void loadTable(TableModel tb) {
    new UsableDefaultLoadTable(tb, table);
}

@Override
protected void done() {
    try {
        loadTable(DbUtils.resultSetToTableModel(get()));            
    } catch (Exception ignore) {
        StaticAccess.showMassageDialog(StaticAccess.mainFrame, "Fail to load. try again later..", ignore);
    }
}

}