0

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?

Jestino Sam
  • 526
  • 2
  • 12
  • 31
  • You could create a text property and bind all the `TextField` values to it. Then just alter the property. Have a look here: https://stackoverflow.com/questions/22622636/javafx-8-how-to-bind-textfield-text-property-to-tableview-integer-property – deHaar Jun 19 '18 at 07:38
  • @deHaar Thanks! But can you add some code snippet for the same? I am new to using Tabs. – Jestino Sam Jun 19 '18 at 07:39
  • 1
    There are code snippets in the link and you can look for `Binding`, `Property` and `TextField` at stackoverflow as well as everywhere else. Forget about those `Tabs`, you only need to bind (each) `TextField`'s text property to the property for the text value. Those properties are very similar to regular class attributes. I just don't have time now for an example... Try something and post it here when errors or questions come up. – deHaar Jun 19 '18 at 07:42
  • Nothing happens since you don't do anything with the tab with the textfield you just created. Replacing the value in a field, even in one that is annotated with `@FXML`, does nothing to the new and/or old value. You still need to add the tab to the tabpane. BTW: Beware of `NullPointerExceptions`: `file.getPath(); if (file != null) {` – fabian Jun 19 '18 at 08:24
  • @deHaar I have updated the code with Property Class. I am able to print the value of VendorHeader1 in console but when I try setting the value for the textfiled, it doesn't happen so. – Jestino Sam Jun 19 '18 at 12:02

1 Answers1

1

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());
deHaar
  • 17,687
  • 10
  • 38
  • 51