如何禁用按钮时,文本字段为空? [英] How to disable Button when TextField is empty?

查看:159
本文介绍了如何禁用按钮时,文本字段为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下code我有一个文本字段和一个按钮。我需要禁用按钮时曾经的文本字段是空的,这样我就可以尽量避免进入空值的数据库。我怎样才能使按钮停用?

 私人垂直框addVBox(){    垂直框VB1 =新的垂直框();
    vb1.setPadding(新插图(15,20,25,20));
    vb1.setSpacing(15);
    vb1.setStyle( - FX-背景色:#333333;);    最后标签标签=新标签(员工详细信息);
    label.setFont(Font.font(宋体,为FontWeight.BOLD,20));
    label.setTextFill(Color.WHITE);    TableColumn的分=新的TableColumn(员工姓名);
    sub.setMinWidth(400);
    sub.setCellValueFactory(
            新的PropertyValueFactory<工作人员,字符串>(子名));    table.setItems(数据);
    。table.getColumns()的addAll(分);    addSubName =新的TextField();
    addSubName.setPromptText(员工姓名);
    addSubName.set prefSize(200,30);    最终按钮B2 =新按钮(添加);
    b2.setFont(Font.font(宋体,为FontWeight.BOLD,17));
    b2.set prefSize(70,30);
    b2.setStyle(-fx基:#0066FF;);
    b2.setTextFill(Color.BLACK);     b2.setOnAction(新的EventHandler<&ActionEvent的GT;(){
        @覆盖
        公共无效手柄(ActionEvent的五){            味精= addSubName.getText();
            尝试{
                enterStaff();
            }赶上(ClassNotFoundException的|的SQLException前){
                。Logger.getLogger(AddStaff.class.getName())日志(Level.SEVERE,空,前);
            }            data.add(新职工(addSubName.getText()));
            addSubName.clear();
            }
     });    。hb.getChildren()的addAll(addSubName,B2);
    hb.setSpacing(5);    。vb1.getChildren()的addAll(标签,表,HB);
    返回VB1;}


解决方案

另一种方法可以使用​​绑定:

 最终文本字段extField1的=新的TextField();
最终文本字段文本字段2 =新的TextField();
最终文本字段文本字段3 =新的TextField();BooleanBinding BB =新BooleanBinding(){
    {
        super.bind(textField1.textProperty(),
                textField2.textProperty(),
                textField3.textProperty());
    }    @覆盖
    保护布尔computeValue(){
        回报(textField1.getText()的isEmpty()
                &功放;&安培; textField2.getText()的isEmpty()
                &功放;&安培; 。textField3.getText()的isEmpty());
    }
};按钮BTN =新按钮(按钮);
。btn.disableProperty()绑定(BB);垂直框VBOX =新的垂直框();
。vBox.getChildren()的addAll(extField1的文字栏,文字栏,BTN);

In the following code I have a TextField and a Button. I need to disable the Button when ever the TextField is empty, so that I can avoid entering empty values to the database. How can I make the button disabled ?

    private VBox addVBox() {

    VBox vb1 = new VBox();
    vb1.setPadding(new Insets(15, 20, 25, 20));
    vb1.setSpacing(15);
    vb1.setStyle("-fx-background-color: #333333;");

    final Label label = new Label("Staff Details");
    label.setFont(Font.font("Arial", FontWeight.BOLD, 20));
    label.setTextFill(Color.WHITE);

    TableColumn sub = new TableColumn("Staff Name");
    sub.setMinWidth(400);
    sub.setCellValueFactory(
            new PropertyValueFactory<Staff, String>("subName"));

    table.setItems(data);
    table.getColumns().addAll(sub);

    addSubName = new TextField();
    addSubName.setPromptText("Staff Name");
    addSubName.setPrefSize(200, 30);

    final Button b2 = new Button("Add");
    b2.setFont(Font.font("Calibri", FontWeight.BOLD, 17));
    b2.setPrefSize(70, 30);
    b2.setStyle(" -fx-base: #0066ff;");
    b2.setTextFill(Color.BLACK);

     b2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {

            msg = addSubName.getText();
            try {
                enterStaff();
            } catch ( ClassNotFoundException | SQLException ex) {
                Logger.getLogger(AddStaff.class.getName()).log(Level.SEVERE, null, ex);
            }

            data.add(new Staff(addSubName.getText()));
            addSubName.clear();
            }
     });

    hb.getChildren().addAll(addSubName, b2);
    hb.setSpacing(5);

    vb1.getChildren().addAll(label, table, hb);
    return vb1;

}

解决方案

The other way can be using bindings:

final TextField textField1 = new TextField();
final TextField textField2 = new TextField();
final TextField textField3 = new TextField();

BooleanBinding bb = new BooleanBinding() {
    {
        super.bind(textField1.textProperty(),
                textField2.textProperty(),
                textField3.textProperty());
    }

    @Override
    protected boolean computeValue() {
        return (textField1.getText().isEmpty()
                && textField2.getText().isEmpty()
                && textField3.getText().isEmpty());
    }
};

Button btn = new Button("Button");
btn.disableProperty().bind(bb);

VBox vBox = new VBox();
vBox.getChildren().addAll(textField1, textField2, textField3, btn);

这篇关于如何禁用按钮时,文本字段为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆