2016-05-07 94 views
0

在我的javaFx应用程序中,我试图将我的进度条附加到应该从另一个类执行某些方法的任务,我似乎无法获得通过这些方法运行的任务,点击此任务的按钮。任务/进度条JavaFX应用程序

这是我通过用户输入两个词的搜索页面控制器

FXML Controller class 

public class WordComparePageController implements Initializable { 
@FXML 
private TextField wordOneText; 
@FXML 
private TextField wordTwoText; 
@FXML 
private Button pairSearchButton; 
@FXML 
private TextField wordPairText; 

WordNetMeasures wordNetMeasures = new WordNetMeasures(); 

private double distance; 
private double linDistance; 
private double leskDistance; 

DecimalFormat df = new DecimalFormat("#.0000"); 
DecimalFormat pf = new DecimalFormat("#.0"); 
@FXML 
private ProgressBar progressBar; 
@FXML 
private ProgressIndicator progressIndicator; 

@Override 
public void initialize(URL url, ResourceBundle rb) {} 

绑定进度条过任务

@FXML 
private void onSearchButtonClicked(ActionEvent event) throws InstantiationException, IllegalAccessException { 

    progressBar.progressProperty().bind(taskPS.progressProperty()); 
     progressIndicator.progressProperty().bind(taskPS.progressProperty()); 

    Thread th = new Thread(taskPS); 
    th.setDaemon(true); 
    th.start(); 
      } 

    Task<Void> taskPS = new Task<Void>() { 

    @Override 
    protected Void call() throws InstantiationException, IllegalAccessException { 
     updateProgress(0, 1); 

     distance = wordNetMeasures.searchForWord(wordOneText.getText(), wordTwoText.getText()); 
     linDistance = wordNetMeasures.linMethod(wordOneText.getText(), wordTwoText.getText()); 
     leskDistance = wordNetMeasures.leskMethod(wordOneText.getText(), wordTwoText.getText()); 
     updateProgress(1, 40); 
     ProjectProperties.getInstance().setPathResult(distance); 

     System.out.println("Distance: = " + ProjectProperties.getInstance().getPathResult()); 

    ProjectProperties.getInstance().setWordText(wordOneText.getText() + "," + wordTwoText.getText()); 

     String wordNetDistance = String.valueOf(df.format(distance)); 
     ProjectProperties.getInstance().setPathWordNetText(wordNetDistance); 
     ProjectProperties.getInstance().setLinWordNetText((String.valueOf(df.format(linDistance)))); 
     ProjectProperties.getInstance().setLinResult(linDistance); 
     ProjectProperties.getInstance().setPathResult(distance); 
     ProjectProperties.getInstance().setLeskResult(leskDistance); 
      ProjectProperties.getInstance().setLeskWordNetText((String.valueOf(df.forma t(leskDistance)))); 

     updateProgress(40, 70); 



     Database databaseConnection = new Database(); 
     try { 
      databaseConnection.getConnection(); 
      databaseConnection.addWordNetToDatabase(ProjectProperties.getInstance().getWordText(), distance, linDistance, leskDistance); 
      updateProgress(100, 100); 
     } catch (SQLException ex) { 
      Logger.getLogger(WordComparePageController.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     return null; 
    } 
}; 

}

类与WordNet的测量方法任务

public class WordNetMeasures { 


private static ILexicalDatabase db = new NictWordNet(); 
private static RelatednessCalculator[] rcs = { 
    new HirstStOnge(db), new LeacockChodorow(db), new Lesk(db), new WuPalmer(db), 
    new Resnik(db), new JiangConrath(db), new Lin(db), new Path(db) 
}; 

private static RelatednessCalculator pathMethod = new Path(db); 
private static RelatednessCalculator linMethod = new Lin(db); 
private static RelatednessCalculator leskMethod = new Resnik(db); 
private static double distance; 
private static double linDistance; 
private static double leskDistance; 



public static double searchForWord(String word1, String word2) { 
    WS4JConfiguration.getInstance().setMFS(true); 
    RelatednessCalculator rc = pathMethod; 
    distance = rc.calcRelatednessOfWords(word1, word2); 

    return distance; 
} 


public static double linMethod(String word1, String word2) { 
    WS4JConfiguration.getInstance().setMFS(true); 
    RelatednessCalculator rc = linMethod; 
    linDistance = rc.calcRelatednessOfWords(word1, word2); 

    return linDistance; 
} 


public static double leskMethod(String word1, String word2) { 
    WS4JConfiguration.getInstance().setMFS(true); 
    RelatednessCalculator rc = leskMethod; 
    leskDistance = rc.calcRelatednessOfWords(word1, word2); 

    return leskDistance; 
} 


/** 
* Gets the ontology path for the word passed in 
* @param word 
* @return 
* @throws JWNLException 
*/ 
public String[] getWordNetPath(String word) throws JWNLException { 

    String[] wordResults = new String[500]; 
    RiWordnet wordnet = new RiWordnet(); 
    String[] posOfWord = wordnet.getPos(word); 
    int[] wordIds = wordnet.getSenseIds(word, posOfWord[0]); 
    wordResults = wordnet.getHypernymTree(wordIds[0]); 

    return wordResults; 
} 

/** 
* Gets the set of synsets for the word passed in 
* @param word 
* @return 
*/ 
public String[] getWordNetSynset(String word) { 
    RiWordnet wordnet = new RiWordnet(); 
    String[] posOfWord = wordnet.getPos(word); 
    int[] wordIds = wordnet.getSenseIds(word, posOfWord[0]); 
    String[] wordResults = wordnet.getSynset(wordIds[0]); 

    return wordResults; 
} 

}

+0

'taskPS'is没有初始化,当你调用它的方法。其实你应该得到一个NPE。 – jns

+0

请你详细说明一下吗? –

+0

我的错误,我忽略了taskPS之前的大括号。我已经编辑了我的答案... – jns

回答

1

A Task仅用于一次。要重用它,你必须重新实例化它,或者创建一个扩展Service的类。

Service负责创建和管理Task,并且不必重新绑定progressProperty的绑定。

ProgressBar已被添加到一个Scene可见

public class TestApp extends Application { 

    private Stage progressStage; 

    @Override 
    public void start(Stage primaryStage) throws IOException { 

     Button btn = new Button("start task"); 

     TaskService service = new TaskService(); 
     service.setOnScheduled(e -> progressStage.show()); 
     service.setOnSucceeded(e -> progressStage.hide()); 

     ProgressBar progressBar = new ProgressBar(); 
     progressBar.progressProperty().bind(service.progressProperty()); 

     progressStage = new Stage(); 
     progressStage.setScene(new Scene(new StackPane(progressBar), 300, 300)); 
     progressStage.setAlwaysOnTop(true); 

     btn.setOnAction(e -> service.restart()); 

     primaryStage.setScene(new Scene(new StackPane(btn), 300, 300)); 
     primaryStage.show(); 
    } 

    private class TaskService extends Service<Void> { 

     @Override 
     protected Task<Void> createTask() { 
      Task<Void> task = new Task<Void>() { 

       @Override 
       protected Void call() throws Exception { 

        for (int p = 0; p < 100; p++) { 
         Thread.sleep(40); 
         updateProgress(p, 100); 
        } 
        return null; 
       } 
      }; 
      return task; 
     } 
    } 

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