Results 1 to 5 of 5
  1. #1
    encryptor is offline Member
    Join Date
    Apr 2017
    Posts
    3
    Rep Power
    0

    Default How to return a value from Modal window back to Parent

    I have a Scene set up that when you press a button a modal window pops up using primaryStage.showandwait();
    After I choose a selection from a ListView I want to return that selection back to the window that opened the modal window. Is this possible?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: How to return a value from Modal window back to Parent

    The value could be passed by storing it in a variable that is in scope for both methods that want access to it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    encryptor is offline Member
    Join Date
    Apr 2017
    Posts
    3
    Rep Power
    0

    Default Re: How to return a value from Modal window back to Parent

    I dont really understand how that would work in my case.

    Java FX Code:
    //(BuilderController)
        Stage primaryStage = new Stage();
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/resources/fxml/DealerInventory.fxml"));
                loader.setController(new DealerInventoryController(modelCode));
                Parent root = loader.load();
    
                Scene scene = new Scene(root);
    
                primaryStage.setScene(scene);
                primaryStage.initModality(Modality.APPLICATION_MODAL);
                primaryStage.initStyle(StageStyle.UTILITY);
                primaryStage.setResizable(false);
                primaryStage.showAndWait();       //THIS OPENS MODAL WINDOW
    In my modal window that opens from the BuilderController I click a button and I want to pull back a variable from that modal window.
    Java FX Code:
    @FXML
        void LoadQuote_Clicked(MouseEvent event) {
    
            try {
                //CONVERTS SELECTED ITEM FIELDS TO DEALERINVENTORYLISTVIEW OBJECT
                DealerInventoryListView list = (DealerInventoryListView) tblDealerInventory.getSelectionModel().getSelectedItem();
                String returnValue = list.getOrderNumber();    // I want to return this value to BuilderController
                System.out.println(list.getOrderNumber());
            } catch (Exception ex) {
                System.out.println("Error: " + ex);
            }
    
        }
    How would I access a variable from the BuilderController if I have this variable in the DealerInventoryController?

    I could cheat and put it in a Preferences object and retrieve it from the BuilderController but that doesnt sound right to me.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: How to return a value from Modal window back to Parent

    I dont really understand how that would work
    Hard to say if it would work without seeing more of the code. Bits and pieces don't give enough context.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    388
    Rep Power
    4

    Default Re: How to return a value from Modal window back to Parent

    You have to inject a dependency of the receiving class into the sending class and then you can send that value back. There are a couple of ways to do that (Spring!!), let me give you a start via the "pedestrian way"
    I am assuming that the receiving class is BuilderController.
    I am assuming that the sending class is the controller of that fxml popup window. Right now I don't see a reference to it (should be fixable easily), so I call it dealerInventoryController. It is a singleton. It carries the ActionHandler for that Button.
    In the BuilderController I would add
    Java FX Code:
    String returnValue;
    
    //depending on your setup the class might have to implement the Initializable Interface. 
    //If it is a FXML controller class the annotation @FXML should be good enough
    private void initialize(){
    DealerInventoryController.injectBuilderController(this);
    }
    
    public setReturnValue(String returnValue){
    this.returnValue=returnValue;
    }
    In the DealerInventoryController I would add
    Java FX Code:
    private static BuilderController builderController;
    
    public static void injectBuilderController(BuilderController buildContr){
    builderController = buildContr;
    }
    
    // to be added in LoadQuote_Clicked method
    builderController.setReturnValue(returnValue);
    Depending on your setup there might be some timing issues involved, obviously you cannot inject a Dependency into something that does not exist, but you get the idea.
    Last edited by benji2505; 06-02-2017 at 04:11 AM.

Similar Threads

  1. Replies: 1
    Last Post: 07-25-2011, 10:24 AM
  2. Replies: 9
    Last Post: 06-16-2010, 03:42 AM
  3. Replies: 4
    Last Post: 01-10-2010, 07:48 AM
  4. posting results back to parent page
    By carag in forum New To Java
    Replies: 0
    Last Post: 07-29-2009, 12:29 PM
  5. change url in parent window from child window
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-09-2009, 12:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •