I'm recently writing a javafx application and in one of its parts the client must wait for the server to get a list of people and after getting the list it must be used in a listview that is going to be added to a parent. That parent is and fxml file and after loading it I want to know if it is possible to add the vbox containing the listview to the parent or not. I'd be grateful if anyone could help...
-
2Using `FXMLLoader`+fxml is just one way of creating a scene. The resulting object structure is no different from a scene that is created by creating the same object structure in java code. The scene can be modified just the same way. – fabian Jun 10 '18 at 20:30
1 Answers
From your question I gather you are not familiar with the idea of a controller or the associated FXML injection performed by a FXMLLoader
. This answer by James_D goes over the very basics of the JavaFX lifecycle but it first goes over the basics of the procedure involved when loading a FXML file. If you want to modify the scene-graph that is loaded via FXML then you need to use a controller class with the appropriate FXML
annotated fields. For example, let's say your parent is a BorderPane
. In your FXML file you'd have:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="parent" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/10.0.1"
fx:controller="some.package.YourController">
<top>
<!-- maybe have something like a MenuBar here -->
</top>
<bottom>
<!-- maybe have a some type of status bar here -->
</bottom>
</BorderPane>
Notice the fx:controller
attribute; it is the class name of the class to instantiate and use as a controller. Also note the fx:id
attribute. In your controller class you'd have:
package some.package;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXML;
public class YourController {
@FXML
private BorderPane parent; // field name matches the fx:id attribute
@FXML
private void initialize() {
// do any initializing if needed (if not, get rid of this method)
// you can access and modify any FXML injected field from this method
}
}
Then you can use the parent via the controller instance. You could also create and link event handler methods in the controller and do certain behavior based on user actions. It's important to note, however, that how you change the state of the UI in the controllers depends on how you access you model classes. You'll need to have the model available to your controller and possibly have it shared between mutliple controllers. There are a decent number of questions/answers on Stack Overflow about how to do this already.
Here is another resource that may help you: Introduction to FXML.

- 37,820
- 8
- 53
- 80