Concatenate Javafx fx:Id

岁酱吖の 提交于 2019-12-20 04:52:43

问题


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 such a

@FXML
private Label Box01;
 (In function)
 String ExampleNum = "01";
 (Box+ExampleNum).setText("Test");

回答1:


In addition to the methods mentioned by @jewelsea here are 2 more ways to do this:

  1. Create & inject a Map containing the boxes as values from the fxml:

    <VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.Controller">
        <children>
            <Label text="foo" fx:id="a"/>
            <Label text="bar" fx:id="b"/>
            <Spinner fx:id="number">
                <valueFactory>
                    <SpinnerValueFactory.IntegerSpinnerValueFactory min="1" max="2"/>
                </valueFactory>
            </Spinner>
            <Button text="modify" onAction="#modify"/>
            <fx:define>
                <HashMap fx:id="boxes">
                    <box1>
                        <fx:reference source="a"/>
                    </box1>
                    <box2>
                        <fx:reference source="b"/>
                    </box2>
                </HashMap>
            </fx:define>
        </children>
    </VBox>
    

    Controller

    public class Controller {
    
        private Map<String, Label> boxes;
        @FXML
        private Spinner<Integer> number;
        @FXML
        private Label box1;
        @FXML
        private Label box2;
    
        @FXML
        private void modify(ActionEvent event) {
            boxes.get("box"+number.getValue()).setText("42");
        }
    
    }
    
  2. Pass the namespace of the FXMLLoader, which is a Map<String, Object> mapping fx:ids to the associated Objects, to the controller:

    <VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.Controller">
        <children>
            <Label text="foo" fx:id="box1"/>
            <Label text="bar" fx:id="box2"/>
            <Spinner fx:id="number">
                <valueFactory>
                    <SpinnerValueFactory.IntegerSpinnerValueFactory min="1" max="2"/>
                </valueFactory>
            </Spinner>
            <Button text="modify" onAction="#modify"/>
        </children>
    </VBox>
    

    Controller

    public class Controller implements NamespaceReceiver {
    
        private Map<String, Object> namespace;
        @FXML
        private Spinner<Integer> number;
        @FXML
        private Label box1;
        @FXML
        private Label box2;
    
        @FXML
        private void modify(ActionEvent event) {
            ((Label)namespace.get("box" + number.getValue())).setText("42");
        }
    
        @Override
        public void setNamespace(Map<String, Object> namespace) {
            this.namespace = namespace;
        }
    }
    
    public interface NamespaceReceiver {
        public void setNamespace(Map<String, Object> namespace);
    }
    

    Code for loading the fxml:

    public static <T> T load(URL url) throws IOException {
        FXMLLoader loader = new FXMLLoader(url);
        T result = loader.load();
        Object controller = loader.getController();
        if (controller instanceof NamespaceReceiver) {
            ((NamespaceReceiver) controller).setNamespace(loader.getNamespace());
        }
        return result;
    }
    



回答2:


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:

    <Label text="hello" fx:id="Box01" id="Box01"/>
    

    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.



来源:https://stackoverflow.com/questions/41391169/concatenate-javafx-fxid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!