A question pertaining to javaFX and Scene Builder: I want to assign a fx: id to a button, that I've created by a loop, because I need the text in that button to create another label on another pane.
This is the main class
package People;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* Created by 2e3cr on 27/6/2017.
*/
public class Main extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Parent m = FXMLLoader.load(getClass().getResource("eventGroup.fxml"));
Scene scene = new Scene(m);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the controller of a FXML file called EventGroupController
package People;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
/**
* Created by 2e3cr on 23/6/2017.
*/public class EventGroupController implements Initializable{
@FXML
private AnchorPane eventsInf;
@FXML
private Label eventName;
@FXML
private VBox events;
ArrayList<String> eventsList = new ArrayList<String>();
@Override
public void initialize(URL location, ResourceBundle resources) {
eventsList.add("event 1");
eventsList.add("event 2");
eventsList.add("event 3");
eventsList.add("event 4");
eventsList.add("event 5");
eventsList.add("event 6");
eventsList.add("event 7");
eventsList.add("event 8");
eventsList.add("event 9");
Button btn;
for(int i = 0; i < eventsList.size(); i++){
btn = new Button(eventsList.get(i));
btn.setPrefWidth(195);
btn.setPrefHeight(100);
btn.setStyle("-fx-background-color : #808080");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
events.getChildren().add(btn);
Separator sep = new Separator();
sep.setPrefWidth(200);
sep.setPrefHeight(10);
events.getChildren().add(sep);
}
}
}
and my controller currently look like this
https://i.stack.imgur.com/vZwnS.png
as you can see, I wanted the label of the buttons on the left to appear on the label on the right but I don't know how. Pardon me if this questions have already been answered but I have tried googling "how to assign fx:id to a button generated by a loop" but to no avail