Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX class controller scene reference

Is there any way of getting the Scene object of an FXML loaded file from the associated class controller.

I'm doing something like this:

@FXML
private AnchorPane anchor; 

Scene scene = anchor.getScene();

but i'd like a solution that does not reference the AnchorPane control.

like image 811
nailujed Avatar asked Oct 17 '12 13:10

nailujed


People also ask

What does FXML stand for?

FX Markup Language. In the same way that HTML is for Hypertext Markup Language, and many more acronyms that end in ML .

How do I get the stage from FXML controller?

There are three main ways to get access a Stage from within a controller: Access the Stage through a node. Set the Stage using a custom controller method. Get the Stage through an ActionEvent.

What is FXMLLoader?

The FXMLLoader object is a mixed-function class with the responsibility to parse FXML content (as XML), build the Scene Graph and initialise the Controller of a JavaFX View. JavaFX's FXML markup language allows you to separate the business logic of a program from the user interface design.

What is the purpose of the attribute FX controller?

As discussed earlier, the fx:controller attribute allows a caller to associate a "controller" class with an FXML document. A controller is a compiled class that implements the "code behind" the object hierarchy defined by the document.


2 Answers

Why not? Controller is an abstract class, he's not aware about UI unless you deliberately make him know.

Nodes (inlcuding AnchorPane) are another story, they hardly exists outside for scenegraph. So it's perfectly fine to ask Node about his parent or scene.

If you still want to handle that separately there are next approaches:

  1. you can create a custom controller and set scene after loader. Just note that at the time initialize() called it wouldn't yet initialized.

    public class MyController {
        private void Scene scene;
        public void setScene(Scene scene) { this.scene = scene; }
    
    }
    
    // loading code
    FXMLLoader fxmlLoader = new FXMLLoader();
    AnchorPane root = (AnchorPane) fxmlLoader.load(getClass().getResource("MyApp.fxml"));
    MyController myController = (MyController) fxmlLoader.getController();
    myController.setScene(scene);
    
  2. You can create a custom fxml control which will incorporate controller and he can just call getScene() for itself. See an example here: https://stackoverflow.com/a/10718683/1054140

like image 160
Sergey Grinev Avatar answered Sep 22 '22 06:09

Sergey Grinev


I tried your answer, but it did not work, I found the reason here:
JavaFX: How to get stage from controller during initialization?
after the comment:

// loading code 

don't use the static load method

AnchorPane root=(AnchorPane) FXMLLoader.load(getClass().getResource("MyApp.fxml"));

but instead use instantiated loader's method

AnchorPane root=(AnchorPane) fxmlLoaded.load(getClass().getResource("MyApp.fxml"));
like image 45
kernel255 Avatar answered Sep 24 '22 06:09

kernel255