I'm trying to bind a DoubleProperty to a TextField and the only binding functionnal is the bidirectionnal and not the unidirectionnal binding.
My specific TextField :
public class DoubleTextField extends TextField{
public DoubleTextField() {
super();
Pattern validDoubleText = Pattern.compile("-?(\\d*)|(\\d+\\.\\d*)");
UnaryOperator<TextFormatter.Change> doubleCheck = change -> {
String newText = change.getControlNewText();
if (validDoubleText.matcher(newText).matches()) {
return change;
} else
return null;
};
this.setTextFormatter(new TextFormatter<Double>(new DoubleStringConverter(), null, doubleCheck));
}
}
public class Controller implements Initializable {
@FXML
GridPane gpTest;
@FXML
TextField tfUSDT;
Model model;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
this.model = new Model();
DoubleTextField dtfPrice = new DoubleTextField();
gpTest.add(dtfPrice, 1, 0);
tfUSDT.textProperty().bind(model.getDpActualUsdt().asString("%f"));
//dtfPrice.textProperty().bind(model.getDpActualPrice().asString("%f"));
dtfPrice.textProperty().bindBidirectional(model.getDpActualPrice(), new NumberStringConverter());
}
public void modifyUSDT(ActionEvent e) {
this.model.getDpActualUsdt().set(50);
}
public void modifyPrice(ActionEvent e) {
this.model.getDpActualPrice().set(50);
}
}
public class Model {
private DoubleProperty dpActualUsdt, dpActualPrice;
public Model() {
dpActualPrice = new SimpleDoubleProperty(0);
dpActualUsdt = new SimpleDoubleProperty(0);
}
public void setDpActualUsdt(DoubleProperty dpActualUsdt) {
this.dpActualUsdt = dpActualUsdt;
}
public void setDpActualPrice(DoubleProperty dpActualPrice) {
this.dpActualPrice = dpActualPrice;
}
public DoubleProperty getDpActualPrice() {
return dpActualPrice;
}
public DoubleProperty getDpActualUsdt() {
return dpActualUsdt;
}
}
<FlowPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<GridPane fx:id="gpTest">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="tfUSDT" />
<Button mnemonicParsing="false" onAction="#modifyUSDT" text="Modify USDT" GridPane.rowIndex="1" />
<Button mnemonicParsing="false" onAction="#modifyPrice" text="Modify Price" GridPane.columnIndex="1" GridPane.rowIndex="1" />
</children>
</GridPane>
</children>
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader fxml = new FXMLLoader(getClass().getResource("/resources/ihm.fxml"));
Pane mainPane = fxml.load();
Scene scene = new Scene(mainPane);
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Here are my classes
With this the code work as expected but if i comment dtfPrice.textProperty().bindBidirectional(model.getDpActualPrice(), new NumberStringConverter()) and decomment //dtfPrice.textProperty().bind(model.getDpActualPrice().asString("%f"));
He doesn't work
Thanks for everyone helping me !