Frage

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.

War es hilfreich?

Lösung

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

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top