2016-12-05 1 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(); 
} 

BTW하십시오 fxml를로드하지 않고, FXMLLoader는 컨트롤러 인스턴스를 생성하지 않습니다 fxml을 지정하지 않고 혼자 할 수 있습니다. 또한 어떤 경우에도 Restaurant 창에 사용 된 컨트롤러 인스턴스와 동일한 컨트롤러 인스턴스를 반환하지 않습니다.

+0

잘 작동했습니다. 고마워요 Fabian –

관련 문제