palexdev / MaterialFX

A library of material components for JavaFX
GNU Lesser General Public License v3.0
1.21k stars 122 forks source link

MFXTextField - setting a new content from text property listener does not work #252

Closed jbenak8 closed 1 year ago

jbenak8 commented 1 year ago

There is a problem that when you add a text property listener to the MFXTextField and in the listener you are setting a new value fo the text field, the value is not set to the text field. The purpose of this is to prevent entering unwanted values into the field.

For example, I want only to fill in the text field only 8-digit number, so I write this code:

MFXTextField textField = new MFXTextField(); textField.textProperty().addListener((observable, oldVal, newVal) -> { if (!newVal.matches("(\\d{1,8})") && !newVal.isEmpty()) { textField.setText(oldVal); } });

I expected by this example, that if I try to enter a string 859abc then in the text field will be written just 859

As it works for normal javafx.scene.control.TextField I expected the same behavior also for MFXText field. However entering unwanted characters, which does not match criterion above, is still possible because setText() does not work.

This occurs in versions 11.13.5 and 11.14.0-EA3

palexdev commented 1 year ago

There's nothing much I can do about this since MFXTextField is just a wrapper for the standard text field

What you could do is to use a TextFormatter instead, make sure to check the docs too

jbenak8 commented 1 year ago

Thank you, I will try it.