3

I am trying to upload a few files over the network. while uploading it will take an indeterminate time. so wish to display an indeterminate jprogressbar during the upload. The problem, however is that my jprogressbar and upload does not work simultaneously. I tried the upload in a separate thread while keeping the jprogressbar in the EDT. I tried a few different ways. some of them are: 1) implemented Runnable and in run() I uploaded the file. The progressbar was in the EDT all this time. (did not work.) 2) had two separate threads and put both upload and progressbar handling in each. (not working). the code for this is :

Thread oThread = new Thread(new Runnable() {

   @Override
   public void run() 
   {
        progressBar.setIndeterminate(true);
        progressBar.setVisible(true);
        progressBar.validate();

   }
});

Thread oThread1 = new Thread(new Runnable() 
{

    @Override
    public void run() {
        logger.info("Upload result from ***: "+ newport.upload(textbyte, wavbyte,xmlbyte, filename));

        }
    });

3) then I rewrote the entire thing and tried a different approach using Executor like this:

executor.execute(new Runnable() {
    @Override
    public void run() {
            upload actions

            SwingUtilities.invokeLater(new Runnable() {
                 progBar.setVisible(false);
            });
    }});

but none of these techniques worked. I am new to this and am wondering if all this has to be done to have a jprogressbar (indeterminate) displayed. I would like to know if there is a simpler and easier way to do this.

antony.ouseph.k
  • 907
  • 2
  • 15
  • 28
  • Did you try the examples from the Swing tutorial on `How to Use Progress Bars`? – camickr Dec 18 '13 at 06:22
  • @camickr almost all of them. now when I search for jprogressbar issues all the websites returned by google are shown as visited links. – antony.ouseph.k Dec 18 '13 at 06:27
  • That doesn't answer my question. The Swing tutorial has a working example. Why do you think any example we post here will be any different than the example found in the tutorial? You haven't asked a specific question about the code that you find confusing in the tutorial. – camickr Dec 18 '13 at 06:38

2 Answers2

1

you must use setValue() for change progressbar below code example for work with jporgressbar

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class Tets  {

    public static void main(String [] args) {
       JFrame frame =new JFrame();
       JPanel panel=new JPanel();

       JProgressBar bar=new JProgressBar();
       bar.setMaximum(100);
       bar.setMinimum(0);
       panel.add(bar);


       frame.getContentPane().add(panel);
       frame.pack();
       frame.setVisible(true);

       for(int i=0;i<100;i++){
           bar.setValue(i);
           try {
               Thread.sleep(100);
           } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }

       }
   }

}

good luck

1

You aren't actually setting the value of the progress bar at any point in the code you have posted.

Additionally this looks like a good case for using SwingWorker, it will perform the work on a different thread and then call you back with progress. The good thing being those callbacks happen already on the Swing thread.

This SwingWorker example even includes setting a progress bar:

http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html

Tim B
  • 40,716
  • 16
  • 83
  • 128