-1

I have a javafx application with a popup window, which contains some checkboxes. I want to set the selected value from true to false, but I can't find a way to do this from inside the controller.

Redefining values works well for the main window from inside the INITIALIZE class, both for checkboxes and buttons, but the redefined values have no effect on the nodes in the popup window.

The solutions is probably simple, still I am having a more than hard time to figure it out.

Any ideas?

@FXML
private CheckBox googleHider;
@FXML
public Button googleButton; 
@FXML
private CheckBox popupCheckbox;


@FXML
public void openDialog(ActionEvent event) throws IOException{

    Stage stage; 
    Parent root;

    if(event.getSource()==dialog){

        //following line should set the value for the checkbox, but popup refuses to open

        popupCheckbox.setSelected(false);

        stage = new Stage();
        root = FXMLLoader.load(getClass().getResource("PopupFXML.fxml"));
        stage.setScene(new Scene(root));
        stage.setTitle("Dialog Window");
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initOwner(dialog.getScene().getWindow());
        stage.showAndWait();     

        }

     @Override
     public void initialize(URL url, ResourceBundle rb) {

    // works fine, button is in the main window
    googleButton.setText("hello");

    // works fine, checkbox is in the main window
    checkbox.setSelected(false);

    //doesn't work, checkbox is in the popup window
    popupCheckbox.setSelected(false);


    } catch (Exception e) {
}
rainer
  • 3,295
  • 5
  • 34
  • 50
  • 1
    Create a method in the controller for `PopupFXML.fxml` that sets the selected state of the popup checkbox. – James_D Dec 28 '16 at 23:05
  • James, thank you. I am using the same controller for both windows (stages) ... – rainer Dec 28 '16 at 23:09
  • No you're not. I don't know why people keep claiming this when it is so clearly not true: you are **not** using the same controller for both views. The `FXMLLoader` creates a new controller each time you call `load()`. – James_D Dec 29 '16 at 02:45
  • James, I sure do only have one controller file, but two fxml files. I know I can ask NetBeans to build the controller for both fxml documents separately, but the code in the controller remains essentially the same. Bear with me, but I still don't follow of how to create a method for only one scene, since both scenes are present in the controller. Could you be more specific or - best of all - place some code? – rainer Dec 29 '16 at 12:44
  • You might only have one controller *class*, but `FXMLLoader.load()` creates a new controller when you call it. So you have at least two controllers here. It sounds like you have made both of them come from the same class, which makes little sense, but you still have two controllers. I would start by creating a different controller class for each FXML file. Then use the techniques in http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – James_D Dec 29 '16 at 14:14
  • James, got it figured out ..... last posted link rocks .... http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml. Will post the snippet in my answer. Thanks for your invaluable help. – rainer Dec 29 '16 at 21:34

1 Answers1

0

Following the recommendations posted in the comments, I decided to make a popup windows based on a second fxml document and second controller (in NetBeans, New -> Empty FXML ... -> Use Java Controller -> Create New ...).

This in order to have more control over the nodes and the layout, including settings obtained from a properties file.

Initial problem was to figure out how to build the stage in the main controller and connect it to the popup controller.

The link Passing Parameters JavaFX FXML gives some real good insights and techniques.

The final code looks like this (I hope it can help someone):

// Anchor Pane from the popup
@FXML
AnchorPane anchorPanePopup;

@FXML
private void eButtonAction(ActionEvent event) throws IOException {     
    Stage newStage = new Stage();
    AnchorPane anchorPanePopup = (AnchorPane) FXMLLoader.load(getClass().getResource("Popup_FXML.fxml"));
    Scene scene = new Scene(anchorPanePopup);
    newStage.setScene(scene);
    newStage.initModality(Modality.APPLICATION_MODAL);
    newStage.setTitle("Dialog Window");
    newStage.showAndWait();        
}
Community
  • 1
  • 1
rainer
  • 3,295
  • 5
  • 34
  • 50