You can create these buttons in the Java code. Not declaring them in FXML doesn't mean they won't belong to your scene. What you have to do is give the fx:id
tag to a container in which you would like to put more buttons, declare it in a Controller
class with the @FXML
annotation and then put some new Nodes
there. Look at the simple app below (assuming all the files are in the sample
package) - I created some buttons in FXML file and some via the Java code.
Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
sample.fxml:
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<VBox fx:id="vBox" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
<Button text="FXML_Button_1"/>
<Button text="FXML_Button_2"/>
<Button text="FXML_Button_3"/>
</VBox>
Controller.java:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
public class Controller {
@FXML
VBox vBox;
private final int BUTTONS_NUMBER_OF_ROWS = 5;
private final int BUTTONS_NUMBER_OF_COLUMNS = 5;
private Button [][] buttons = new Button[BUTTONS_NUMBER_OF_ROWS][BUTTONS_NUMBER_OF_COLUMNS];
@FXML
private void initialize() {
initializeButtonsArray();
putButtonsOnGrid();
}
private void initializeButtonsArray() {
for (int i = 0 ; i < BUTTONS_NUMBER_OF_COLUMNS ; i++) {
for (int j = 0 ; j < BUTTONS_NUMBER_OF_ROWS ; j++) {
buttons[i][j] = new Button("Button_" + i + j);
}
}
}
private void putButtonsOnGrid() {
GridPane buttonsGridPane = new GridPane();
for (int i = 0 ; i < BUTTONS_NUMBER_OF_COLUMNS ; i++) {
for (int j = 0 ; j < BUTTONS_NUMBER_OF_ROWS ; j++) {
buttonsGridPane.add(buttons[i][j], i, j);
}
}
vBox.getChildren().add(buttonsGridPane);
}
}
