Howto bind custom property of my own component in FXML file in javafx-8?


Question

This code work fine in javafx-2.2, but not work in javafx-8 (1.8.0-b132 from oracle downloads) Binding of property f2.myText not work. What's wrong?

test.fxml content:

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx8test.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"  xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
    <TextField fx:id="f1" layoutX="70" text="${f2.myText}" layoutY="20" />
    <MyTextField fx:id="f2" layoutX="70" layoutY="60" />
</children>
</AnchorPane>

MyTextField.java content:

package javafx8test;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TextField;

public class MyTextField extends TextField{
    private StringProperty myText = new SimpleStringProperty();

    public MyTextField(){
        super();
        textProperty().addListener(new ChangeListener<String>(){
            @Override
            public void changed(ObservableValue<? extends String> ov, String t, String t1) {
                myText.set( "("+t1+")");
            }
        });
    }

    public StringProperty myTextProperty(){
        return myText;
    }

    public void setMyText(String str){
        myText.set(str);
    }

    public String getMyText(){ 
        return myText.get(); 
    }

}

Javafx8Probs.java content:

package javafx8test;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class Javafx8Probs extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {

        URL url =Javafx8Probs.class.getResource("test.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader(url);
        Parent root = (Parent) fxmlLoader.load();

        Stage s = new Stage();

        Scene scene=new Scene(root);
        s.setScene(scene);

        s.show();

        TextField f1 = (TextField)scene.lookup("#f1");
        MyTextField f2 = (MyTextField)scene.lookup("#f2");

        f2.setMyText("asdasd");
    }

}
1
2
3/19/2014 1:37:47 PM

Accepted Answer

That looks like a bug: you should file it at https://javafx-jira.kenai.com

The fix is to initialize your property as a fully-fledged property of the class, that provides correct values for getBean() and getName(). This is good practice anyway, but probably shouldn't be necessary for your binding to work.

private StringProperty myText = new SimpleStringProperty(this, "myText");
2
3/19/2014 5:39:19 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon