How to display Progress bar for a procedure call

前端 未结 2 846
逝去的感伤
逝去的感伤 2021-01-25 23:28

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

2条回答
  •  爱一瞬间的悲伤
    2021-01-25 23:58

    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 worker = new SwingWorker() {
        @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 list) {
            progressBar.setValue(list.get(list.size() - 1));
        }
    };
    worker.execute();
    

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

提交回复
热议问题