2

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

Crescens Tan
  • 21
  • 1
  • 2
  • Also, if the button's text is on the left and that's what you want to use, why not use `getText()`? – SedJ601 Jun 28 '17 at 05:06
  • 3
    Maybe this will help: https://stackoverflow.com/questions/34189259/how-can-i-get-the-indexes-of-each-button-clicked-for-my-program/34189791 Also, note that `fx:id` is only used for injection when using `FXMLLoader`, so it makes no sense to use it when creating your buttons in code. the `id` property referred to by @SedrickJefferson is not the same as the `fx:id` attribute in the FXML: https://stackoverflow.com/questions/23686325/whats-the-difference-between-fxid-and-id-in-javafx – Itai Jun 28 '17 at 05:46
  • Why do you need the `Button`s to access the text? You stored the text in the `eventsList` list. Simply reference this text by using the list index with `eventsList.get`. – fabian Jun 28 '17 at 15:47
  • Based on your description I don't know what you like to archieve. What do you mean by "I wanted the label of the buttons on the left to appear on the label on the right"? A new label for each button? – Manuel Mauky Jun 28 '17 at 16:16
  • Alright, thanks for the help guys. I've found a solution where I can throw the buttons into an array list and continue things from there on. Thank You! – Crescens Tan Jun 29 '17 at 03:18

0 Answers0