如何使用处理程序javafx限制textfield中的最大值?
在我的文本字段中,我希望用户不要输入值> 127?
如何使用处理程序执行此操作?
我已经将文本字段限制为数值(使用正则表达式)
答案 0 :(得分:0)
If this example documentation是可以接受的,您应该可以访问生成KeyEvent的对象(如果我错了,请有人纠正我。)
String str= new String();
int iCharacter;
public void handle(KeyEvent keyEvent) {
str+=keyEvent.getCharacter();
//should be able to get the object like txtField_fxid.getText();
String input = fxid.getString();
int value = Integer.parseInt( input );
if( value > 0 && value < 127 ){
// do stuff
}
}
我今天回家后会更新。
答案 1 :(得分:0)
不是使用允许用户输入无效值然后忽略它们或修复它们的事件处理程序,而是考虑将TextField
子类化为仅允许有效输入。
import java.util.concurrent.Callable;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.IndexRange;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class RestrictedTextFieldExample extends Application {
@Override
public void start(Stage primaryStage) {
final TextField restrictedTextField = new TextField() {
private final Pattern numberPattern = Pattern.compile("\\d{1,3}");
@Override
public void replaceText(int start, int end, String text) {
if (valid(start, end, text)) {
super.replaceText(start, end, text);
}
}
@Override
public void replaceSelection(String text) {
IndexRange selectionRange = getSelection();
if (valid(selectionRange.getStart(), selectionRange.getEnd(), text)) {
super.replaceSelection(text);
}
}
private boolean valid(int start, int end, String text) {
String attemptedText =
getText().substring(0, start)
+ text
+ getText().substring(end);
if (attemptedText.length() == 0) {
return true ;
}
if (numberPattern.matcher(attemptedText).matches()) {
int value = Integer.parseInt(attemptedText);
if (value >= 0 && value <= 127) {
return true ;
}
}
return false ;
}
};
IntegerProperty value = new SimpleIntegerProperty();
value.bind(Bindings.createIntegerBinding(new Callable<Integer>(){
@Override
public Integer call() throws Exception {
String text = restrictedTextField.getText();
if (text.isEmpty()) {
return 0 ;
} else {
return Integer.parseInt(text);
}
}
}, restrictedTextField.textProperty()));
value.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable,
Number oldValue, Number newValue) {
System.out.printf("Value changed from %d to %d%n", oldValue.intValue(), newValue.intValue());
}
});
VBox root = new VBox(5);
root.getChildren().add(restrictedTextField);
Scene scene = new Scene(root, 250, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是Richard Bair在fx experience blog
中的例子答案 2 :(得分:0)
此方法允许TextField完成所有处理(复制/粘贴/撤消安全)。 不要重新制作扩展课程。 并允许您在每次更改后确定如何处理新文本 (将其推送到逻辑,或转回以前的值,甚至修改它)。
// fired by every text property change
textField.textProperty().addListener(
(observable, oldValue, newValue) -> {
// Your validation rules, anything you like
// (! note 1 !) make sure that empty string (newValue.equals(""))
// or initial text is always valid
// to prevent inifinity cycle
// do whatever you want with newValue
// If newValue is not valid for your rules
((StringProperty)observable).setValue(oldValue);
// (! note 2 !) do not bind textProperty (textProperty().bind(someProperty))
// to anything in your code. TextProperty implementation
// of StringProperty in TextFieldControl
// will throw RuntimeException in this case on setValue(string) call.
// Or catch and handle this exception.
// If you want to change something in text
// When it is valid for you with some changes that can be automated.
// For example change it to upper case
((StringProperty)observable).setValue(newValue.toUpperCase());
}
);
对于你的情况,只需在里面添加这个逻辑。效果很好。
if (newValue.equals("")) return;
try {
Integer i = Integer.valueOf(newValue);
if (i >= 0 && i <= 127) {
// do what you want with this i
} else {
((StringProperty)observable).setValue(oldValue);
}
} catch (Exception e) {
((StringProperty)observable).setValue(oldValue);
}