0

I have a swingworker that will be representing a jProgressbar. This is the code

private Swingworker timeOfProccess;

class Swingworker extends SwingWorker<Object, Object> {

    @Override
    protected Object doInBackground() throws Exception {

        jProgressBar1.setStringPainted(true);
        int progress = 0;
        setProgress(0);

        while (progress <= 100) {
            jProgressBar1.setValue(progress);
            Thread.sleep(5);
            progress++;
        }
        mainProccess();
        return null;
    }

    @Override
    protected void done() {
        jProgressBar1.setValue(100);
        JOptionPane.showMessageDialog(null, "Proses Selesai");
        jProgressBar1.setValue(0);
        jProgressBar1.setStringPainted(false);
    }

}

private void btnExecuteActionPerformed(java.awt.event.ActionEvent evt) {                                           

    timeOfProccess = new Swingworker();
    timeOfProccess.execute();}

I dont know, why the progressbar running is uncontrolled. it is so fast to 100% even the process still working. But void done is success to pop-up the JoptionPane after main process end. where is I am lost in my code. thanks..

user2971238
  • 95
  • 1
  • 2
  • 9
  • Don't update the GUI from the background thread; use `setProgress()`, as shown in this [duplicate](http://stackoverflow.com/q/4637215/230513). – trashgod Jul 16 '14 at 16:28

1 Answers1

0

Visit How to Use Progress Bars where You will find good examples on progress bar along with detail description.

Don't use Thread.sleep() that sometime hangs the whole swing application instead try with Swing Timer that is most suitable for swing application.

Read more How to Use Swing Timers

sample code:

// delay for 1500 mill-seconds
Timer timer = new javax.swing.Timer(1500, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // call for your next task
    }
});
timer.setRepeats(true); // you can turn off the repetition
timer.start();

How to get the progressbar showing the number 1 - 100 %.

Use JProgressBar#setStringPainted() method to show the percent done status.

enter image description here

Braj
  • 46,415
  • 5
  • 60
  • 76