2016-04-23 49 views
1

我有一個Dialog類與它一wait方法來顯示我的自定義進度對話框延遲顯示在JavaFX的節點運行Android

public static void wait(String title){ 
    isOpen = true; 
    ProgressIndicator progress = new ProgressIndicator(-1); 

    Label label = new Label(title); 
    label.getStyleClass().add("login-label"); 

    HBox container = new HBox(); 
    container.setStyle("-fx-background-color: white;"); 
    container.setAlignment(Pos.CENTER); 
    container.getChildren().addAll(progress,label); 
    if(Main.HEIGHT < 700){ 
     container.setSpacing(10); 
     container.setPadding(new Insets(10,15,10,15)); 
    }else if(Main.HEIGHT < 1200){ 
     container.setSpacing(15); 
     container.setPadding(new Insets(15,20,15,20)); 
    }else{ 
     container.setSpacing(20); 
     container.setPadding(new Insets(20,30,20,30)); 
    } 

    show("", container); 
} 

我有這一段代碼在我的類中的一個到dislay我陸侃對話框

Platform.runLater(new Runnable(){ 
    @Override 
    public void run() { 
     Dialog.wait("Processing, please wait..."); 
    } 
}); 

但遺憾的是其顯示一個延遲,我也試圖把它包裝在一個線程但它沒有工作,我試圖運行它在桌面和它的工作完美,但爲什麼不在我的Android設備

下面是完整的代碼:

download = new Button("Download"); 
download.getStyleClass().add("terminal-button"); 
download.setPrefWidth(Main.HEIGHT > 700 ? 180 : 140); 
download.setOnAction(new EventHandler<ActionEvent>(){ 
    @Override 
    public void handle(ActionEvent event) { 

     Platform.runLater(new Runnable(){ 
      @Override 
      public void run() { 
       Dialog.wait("Processing, please wait..."); 
      } 
     }); 

     Platform.runLater(new Runnable(){ 
      @Override 
      public void run() { 
       //GET THE SELECTED AREAS FOR DOWNLOAD 
       List<String> selectedSectors = new ArrayList(); 
       String sectorid = null; 
       for(Sector r : listView.getItems()){ 
        if(r.isSelected()){ 
         selectedSectors.add(r.getObjid()); 
         sectorid = r.getObjid(); 
        } 
       } 

       if(selectedSectors.size() > 1){ 
        Dialog.hide(); 
        Dialog.showAlert("Multiple downloads are not supported!"); 
        return; 
       } 

       MobileDownloadService mobileSvc = new MobileDownloadService(); 
       //INIT DOWNLOAD 
       Map params = new HashMap(); 
       params.put("assigneeid", SystemPlatformFactory.getPlatform().getSystem().getUserID()); 
       params.put("sectorid", sectorid); 
       batchid = mobileSvc.initForDownload(params); 

       int recordcount = -1; 
       while (true) { 
        int stat = mobileSvc.getBatchStatus(batchid); 
        if (stat < 0) { 
         try { 
          Thread.sleep(2000); 
         }catch(Throwable t){;} 
        } else { 
         recordcount = stat; 
         break; 
        } 
       } 

       if (recordcount <= 0) { 
        Dialog.hide(); 
        Dialog.showError("No data to download"); 
        return; 
       } 

       downloadsize = recordcount; 
       accountList = new ArrayList(); 
       int start=0, limit=50; 
       while (start < recordcount) { 
        params = new HashMap(); 
        params.put("batchid", batchid); 
        params.put("_start", start); 
        params.put("_limit", limit); 
        List<Map> list = mobileSvc.download(params); 
        //if (list != null) accountList.addAll(list); 
        System.out.println("fetch results is " + list.size()); 
        //new Thread(new ProcessDownloadResultTask(start,list)).start(); 
        start += limit;        
       } 

       Dialog.hide(); 

       //SAVE AREA, STUBOUTS 
       clearSector(); 
       for(Sector r : listView.getItems()){ 
        if(r.isSelected()){ 
         saveSector(r); 
        } 
       } 

       label.setVisible(true); 
       progressbar.setVisible(true); 
       progressbar.progressProperty().bind(task.progressProperty()); 
       new Thread(task).start(); 
       download.setText("Cancel"); 
       download.setDisable(false); 
       download.setOnAction(new EventHandler<ActionEvent>(){ 
        @Override 
        public void handle(ActionEvent event) { 
         continueDownload = false; 
         label.setVisible(false); 
         progressbar.setVisible(false); 
         download.setText("Back"); 
         download.setOnAction(new EventHandler<ActionEvent>(){ 
          @Override 
          public void handle(ActionEvent event) { 
           Main.ROOT.setCenter(new Home().getLayout()); 
          } 
         }); 
         root.setOnKeyReleased(new EventHandler<KeyEvent>(){ 
          @Override 
          public void handle(KeyEvent event) { 
           if(event.getCode() == KeyCode.ESCAPE){ 
            if(Dialog.isOpen){ Dialog.hide(); return; } 
            Main.ROOT.setCenter(new Home().getLayout()); 
           } 
          } 
         }); 
         Map params = new HashMap(); 
         params.put("batchid", batchid); 
         params.put("downloadedlist", downloadedList); 
         MobileDownloadService svc = new MobileDownloadService(); 
         svc.cancelDownload(params); 
        } 
       }); 
       download.setDisable(false); 
        } 
     }); 
    } 
}); 

上述情況發生時,你單擊按鈕,輸出應該是:對話會馬上爲你按一下按鈕儘快彈出,但可悲的比方說,按鈕完成整個過程後,對話框會顯示!我試圖把它包裹在線程但沒有運氣!

請幫幫我!任何想法?

+0

你的'Dialog'類從哪裏擴展?從內置的JavaFX對話框?從膠子的對話?或者它是一個免費的實現? –

+0

這是我自己定製的對話框,先生。 –

+0

不錯,你有它的工作,但有時不需要重新發明輪子:)內置的'對話框(JavaFX或膠子的)都可以輕鬆定製。 –

回答

0

我通過在鼠標事件的一個分離的執行,而不是把所有一起在setOnAction解決了這個問題,我把代碼Dialog.wait("Processing, please wait...");setOnMousePressed,就像這樣:

download.setOnMousePressed(new EventHandler<MouseEvent>(){ 
    @Override 
    public void handle(MouseEvent event) { 
     if(!Dialog.isOpen) Dialog.wait("Processing, please wait..."); 
    } 
}); 
download.setOnMouseReleased(new EventHandler<MouseEvent>(){ 
    @Override 
    public void handle(MouseEvent event) { 
     doDownload(); 
    } 
}); 

此代碼工作!

1

這是一個簡短的示例,顯示如何使用Gluon的Dialog來處理後臺任務的進度通知。

它使用虛擬任務,但您可以看到如何處理顯示和隱藏對話框,以及使用ProgressBar來通知進度,甚至取消該任務。

使用膠子插件爲您的IDE,創建一個單一視圖移動項目,並修改這一個觀點:

public class BasicView extends View { 

    public BasicView(String name) { 
     super(name); 

     Dialog dialog = new Dialog("Download Progress"); 

     final ProgressBar progressBar = new ProgressBar(); 
     progressBar.setPrefWidth(200); 

     final Label label = new Label("Process has ended"); 

     VBox vbox = new VBox(10, new Label("Download in progress..."), progressBar, label); 
     vbox.setAlignment(Pos.CENTER); 
     dialog.setContent(vbox); 

     final Button cancel = new Button("Cancel"); 
     dialog.getButtons().add(cancel); 

     dialog.setOnShown(e -> { 
      cancel.setDisable(false); 
      label.setVisible(false); 

      final Task<Void> task = createDownloadTask(); 
      progressBar.progressProperty().bind(task.progressProperty()); 

      cancel.setOnAction(a -> task.cancel(true)); 

      task.setOnCancelled(c -> { 
       PauseTransition pause = new PauseTransition(Duration.seconds(1)); 
       pause.setOnFinished(t -> dialog.hide()); 
       cancel.setDisable(true); 
       label.setVisible(true); 
       pause.play(); 
      }); 

      task.setOnSucceeded(s -> { 
       PauseTransition pause = new PauseTransition(Duration.seconds(1)); 
       pause.setOnFinished(t -> dialog.hide()); 
       cancel.setDisable(true); 
       label.setVisible(true); 
       pause.play(); 
      }); 

      final Thread thread = new Thread(task); 
      thread.setDaemon(true); 
      thread.start(); 
     }); 

     Button button = new Button("Download"); 
     button.setGraphic(new Icon(MaterialDesignIcon.CLOUD_DOWNLOAD)); 
     button.setOnAction(e -> dialog.showAndWait()); 

     setCenter(new StackPane(button)); 
    } 

    @Override 
    protected void updateAppBar(AppBar appBar) { 
     appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> System.out.println("Menu"))); 
     appBar.setTitleText("Downloads View"); 
    } 

    private Task<Void> createDownloadTask() { 
     return new Task<Void>() { 
      @Override 
      protected Void call() throws Exception { 
       for(int i = 0; i <= 10; i++) { 
        if (isCancelled()) { 
         break; 
        } 
        try { 
         Thread.sleep(1000); 
         updateProgress(i, 10); 
        } catch (InterruptedException ie) { 
         if (isCancelled()) { 
          break; 
         } 
        } 
       } 
       return null; 
      } 
     }; 
    } 
} 

Progress Dialog

嘗試用你的替代僞任務,看看它是如何去。