What I'm trying to do I think is exactly what Meg is talking about here: JavaFX2 : Closing a stage (substage) from within itself
When I try to implement JewelSea's answer I get the "nonstatic method getSource() cannot be referenced from a static context."
So I have my secondary window (scene) created in Scene Builder with a simple controller class that has basically one function: tie the button to a close() event handler. Here's the code I have:
public class ProductNotFoundController
implements Initializable {
@FXML // fx:id="closeButton"
private Button closeButton; // Value injected by FXMLLoader
@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
closeButton.setOnAction(new EventHandler<ActionEvent> () {
@Override
public void handle(ActionEvent t) {
// ProductNotFound.avisoClose();
Node source;
source = (Node) ActionEvent.getSource();
Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
});
}
}
Can someone please tell me what I'm doing wrong? And/or where should I put the close() method?