JavaFX - 如何将值设置为选项卡内的文本字段?

时间:2018-06-19 07:34:02

标签: java javafx fxml

我正在尝试将值设置为Tab内的文本字段。我有多个选项卡,我想在每个选项卡中设置值到文本字段。 有关如何在选项卡中设置文本字段文本的任何想法? 我使用下面的代码更新文本字段的值,但尝试执行相同操作时没有发生任何事情。

代码:

public class FXMLController {
    @FXML
    private Button inputXmlFileBtn;
    @FXML
    private TextField inputXmlName;
    @FXML
    private TabPane xmlData;
    @FXML
    private Tab vendorHeaderFb;
    @FXML
    private TextField vendorHeader1;
    Label label;

    public String inputXmlFileChooser() throws ParserConfigurationException,
            SAXException, IOException, JAXBException {
        FileChooser fileChooser = new FileChooser();
        // Set extension filter
        fileChooser.getExtensionFilters().addAll(
                new ExtensionFilter("XML files (*.xml)", "*.xml"));

        // Open Dialog
        File file = fileChooser.showOpenDialog(null);
        String xmlPath = "";
        xmlPath = file.getPath();

        // Set the path for inputXmlName text field
        if (file != null) {
            inputXmlName.setText(xmlPath);
        }

        //Unmarshall
        label = this.unmarshallXml();
        System.out.println(label.getVendorHeader1());
        vendorHeaderFb = new Tab();
        vendorHeader1 = new TextField();
        vendorHeader1.setText(label.getVendorHeader1());
        vendorHeaderFb.setContent(vendorHeader1);
        return xmlPath;
    }

更新了代码,包括FXML的Pojo类。

public class FXMLController {
    @FXML
    private Button inputXmlFileBtn;
    @FXML
    private TextField inputXmlName;
    @FXML
    private TabPane xmlData;
    @FXML
    private Tab vendorHeaderFb;
    @FXML
    private TextField VendorHeader1;
    Label label;

    public String inputXmlFileChooser() throws ParserConfigurationException,
            SAXException, IOException, JAXBException {
        FileChooser fileChooser = new FileChooser();
        // Set extension filter
        fileChooser.getExtensionFilters().addAll(
                new ExtensionFilter("XML files (*.xml)", "*.xml"));

        // Open Dialog
        File file = fileChooser.showOpenDialog(null);
        String xmlPath = "";
        xmlPath = file.getPath();

        // Set the path for inputXmlName text field
        if (file != null) {
            inputXmlName.setText(xmlPath);
        }

        //Unmarshall
        label = this.unmarshallXml();
        System.out.println(label.getVendorHeader1());
        FXMLProps fxmlProps = new FXMLProps();
        fxmlProps.setVendorHeader1(label.getVendorHeader1());
        System.out.println(fxmlProps.getVendorHeader1());
        VendorHeader1 = new TextField();
        VendorHeader1.setText(fxmlProps.getVendorHeader1());
        //vendorHeaderFb.setContent(vendorHeader1);
        //vendorHeader1.setText(label.getVendorHeader1());
        //vendorHeaderFb.setContent(vendorHeader1);


        return xmlPath;
    }

POJO / Property Class

public class FXMLProps {
    private final SimpleStringProperty VendorHeader1 = new SimpleStringProperty(
            "");

    public FXMLProps() {
    }

    public FXMLProps(String VendorHeader1) {
        setVendorHeader1(VendorHeader1);
    }

    public String getVendorHeader1() {
        return VendorHeader1.get();
    }

    public void setVendorHeader1(String vH1) {
        VendorHeader1.set(vH1);
    }
}

我仍无法设置文本字段vendorHeader1的值。 有人可以指出出了什么问题吗?

1 个答案:

答案 0 :(得分:1)

您必须在Binding的text属性和用于该值的TextField之间应用SimpleStringProperty。您必须通过启用其他类中的FXMLProps选项的方式来公开Binding的供应商标头属性:

public class FXMLProps {

    private final SimpleStringProperty vendorHeader = new SimpleStringProperty("");

    public FXMLProps() {}

    public FXMLProps(String vendorHeader) {
        setVendorHeader(vendorHeader);
    }

    public String getVendorHeader() {
        return VendorHeader1.get();
    }

    public void setVendorHeader(String vendorHeaderText) {
        vendorHeader.set(vendorHeaderText);
    }

    // this is needed for the Binding
    public final SimpleStringProperty vendorHeaderProperty() {
        return vendorHeader;
    }
}

然后在应用程序中的某个位置(可能在start()中),您需要像这样创建Binding

// bind those two properties (TextField, SimpleStringProperty)
Bindings.bindBidirectional(vendorHeader1.textProperty(), fxmlProps.vendorHeaderProperty());