2015-01-31 2 views
-1

안녕하세요. 저는이 예제를 실행하기 위해 밤새도록 노력해 왔으며 행운이 없었습니다. 솔루션을 찾을 수 없습니다. 두 파일이 있습니다.간단한 작업 예제를 실행

먼저 다음은 내가하지만 매우 뭔가를 잘못하고 있어요 알고 IteratingTask.java 파일과 그 내용

//import javafx.concurrent.Task; 
import javafx.application.Application; 
import javafx.concurrent.Task; 
/** 
* 
* @author brett 
*/ 
public class IteratingTask extends Task<Integer> { 
     private final int totalIterations; 

     public IteratingTask(int totalIterations) { 
      this.totalIterations = totalIterations; 
     } 

     @Override protected Integer call() throws Exception { 
      int iterations; 
      // iterations = 0; 
      for (iterations = 0; iterations < totalIterations; iterations++) { 
       if (isCancelled()) { 
        updateMessage("Cancelled"); 
        break; 
       } 
       updateMessage("Iteration " + iterations); 
       updateProgress(iterations, totalIterations); 
      } 
      return iterations; 
     } 
    } 

입니다 여기에 그 내용

import javafx.application.Application; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author brett 
*/ 

public class Worker { 

    /** 
    * @param args the command line arguments 
    * @throws java.lang.Exception 
    */ 


    /** 
    * 
    * @param args 
    * @throws Exception 
    */ 
    public static void main(String[] args) throws Exception { 
     // TODO code application logic here 
     doit(); 
    } 

    private static void doit(){ 

     try { 
      IteratingTask mytask = new IteratingTask(800000); 
      mytask.call(); 
      System.out.println(mytask.getValue()); 
      int pro = (int) mytask.getProgress(); 
     System.out.println(pro); 
     } catch (Exception ex) { 
      Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 
} 

Worker.java입니다 .. 나는 그것을 볼 수 없다. 여기에 어떤 조언이 좋지 않을까 ....이

run: 
Jan 31, 2015 11:56:38 PM Worker doit 
SEVERE: null 
java.lang.IllegalStateException: Toolkit not initialized 
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:270) 
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:265) 
    at javafx.application.Platform.runLater(Platform.java:81) 
    at javafx.concurrent.Task.runLater(Task.java:1211) 
    at javafx.concurrent.Task.updateMessage(Task.java:1129) 
    at IteratingTask.call(IteratingTask.java:24) 
    at Worker.doit(Worker.java:38) 
    at Worker.main(Worker.java:31) 

BUILD SUCCESSFUL (total time: 0 seconds) 

그것은 확인 빌드를 얻을 오류입니다.

+0

을 그리고 ..what이 스윙 * 또는 관계 되는가 :

또한, 다음과 같이,이 실행하는 동안, Task의는 일반적으로 백그라운드 스레드에서 실행하도록 구성되어 있습니다 IDE? 코드가 Java FX 기반 (** Swing에 대한 ** 툴킷 **) 인 것 같아서 IDE가 자동으로 문제를 해결하려고하거나 (현재 코드가 실패했음을) 의심 스럽습니다. –

+0

미안하지만, 나는 배우려고 노력하고 있고, 나의 이해는 여전히 매우 원시적이다. – Brett

답변

1

문제는 FX Toolkit, 특히 FX 응용 프로그램 스레드가 시작되지 않았기 때문입니다. update...(...) 메서드는 Task에서 FX 응용 프로그램 스레드의 다양한 상태를 업데이트하므로 그러한 메서드를 호출하면 이러한 스레드가 실행되지 않으므로 IllegalStateException이됩니다.

이 코드를 실제 FX 응용 프로그램에 포함하면 정상적으로 실행됩니다. launch()을 호출하면 FX 툴킷이 시작됩니다. * 당신을

import javafx.application.Application; 
import javafx.scene.Scene ; 
import javafx.scene.layout.StackPane ; 
import javafx.scene.control.Label ; 
import javafx.stage.Stage ; 

import java.util.logging.Level; 
import java.util.logging.Logger; 


public class Worker extends Application { 


    @Override 
    public void start(Stage primaryStage) throws Exception { 
     StackPane root = new StackPane(new Label("Hello World")); 
     Scene scene = new Scene(root, 350, 75); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
     doit(); 
    } 

    public static void main(String[] args) throws Exception { 
     launch(args); 
    } 

    private void doit(){ 

     try { 
      IteratingTask mytask = new IteratingTask(800000); 
      // mytask.call(); 
      Thread backgroundThread = new Thread(mytask); 
      backgroundThread.start(); // will return immediately, task runs in background 
      System.out.println(mytask.getValue()); 
      int pro = (int) mytask.getProgress(); 
     System.out.println(pro); 
     } catch (Exception ex) { 
      Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 
} 
관련 문제