2017-12-29 8 views
-1

나는 (읽기, 쓰기 파일 및 기타 작업도) 거의 3 분 동안 작업을 수행하는 방법이 있습니다.메서드가있는 바인딩 진행률 표시 줄, javafx

메서드의 진행으로 실행할 수있는 javafx에 진행률 표시 줄을 바인딩하고 싶습니다.

이것은 이러한 단계를 수행해야합니다 ProgressBar의 내 방법 바인드를 들어

System.out.println("Going to load contract/security:"+new Date()); 
    Map<Integer,FeedRefData> map = loadContractAndSecurityFromFile(loadFO); 
    addIndicesRefData(map); 
    BufferedWriter writer = createFile(); 
    for (FeedRefData feedRefData : map.values()) { 
     try { 
      updateInstrumentAlias(map, feedRefData); 
      String refDataString = feedRefData.toString(); 
      writer.write(refDataString, 0, refDataString.length()); 
      writer.newLine(); 
      writer.flush(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      log.info("Unable to write Quote Object to : "); 
     } 
    } 
    System.out.println("Ref Data File Generated:"+new Date()); 
+0

방법의 전체 코드입니까? –

+0

예 완료되었습니다. –

답변

0

당신의 방법이다 :

  1. 당신의 방법을 포함하는 작업을 만듭니다.
  2. 이 작업을 실행하는 스레드를 만듭니다.
  3. 진행률 속성을 작업 속성에 바인딩하십시오.

    public class Bind extends Application { 
    
        public static void main(String[] args) { 
         launch(args); 
        } 
    
        @Override 
        public void start(Stage primaryStage) { 
    
         ProgressBar pb = new ProgressBar(); 
         pb.setProgress(1.0); 
    
         Button button = new Button("start"); 
         button.setOnAction((ActionEvent event) -> { 
          /*Create a task which contain method code*/ 
          Task<Void> task = new Task<Void>() { 
           @Override 
           protected Void call() throws Exception { 
    
            File file = new File("C:\\Users\\Electron\\Desktop\\387303_254196324635587_907962025_n.jpg"); 
            ByteArrayOutputStream bos = null; 
            try { 
             FileInputStream fis = new FileInputStream(file); 
             byte[] buffer = new byte[1024]; 
             bos = new ByteArrayOutputStream(); 
             for (int len; (len = fis.read(buffer)) != -1;) { 
              bos.write(buffer, 0, len); 
    
              updateProgress(len, file.length()); 
              /* I sleeped operation because reading operation is quiqly*/ 
              Thread.sleep(1000); 
             } 
             System.out.println("Reading is finished"); 
            } catch (FileNotFoundException e) { 
             System.err.println(e.getMessage()); 
            } catch (IOException e2) { 
             System.err.println(e2.getMessage()); 
            } 
            return null; 
           } 
          }; 
    
           Thread thread = new Thread(task); 
           thread.start(); 
          /*bind the progress with task*/ 
           pb.progressProperty() 
            .bind(task.progressProperty()); 
    
         }); 
         HBox box = new HBox(pb, button); 
         Stage stage = new Stage(); 
         StackPane root = new StackPane(); 
         root.getChildren().add(box); 
         Scene scene = new Scene(root); 
         stage.setScene(scene); 
         stage.show(); 
    
        } 
    
    } 
    

작업 시작 :

enter image description here

작업이 완료 :

enter image description here

난 그냥 당신의 방법 코드로 코드를 변경,이 간단한 예제를 만들어 추신 : 나 내 파일이 너무 작아서 Thread.sleep(1000)을 사용했습니다. 진행 시간이 길면 제거 할 수 있습니다.

+0

고마워, 그거야 :) –