How I can make it as Form and by clicking "add" to create new Form.
@FXML
private DatePicker date_1;
@FXML
private DatePicker date_2;
@FXML
private ChoiceBox<String> choiceBox;
@FXML
private TextField lavozm;
@FXML
private Label year;
@FXML
private Label month;
@FXML
private Label day;
and a button add
@FXML
private Button buttonAdd;
I can send full code if it will be required
CodePudding user response:
To reuse the FXML form multiple times, you can create a custom control. Oracle has a tutorial on the subject here.
Basically, you create a controller class that extends the root node type of the form (eg. VBox). Then, replace the root element tag in your FXML with the <fx:root type="full.name.of.root.node.Type" xmlns:fx="http://javafx.com/fxml"> tag, this tells JavaFX that your controller class should become the same object as the root node loaded from the FXML. When you load the FXML file, make sure you create an instance of FXMLLoader instead of using the static load method so that you can call the setRoot(myCustomCotrollerObj) method prior to calling the instance load method. To make the form easily reusable, you can put all of this code in the constructor of your controller class so creating a new instance of the controller automatically loads the FXML file and you can simply add the new object to any parent node.
Note that the root object doesn't need to be a controller but that is usually the way it is done and that is how the tutorial does it.
In your case, when the button is clicked you would create a new custom control object and add it to the children of whatever parent node it goes in.
