1

I have a situation where I need to update ProgressBar based on some logic being performed in a different method. I am using SwingWorker to create the ProgressBar.

Here is the snippet of code developed so far :

public class ProgressApplet extends JApplet implements ActionListener,PropertyChangeListener{

JProgressBar jp;
JPanel content;
JButton jb;
JTextField jt;
Task task;
JTextArea ja;

class Task extends SwingWorker
{

    @Override
    protected Object doInBackground() throws Exception {

        int progress = 0;
        setProgress(0);
        while (progress < 1000) 
        {
            try 
            {
              Thread.sleep(100);
            } catch (InterruptedException ex) {
                jt.setText(""+ex);
            }
            progress += 10;
            setProgress(progress);
            jt.setText(""+task.getProgress());
          }
          return null;
    }

    @Override
    public void done() {
      //Toolkit.getDefaultToolkit().beep();
      jb.setEnabled(true);
      setCursor(null); 
    }
}

public void init()
{
    content = new JPanel();
    content.setLayout(null);
    jt=new JTextField();
    jb=new JButton("Start");

    jp = new JProgressBar();

    jb.setBounds(30, 80, 100, 30);
    jp.setBounds(30, 140, 120, 30);
    jt.setBounds(30,200,100,30);
    jp.setStringPainted(true);
    //ja.setBounds(30, 250, 100, 200);

    content.add(jb);
    content.add(jp);
    content.add(jt);
    //content.add(ja);
    jb.addActionListener(this);
    jp.setVisible(false);
    this.add(content);
}

@Override
public void actionPerformed(ActionEvent e) 
{


    if(e.getSource() == jb )
    {
        jp.setVisible(true);
        //jt.setText(""+progress);

        jb.setEnabled(false);
        //jp.setVisible(true);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();

    }
    // TODO Auto-generated method stub

}

@Override
public void propertyChange(PropertyChangeEvent evt) 
{

    if ("progress" == evt.getPropertyName()) {
          int progress1 = (Integer) evt.getNewValue();
          jp.setValue(progress1);

         // ja.append(String.format("Completed %d%% of task.\n", task
            //          .getProgress()));
        }

}

public static void main(String[] args)
{
    ProgressApplet pa=new ProgressApplet();
}
} 

Above code contains a normal dummy loop which is currently updating the progressbar. Now I have a method say performwork something like below code which contains the logic based on which I need to update my progreesbar.

public void performwork(ArrayList<String> aa)
{
      //some code
      int i=0;
      while(i<aa.size())
      {
             //some code logic
             //progress bar to be updated based on logic written here
      }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Java Enthusiast
  • 654
  • 7
  • 19
  • You mean something like [this for example](http://stackoverflow.com/questions/12020949/jprogressbar-isnt-progressing/12021971#12021971). Understand what the `SwingWorker` provides you, it provides a means to send information from the worker's thread back to the EDT so you can update the UI safely – MadProgrammer Feb 10 '17 at 08:54

0 Answers0