我正在编写JavaFX应用程序,我有6个 TextFields
这些是必填字段。我也有一个默认按钮,最初是无效的。
我想用逻辑检查所有字段是否已填满。我有一个变量estado
已启动false
。
编码:
if (!((TextField)event.getSource()).getText().trim().isEmpty())
estado = estado | true;
else
estado = false;
if (estado)
button.setDisabled(false)
所有TextFields
在KeyReleased
事件后获得该方法。
到目前为止,我的成绩并不好。
答案 0 :(得分:1)
这是基本的解决方案,如果你需要更多的PLZ告诉......
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package textfieldlogic;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author reegan
*/
public class TextFieldLogic extends Application {
@Override
public void start(Stage primaryStage) {
FormattedTextField btn = new FormattedTextField("^[1-9]+$");
btn.setText("Only Enter Numbers");
FormattedTextField btn1 = new FormattedTextField("^[a-z]+$");
btn1.setText("Only Enter Characters");
VBox vBox = new VBox(20);
vBox.getChildren().addAll(btn,btn1);
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public class FormattedTextField extends TextField {
Matcher matcher;
public FormattedTextField(String regex) {
Pattern pattern = Pattern.compile(regex);
matcher = pattern.matcher("");
EventHandler<KeyEvent> filter = new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
String s = getText() + t.getCharacter();
matcher.reset(s);
if (!( matcher.matches() || matcher.hitEnd())) {
t.consume();
}
}
};
addEventFilter(KeyEvent.KEY_TYPED, filter);
}
}
}
答案 1 :(得分:1)
查看this gist是否有帮助。验证工作在Controller文件中完成。这比你的问题要广泛得多,但可能会给你一些想法。
我将disableProperty绑定到各种BooleanBinding
,而不是侦听事件(或属性的更改)。这些BooleanBinding
封装了给定控件或控件集的“有效/无效”概念。
对于这个的简单版本,对于您的示例,您可以执行以下操作:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SimpleTextFieldValidation extends Application {
@Override
public void start(Stage primaryStage) {
TextField tf1 = new TextField();
TextField tf2 = new TextField();
TextField tf3 = new TextField();
TextField tf4 = new TextField();
TextField tf5 = new TextField();
TextField tf6 = new TextField();
Button button = new Button("Submit");
button.disableProperty().bind(
emptyTextFieldBinding(tf1)
.or(emptyTextFieldBinding(tf2))
.or(emptyTextFieldBinding(tf3))
.or(emptyTextFieldBinding(tf4))
.or(emptyTextFieldBinding(tf5))
.or(emptyTextFieldBinding(tf6))
);
VBox root = new VBox(5, tf1, tf2, tf3, tf4, tf5, tf6, button);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
Scene scene = new Scene(root, 150, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private BooleanBinding emptyTextFieldBinding(TextField textField) {
return Bindings.length(textField.textProperty()).isEqualTo(0);
}
public static void main(String[] args) {
launch(args);
}
}