Hauke
Hauke

Reputation: 1465

JavaFX TextField in Column with Typefilter

I have a simple JavaFX Table with a column like this:

@FXML
private TableColumn<PropertyModel, String> columnPropertyProdValue;

Within the initialize method I used this

columnPropertyProdValue.setCellFactory(TextFieldTableCell.forTableColumn());

in order to get an textarea at a double click event on this column. This is working fine.

The column should store different property values but the datatype depends on a different column in the table. The first column "datatype" defines the datatype like boolean, string or integer and the property column should store its value. But in the moment it is always a String.

Its fine for me if I store the property value as a String in the database but the application should check for correctness of the datatype at runtime.

Does anyone has a good idea how to realize that?

Thanks a lot Hauke

Upvotes: 0

Views: 57

Answers (1)

Evgeny
Evgeny

Reputation: 374

For TextInputControl and subclasses you can apply TextFormatter. This class allows you to control input. For example (for float number):

setTextFormatter(new TextFormatter<String>(
            s -> {
                if (s.getControlNewText().isEmpty())
                    return s;
                try{
                    Float.parseFloat(s.getControlNewText());
                    return s;
                } catch (NumberFormatException e) {
                    return null;
                }
            }

Upvotes: 0

Related Questions