ComboBox的TextField.textProperty()相当于什么?

时间:2014-04-16 15:27:11

标签: combobox javafx javafx-2 textfield

我的代码中有一个TextField和一个ComboBox。当两个控件都有值时,我需要使一个按钮可用。我的代码是 -

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

    comboBox1 = new ComboBox();
    comboBox1.setPromptText("Choose Subject");
    comboBox1.setPrefSize(280, 30);

    BooleanBinding bb = new BooleanBinding() {
    {
        super.bind(addSubName.textProperty());
    }

    @Override
    protected boolean computeValue() {
      return (addSubName.getText().isEmpty());
      }
    };

    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.disableProperty().bind(bb);

如您所见,我知道如何检查TextField是否为空,以禁用该按钮。我也需要检查ComboBox。那么ComboBox的“textProperty()”和“getText()。isEmpty()”等价是什么?

1 个答案:

答案 0 :(得分:2)

ComboBoxvalueProperty

您可以在此处使用Bindings API:

b2.disableProperty().bind(bb.or(Bindings.isNull(comboBox1.valueProperty())));

(在JavaFX 8中,您也可以使用Bindings API来处理文本:

BooleanBinding bb = Bindings.isEmpty(addSubName.textProperty());