2015-08-27 96 views
0

我決定使用JavaFXML更新我的應用程序,但是我無法將場景傳遞到控制器。這是我的控制器;JavaFX傳遞舞臺/切換場景

public class MainApp extends Application { 

@FXML 
public Stage primaryStage; 

@FXML 
private AnchorPane rootLayout; 
@FXML 
private JobInterface jInterface; 

@Override 
public void start(Stage primaryStage) { 
    primaryStage = new Stage(); 
    setPrimaryStage(primaryStage); 
    initRootLayout(); 
} 

@FXML 
public void initRootLayout(){ 
    try { 
     primaryStage = getPrimaryStage(); 
     FXMLLoader loader = new FXMLLoader(); 
     loader.setLocation(MainApp.class.getResource("MainInterface.fxml")); 
     rootLayout = (AnchorPane) loader.load();   
     Scene scene = new Scene(rootLayout);  
     primaryStage.setScene(scene); 
     primaryStage.show(); 
     setPrimaryStage(primaryStage); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

@FXML 
private void setJobLayout(){ 
    primaryStage = getPrimaryStage(); 
    jInterface = new JobInterface(); 
    jInterface.initJobLayout(); 
    primaryStage.setScene(jInterface.getScene()); 
} 

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

public Stage getPrimaryStage() { 
    return primaryStage; 
} 

public void setPrimaryStage(Stage primaryStage) { 
    this.primaryStage = primaryStage; 
} 
} 

這是一種使用不同的FXML文件改變場景並嘗試將場景傳遞迴控制器的方法;

public class JobInterface { 

private AnchorPane rootLayout; 
private Scene scene; 

public void initJobLayout(){ 
    try { 
     FXMLLoader loader = new FXMLLoader(); 
     loader.setLocation(MainApp.class.getResource("JobInterface.fxml")); 
     rootLayout = (AnchorPane) loader.load(); 
     scene = new Scene(rootLayout); 
     setScene(scene); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public Scene getScene() { 
    return scene; 
} 

public void setScene(Scene scene) { 
    this.scene = scene; 
} 
} 

現在我遇到的問題是在主應用程序的這一行上的NullPointerException;

primaryStage.setScene(jInterface.getScene()); 

我試圖通過方法之間的階段,這樣我可以只更新場景,而不必打開一個新的階段,每次的新方法被調用。關於我要去哪裏的任何想法都是錯誤的?

回答

1

應該沒有必要通過舞臺或場景。您的Main將加載fxml資源,該資源將讓您的fxml控制器負責您的fxml文件。

@Override 
    public void start(Stage stage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("jobController.fxml"));   
    Scene scene = new Scene(root);   
    stage.setScene(scene); 
    stage.show(); 
} 

你的控制器可能看起來像這樣(取決於你FXML設計):

public class JobController { 

    @FXML 
    private Label label; 

    @FXML 
    private void handleButtonAction(ActionEvent event) {   
     label.setText("This is the controller speaking"); 
    } 
} 

現在你可以「控制」從控制器的階段(場景)。如果要創建另一個也需要更新場景的課程,請將控制器的引用傳遞給它,例如從控制器:

TimeClock timeClock = new TimeClock(); 
timeClock.init(this); 

,然後在TimeClock.java您有:

private final JobController controller; 

public void init (JobController control){ 
    this.controller = control; 
} 

現在你可以在你的控制器從時鐘進度類訪問任何公共方法。例如。

controller.updateLabel("Time clock speaking!"); 
+0

我對這個@Frank有點困惑。程序中是否只有一個fxml文件? (在JobInterface類中)如何加載另一個具有完全不同UI的fxml文件? – jbanks

+0

也許我在這裏誤解了你。看看這個由jewelsea回答的StackOverflow問題:https://gist.github.com/jewelsea/6460130 – Frank