0

I have data stored in Jtable, and then I am taking this data row by row and passing this data to a callable procedure in database(1 call for each row), and Procedure returns the message once completed, this process takes almost 1-2 min for each call, can someone tell me how can I display a single progress bar for whole process. (I mean 1 progress bar for all the call), I want Progress bar to be finished once loop ends.

My code is below:

DefaultTableModel model = (DefaultTableModel) data.getModel();
for (int count = 0; count < model.getRowCount(); count++) {
    CallableStatement csmt = con.prepareCall("{call CODE_REVIEW.PRC_WRAPPER_REVIEW(?,?,?,?)}");

    String folder=model.getValueAt(count, 0).toString();
    String type=model.getValueAt(count, 1).toString();
    String name=model.getValueAt(count, 2).toString();

    csmt.setString(1,name);
    csmt.setString(2,type);
    csmt.setString(3,folder);
    csmt.registerOutParameter(4,java.sql.Types.VARCHAR);
    csmt.execute();
    String messege =csmt.getString(4);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3065757
  • 475
  • 1
  • 5
  • 14
  • for this you have to understand what it eh EDT (Event Dispatch Thread) and use a thread to perform the loop and update the progress bar in the GUI. Usually SwingWorker thread is used as it is easy to implement. – HpTerm Dec 10 '13 at 15:20
  • you should check here where I give an example : http://stackoverflow.com/questions/10773552/jprogressbar-update-from-swingworker – HpTerm Dec 10 '13 at 15:20

2 Answers2

3

You can use a SwingWorker to do the calls in the background and update the progress bar in the Event Dispatching Thread (a.k.a. EDT). Something like this:

SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
    @Override
    protected Void doInBackground() throws Exception {
        int processed = 0;
        DefaultTableModel model = (DefaultTableModel) data.getModel();
        for (int count = 0; count < model.getRowCount(); count++) {
            //...
            String messege = csmt.getString(4);
            processed++;
            publish(processed / model.getRowCount() * 100);
        }
        return null;
    }

    @Override
    protected void process(List<Integer> list) {
        progressBar.setValue(list.get(list.size() - 1));
    }
};
worker.execute();

Take a look to Worker Threads and SwingWorker section in Concurrency in Swing trail.

dic19
  • 17,821
  • 6
  • 40
  • 69
1

You need to use SwingWorker for updating UI while you do long background process.

Read tutorail for SwingWorker and Concurency in Swing.

alex2410
  • 10,904
  • 3
  • 25
  • 41