2

I want to make a grid with a specific amount of buttons. I know how many buttons there are need to be because I get the number of rows and columns.

I could do a loop, but I don't know how you can place buttons next to eachother and underneath.
Secondly, the buttons need a Text and an Id, text is no problem, but how do you give them an id?
And at last, and probably most difficult, it can occur that there are a lot of rows, so that a scrollbar should be available.

At the end it should look something like this:

enter image description here

fangio
  • 1,746
  • 5
  • 28
  • 52
  • well actually I'm new to javafx, and for all my other scenes I used a scenebuilder, so I looked some up but I really don't find how to place buttons next to eachother and underneath.. and do I have to give it a start coordinate in the scene I really have no idea, I showed a picture how it should look like. I know how to make circle buttons ( I looked it up, css-style sheets) – fangio Apr 16 '15 at 16:02
  • I added a little code I currently have, problem is id of buttons – fangio Apr 16 '15 at 16:14
  • What is the problem with `id`? You can always set `button.setId("some_value");` inside the loop. – ItachiUchiha Apr 16 '15 at 16:43
  • 1
    fangio, it seems you already have a working solution, just remove the code you have in your question, put it an answer and, after the 48 hour time limit, [mark it correct](http://stackoverflow.com/help/self-answer). – jewelsea Apr 16 '15 at 18:19
  • Okay but @ItachiUchiha, i read somewhere (i am a student) that there are 2 kinds of ids... Am I wrong? – fangio Apr 16 '15 at 22:04
  • @jewelsea actually I still have some questions, How can I add an action on the button and that action method needs the text of the button and change the style of the button, I should've mentioned this in the question. – fangio Apr 16 '15 at 22:11
  • You are completely correct. There is a [`idProperty`](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#idProperty--) which can be set to the node and used for applying`css properties` and `node lookup`. The other is called as `fx:id` which is used to `bind` nodes defined in fxml into the controller. You may find some information [here](http://stackoverflow.com/questions/28610966/how-to-keep-css-style-for-only-one-element/28612383#28612383) and [here](http://stackoverflow.com/questions/23686325/whats-the-difference-between-fxid-and-id-in-javafx) – ItachiUchiha Apr 16 '15 at 22:14
  • 1
    @fangio Please ask a different question, instead of asking unrelated question in the comment. Keep the comment thread for asking further clarifications to the current posted question. – ItachiUchiha Apr 16 '15 at 22:16

2 Answers2

5
@Override
public void start(Stage stage) {
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(BUTTON_PADDING));
    grid.setHgap(BUTTON_PADDING);
    grid.setVgap(BUTTON_PADDING);

    for (int r = 0; r < NUM_BUTTON_LINES; r++) {
        for (int c = 0; c < BUTTONS_PER_LINE; c++) {
            int number = NUM_BUTTON_LINES * r + c;
            Button button = new Button(String.valueOf(number));
            grid.add(button, c, r);
        }
    }

    ScrollPane scrollPane = new ScrollPane(grid);

    stage.setScene(new Scene(scrollPane));
    stage.show();
}
fangio
  • 1,746
  • 5
  • 28
  • 52
-1

The best solution would be:

itemNumber starts from 0 to N: 
    Grid.getChildren().get(itemNumber).setId("bt"+itemNumber);
    Grid.getChildren().get(itemNumber).getId();
Antoine C.
  • 3,730
  • 5
  • 32
  • 56