How to display Progress bar for a procedure call

六眼飞鱼酱① 提交于 2019-12-02 11:09:57

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.

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

Read tutorail for SwingWorker and Concurency in Swing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!