I'm creating a custom TextArea which accepts negative decimals while the user is writing.
This is what I found online for decimal TextAreas
public DecimalTextField() {
super();
UnaryOperator<Change> filter = new UnaryOperator<TextFormatter.Change>() {
@Override
public TextFormatter.Change apply(TextFormatter.Change t) {
if (t.isReplaced())
if(t.getText().matches("[^0-9]"))
t.setText(t.getControlText().substring(t.getRangeStart(), t.getRangeEnd()));
if (t.isAdded()) {
if (t.getControlText().contains(".")) {
if (t.getText().matches("[^0-9]")) {
t.setText("");
}
} else if (t.getText().matches("[^0-9.]")) {
t.setText("");
}
}
return t;
}
};
this.setTextFormatter(new TextFormatter<>(filter));
}
Now I need it to accept a "-" in the beginning but I think it's not as simple as it seems.