JavaFX 2.2 Modal Dialog Box * c * FXML

I am trying to use the example provided by @jewelsea in this context , and I am stuck because I use FXML.

I saw both of these posts: How to create a modal window in JavaFX 2.1 and this answer to JavaFX 2 modal window .

Where am I stuck in jewelsea code that says:

final WebView webView = new WebView(); webView.getEngine().load("http://docs.oracle.com/javafx/"); primaryStage.setScene(new Scene(webView)); 

Whereas, since I use FXML, I do this:

 FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MyFXML.fxml")); Scene scene = (Scene)fxmlLoader.load(); myController = fxmlLoader.getController(); primaryStage.setScene(scene); 

Can you tell me how to change your code (4 lines above), so it works with jewelsea example?

Thanks!

+4
source share
1 answer

The replacement code that you use with FXMLLoader , and not with the WebView created sample scene sample, is beautiful, you do not need to change it.

Your code will display the main scene for the main stage based on the fxml document (for my example, I used WebView as the main scene that you don't need, so you don't need any of the WebView related code from the bottom).

At the time you want to display the dialog, you need a trigger in your controller. As a simple example, you can configure fxml for your main scene, which includes only a button, and then provide an ActionHandler for the button in your controller (as in Introduction to the FXML Document ).

Now, instead of just doing println when the button is pressed as an introduction to FXML, call the dialog.show() method, as you did when loading the WebView document. What should happen is that now the dialog will be displayed on top of the scene you created fxml.

You will notice that the dialogue itself contains a scene. If you want (and this is optional), you can define the contents of the scene using fxml. To do this, during the construction of the dialog, you set up a new fxml and a new controller for the contents of the dialog box and load the created fxml scene for the dialog in the dialog mode. The code for this is pretty much identical to the code you used to load the main fxml scene into primaryStage.

Try the above and see if it works for you. If you're still stuck, I can probably create a specific gxml example from the lines of this answer.

Also note that the link was written some time ago, and now there is the showAndWait method in JavaFX, which facilitates code blocking when making a dialog call, and then allows you to process the result of the dialog without using some of the event handler mechanisms from the gist example. Strategies with and without showAndWait are perfectly acceptable solutions.

+3
source

Source: https://habr.com/ru/post/1435607/


All Articles