Jestino Sam
Jestino Sam

Reputation: 602

JavaFX - How to set value to a textfield inside a tab?

I am trying to set value to a text field which is inside a Tab. I am having multiple tabs and I want to set value to text field inside each tab. Any idea as to how to set the text for textfield inside the tab? I am using the below code to update the value for the textfield, but nothing's happening while trying to do the same.

Code:

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;
    }

Updated Code including the Pojo class for FXML.

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);
    }
}

I am still not able to set the value for text field vendorHeader1. Can someone point out what's going wrong?

Upvotes: 0

Views: 852

Answers (1)

deHaar
deHaar

Reputation: 18568

You have to apply a Binding between the text property of the TextField and the SimpleStringProperty that is used for the value. You will have to make the vendor header property of your FXMLProps public in a way that enables Binding options in other classes:

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;
    }
}

Then somewhere in your application (maybe in start()) you need to create the Binding like

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

Upvotes: 1

Related Questions