0
我正在使用Scene Builder和jdk8创建Java Fx应用程序。我有各种文本字段来查找数字输入。一旦文本框失去焦点,我希望能够格式化这些文本框。JavaFx TextField焦点丢失格式化
我一直在使用DecimalFormat df = new DecimalFormat(“###### 0.00”);总计文本字段的结果,但不在输入文本字段上。
任何帮助非常感谢。
我正在使用Scene Builder和jdk8创建Java Fx应用程序。我有各种文本字段来查找数字输入。一旦文本框失去焦点,我希望能够格式化这些文本框。JavaFx TextField焦点丢失格式化
我一直在使用DecimalFormat df = new DecimalFormat(“###### 0.00”);总计文本字段的结果,但不在输入文本字段上。
任何帮助非常感谢。
TextField tf1=new TextField();
TextField tf2=new TextField();
TextField tf3=new TextField();
// add focus listener to all textFields
tf1.focusedProperty().addListener(new TextFieldListener(tf1));
tf2.focusedProperty().addListener(new TextFieldListener(tf2));
tf3.focusedProperty().addListener(new TextFieldListener(tf3));
类实现的ChangeListener
class TextFieldListener implements ChangeListener<Boolean> {
private final TextField textField ;
TextFieldListener(TextField textField) {
this.textField = textField ;
}
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if(!newValue) // check if focus gained or lost
{
this.textField.setText(getFormattedText(this.textField.getText());
}
}
public String getFormatedText(String str)
{
//return formated text
}
}
final ChangeListener<? super Boolean> focusListener = (o,ov,nv)->{
if(!nv){
TextField tf = (TextField)((ReadOnlyBooleanPropertyBase)o).getBean();
//put your code here
}
}
tf1.focusedProperty().addListener(focusListener);
tf2.focusedProperty().addListener(focusListener);
tf3.focusedProperty().addListener(focusListener);
[添加的ChangeListener(http://stackoverflow.com/a/16971194/1315392),并调用您的格式功能丢失焦点时 – vinay 2014-09-22 13:05:17
@vinay - 我是新来的事件处理,到现在只需要处理按钮操作。我已经看过重点听众,并知道有一个重点获得和丢失的方法,但我不确定如何实施它们。 – Decom1 2014-09-22 13:12:28