2016-11-17 118 views
1

我一直在努力這個好幾天,我已經讀過關於線程,MVC,綁定,接口和許多有趣的東西,但我不能把它們放在一起以適當的方式做這個工作。Javafx在哪裏綁定標籤到StringProperty

我只想列出英里c中所有的文件:\和更換標籤 上顯示他們,但我得到的是:

Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4 

這是我FXML:

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.RowConstraints?> 


<GridPane alignment="center" hgap="10" prefHeight="200.0" prefWidth="401.0" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="sample.Controller"> 
    <columnConstraints> 
     <ColumnConstraints /> 
    </columnConstraints> 
    <rowConstraints> 
     <RowConstraints /> 
    </rowConstraints> 
    <children> 
     <AnchorPane prefHeight="200.0" prefWidth="368.0"> 
     <children> 
      <Button fx:id="start" layoutX="159.0" layoutY="35.0" mnemonicParsing="false" onAction="#displayFiles" text="Start" /> 
      <Label fx:id="fileLabel" layoutX="20.0" layoutY="100.0" prefHeight="21.0" prefWidth="329.0" text="This label must change on iteration" /> 
     </children> 
     </AnchorPane> 
    </children> 
</GridPane> 

我公司主營:

import javafx.application.Application; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
     primaryStage.setTitle("Dummy App"); 
     primaryStage.setScene(new Scene(root)); 
     primaryStage.setResizable(false); 


     primaryStage.show(); 
    } 


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

我的C ontroller:

import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 

public class Controller { 
    @FXML 
    Button start; 

    @FXML 
    Label fileLabel; 

    @FXML 
    void displayFiles(ActionEvent event) throws Exception{ 
     Model model = new Model(); 

     //BINDING 
     fileLabel.textProperty().bind(model.status); 

     Thread thread = new Thread(model); 

     thread.start(); 
    } 

} 

和模型:

import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 

import java.io.File; 

/** 
* Created by R00715649 on 16-Nov-16. 
*/ 
public class Model implements Runnable { 
    File rootDirectory = new File("C:/"); 
    StringProperty status = new SimpleStringProperty("Starting scan..."); 

    @Override 
    public void run() { 
     try{ 

      File[] fileList = rootDirectory.listFiles(); 
      for (File f:fileList){ 
       processDirectory(f); 
      } 

     }catch (Exception e){ 

     } 
    } 

    void processDirectory (File directory){ 
     if (directory.isDirectory()){ 
      File[] fileList = directory.listFiles(); 
      for (File f:fileList){ 
       processDirectory(f); 
      } 

     }else{ 
      System.out.println(directory.getAbsolutePath()); 
      status.set(directory.getAbsolutePath()); 
     } 

    } 
} 

回答

2

由於標籤的文本被綁定到模型的狀態,改變在UI(標籤的文本會改變)改變模型的狀態結果。因此,您只能在FX應用程序線程上更改模型的狀態。

您可以使用Platform.runLater(...)計劃代碼在FX應用程序線程上運行。您可以在模型中直接做到這一點:

void processDirectory (File directory){ 
    if (directory.isDirectory()){ 
     File[] fileList = directory.listFiles(); 
     for (File f:fileList){ 
      processDirectory(f); 
     } 

    }else{ 
     System.out.println(directory.getAbsolutePath()); 
     Platform.runLater(() -> status.set(directory.getAbsolutePath())); 
    } 

} 

,或者你可以註冊與模型的狀態(而不是綁定)的監聽器,並委託給FX應用程序線程有:

@FXML 
void displayFiles(ActionEvent event) throws Exception{ 
    Model model = new Model(); 

    ChangeListener<String> listener = (obs, oldStatus, newStatus) -> fileLabel.setText(newStatus); 
    model.status.addListener(listener); 

    Thread thread = new Thread(model); 

    thread.start(); 
} 

在後一種解決方案中,當線程完成時(這需要一些額外的工作),您可能需要刪除該偵聽器,否則在標籤仍然顯示時模型不能被垃圾收集。爲此,您可以考慮使用Task

@FXML 
void displayFiles(ActionEvent event) throws Exception{ 
    Model model = new Model(); 

    Task<Void> task = new Task<Void>() { 
     @Override 
     public Void call() { 
      model.status.addListener((obs, oldStatus, newStatus) -> updateMessage(newStatus)); 
      model.run(); 
      return null ; 
     } 
    }; 

    fileLabel.textProperty().bind(task.messageProperty()); 

    task.setOnSucceeded(e -> fileLabel.textProperty().unbind()); 

    new Thread(task).start(); 
} 
1
status.set(directory.getAbsolutePath()); 

應該是在主線程。它應該是這樣的:

Platform.runLater(() -> status.set(directory.getAbsolutePath()));