Concatenate Javafx fx:Id

后端 未结 2 1492

I\'m kinda new to JavaFX and currently trying to do a Calendar application for a school project. I was wondering if there was a way to concatenate a fx:id

2条回答
  •  粉色の甜心
    2021-01-27 02:47

    Various possible solutions:

    1. You could use reflection, but that would be ugly and I wouldn't advise it.

    2. Normally, if you have a lot of things, you put them in a collection like a list or array. The label will be a child of some layout pane, so you can get the children of the pane and lookup an item by index with something like:

      ((Label) parent.getChildren().get(0)).setText("Text");
      
    3. If the label has been assigned a css id then you can use that to lookup the label.

      For example, in your FXML define:

      Then you can lookup the label using:

      String boxNum = "01";
      Label box = (Label) parent.lookup("#Box" + boxNum);
      
    4. Just refer to the item by it's reference:

      @FXML private Label box01; 
      box01.setText("Test");
      

    Aside: Please use camel case as per standard Java conventions.

提交回复
热议问题