不确定进度栏不会显示

时间:2013-06-29 15:58:35

标签: java multithreading swing jprogressbar

我有一个将文件(通过ADB)复制到Android平板电脑的应用程序。这需要一些时间,所以我想显示一个弹出窗口,上面有一个不确定的进度条。当复制任务完成后,我希望能够停止进度条并让用户关闭对话框。

目前我还没有添加额外的对话框,我只是想让进度条工作。我遇到的问题是进度条没有在任务开始时显示,但我不知道为什么。进度条显示何时显示同步完成的对话框。代码是:

        progress = new JProgressBar(0, 100);
        progress.setForeground(new Color(255, 99, 71));
        progress.setIndeterminate(true);
        progress.setValue(0);
        progress.setPreferredSize( new Dimension( 300, 20 ) );
        progress.setBounds( 278, 12, 260, 20 );
        progress.setVisible(false);
        progress.setString("Sync in progress");
        progress.setStringPainted(true);
        contentPane.add(progress);
        pushtotab = new JButton("");
        pushtotab.addActionListener(new ActionListener() {


 public void actionPerformed(ActionEvent arg0) {
                        if (buildpathset==1){
                            try{
                            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                            progress.setVisible(true);
                            wiredsync();
                        }finally{
                            JOptionPane.showMessageDialog(null, "sync complete. ",null, buildpathset);
                             setCursor(Cursor.getDefaultCursor());      
                             progress.setVisible(false);
                        }}else{ 
    //warning in here later - TO Do
                }
                }
                });

public void wiredsync(){

        try {

                    Process process = Runtime.getRuntime().exec("adb" + " push "+ buildpath + " " + adbtabletsync);
                    InputStreamReader reader = new InputStreamReader(process.getInputStream());
                    Scanner scanner = new Scanner(reader);
                    scanner.close();
                    int exitCode = process.waitFor();
                    System.out.println("Process returned: " + exitCode);

                } catch(IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }//end 

感谢您的帮助,

安迪

2 个答案:

答案 0 :(得分:2)

我认为你的问题是你不使用线程。我的意思是在你将进度条的可见性变为true之后,你应该在一个线程中定义你的长任务。我不熟悉Swing但是 看看那里的Swing(抱歉,如果它没有用完): http://www.java-tips.org/java-se-tips/javax.swing/how-to-handle-long-running-tasks-in-a-swing-applic.html

并为android:http://www.mkyong.com/android/android-progress-bar-example/

答案 1 :(得分:2)

pooyan有正确的想法 - 在后台线程中执行长时间运行的过程 - 但是提供了错误的库示例,因为您的程序是Swing程序而不是Android程序。 Swing的典型答案是在SwingWorker的doInBackground()方法中执行长时间运行的任务。

请在我找到更好的例子时抓住...

像这样:

if (buildpathset == 1) {
   setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
   progress.setVisible(true);

   // create my SwingWorker object
   final SwingWorker<Void, Void> myWorker = new SwingWorker<Void, Void>() {
      protected Void doInBackground() throws Exception {
         // here is my long running task, calling in background
         // thread
         wiredsync();
         return null;
      };
   };

   // this allows me to be notified when the SwingWorker has
   // finished
   myWorker.addPropertyChangeListener(new PropertyChangeListener() {

      @Override
      public void propertyChange(PropertyChangeEvent pcEvt) {
         // if the SwingWorker is done
         if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
            // notify the user
            JOptionPane.showMessageDialog(null, "sync complete. ",
                  null, buildpathset);
            setCursor(Cursor.getDefaultCursor());
            progress.setVisible(false);

            try {
               // one way to catch any errors that occur in
               // SwingWorker
               myWorker.get();
            } catch (InterruptedException | ExecutionException e) {
               e.printStackTrace();
            }

         }
      }
   });
   // run my SwingWorker
   myWorker.execute();
} else {
   // warning in here later - TO Do
}

有关详情,请查看:Lesson: Concurrency in Swing