-1

I can have FXML file with root tag:

<BorderPane fx:id="mypane" fx:controller="my.package.MyController" 
xmlns="http://javafx.com/javafx/8.0.91" xmlns:fx="http://javafx.com/fxml/1" />

And I can autowire it into controller

public class MyController {
   @FXML
   BorderPane mypane;
}

But can I do the same without assigning fx:id? The control is topmost, then what is the need to name it? Can I refer it as "root" somehow?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • The short answer is yes, but the long answer is why are you trying to do this? Do you just want to save yourself from having to name your parent elements with an fx:id? – IanGabes Jun 02 '16 at 16:51
  • @IanGabes OK, I'm intrigued now: how? – James_D Jun 02 '16 at 16:51
  • Depends on your version, but this is the defacto answer: http://stackoverflow.com/questions/23600926/how-to-understand-and-use-fxroot-in-javafx – IanGabes Jun 02 '16 at 16:56
  • @IanGabes That's somewhat different, isn't it? There you have to (one way or another) instantiate the root node in Java, and set it on the `FXMLLoader`. In the posted question, the root node is determined by an FXML tag. (However, "Why are you trying to do this" is a very valid question.) – James_D Jun 02 '16 at 17:08
  • @James_D In my understanding of the question, it seems like the OP simply wants to load fxml without using an fx:id attribute for his root element. I assume he is trying to create a component, and loading the fxml using the FXMLoader is the way to do this. You are right, that this is different than having the fxml auto-magically loaded, but this FXMLoader process is what happens in the back anyways. – IanGabes Jun 02 '16 at 17:23
  • 1
    I guess we need to wait for the OP to clarify. However, using `` you *must* have a reference to the root node (even if that reference is `this`) *prior* to loading the FXML - that seems more of a requirement than adding an `fx:id` attribute, not less... – James_D Jun 02 '16 at 17:25

1 Answers1

1

As far as I know there is no "default fx:id" for the root node. The only elements I know of that can be injected without an explicit fx:id are ones that are not actually defined in the FXML file:

@FXML
private URL location ;

@FXML
private ResourceBundle resources ;

Note the FXMLLoader's namespace also contains a value for the controller (with key "controller"), but it is not injected into the controller (it would just be identical to this in that context anyway...)

James_D
  • 201,275
  • 16
  • 291
  • 322