2016-12-05 4 views
0

私はそのクラスにFXMLファイルを持つRestaurantというクラスを持っています。私はボタンを押すとテーブルという別のウィンドウを開き、FXMLファイルも持っています。テーブルウィンドウ内の最小化ボタン。別のクラスにボタンを追加するには

テーブルの最小化ボタンを押すと、新しいボタンがレストランウィンドウに追加されます。

しかし、null例外が発生しています。 誰かが私を助けてこれを解決できますか?

これは私の最小化ボタンのコードです:

@FXML 
public void minimiza(ActionEvent event) { 
    Button Tables = new Button("Mesas"); 
    try { 
     Stage mesa = (Stage) ((Button) event.getSource()).getScene().getWindow(); 
     mesa.setIconified(true); 
     FXMLLoader loader = new FXMLLoader(); 
     RestauranteController controle = loader.getController(); 
     controle.adicionaBotao(Tables); 
    } catch (Exception e) { 
     System.out.println("Not working " + e.getMessage()); 
    } 
} 

答えて

0

それはちょうどRestaurantクラスからStageiconifiedプロパティを聞くことがおそらく良いでしょう。 状態が最小化されたiconified状態と一致しない場合は、tablesウィンドウのコントローラでこのプロパティを作成することができます。

private int windowCount; 

@Override 
public void start(Stage primaryStage) { 

    Button btn = new Button("Show new Window"); 
    VBox buttonContainer = new VBox(btn); 

    btn.setOnAction((ActionEvent event) -> { 
     // create new window 
     windowCount++; 
     Label label = new Label("Window No " + windowCount); 
     Stage stage = new Stage(); 
     Scene scene = new Scene(label); 
     stage.setScene(scene); 

     // button for restoring from main scene 
     Button restoreButton = new Button("Restore Window " + windowCount); 

     // restore window on button click 
     restoreButton.setOnAction(evt -> { 
      stage.setIconified(false); 
     }); 

     // we rely on the minimize button of the stage here 

     // listen to the iconified state to add/remove button form main window 
     stage.iconifiedProperty().addListener((observable, oldValue, newValue) -> { 
      if (newValue) { 
       buttonContainer.getChildren().add(restoreButton); 
      } else { 
       buttonContainer.getChildren().remove(restoreButton); 
      } 
     }); 
     stage.show(); 
    }); 


    Scene scene = new Scene(buttonContainer, 200, 200); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

ところで:FXMLをロードせずに、FXMLLoaderは、コントローラのインスタンスを作成することはありませんFXMLが指定されることなく、ましてやことに注意してください。さらに、あなたのRestaurantウィンドウで使用されていたのと同じコントローラインスタンスを返しません。

+0

それはうまくいった!ありがとうFabian –

関連する問題