سؤال

I have a TextField and a ComboBox in my code. I need to make a button workable when both controls have values in it. My code is -

    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);

As you have seen, I know how to check whether the TextField is empty or not, to make the button disabled. I need to check the ComboBox too. So what is the equivalent to "textProperty()" and "getText().isEmpty()" for a ComboBox ?

هل كانت مفيدة؟

المحلول

ComboBox has a valueProperty.

You could use the Bindings API here:

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

(In JavaFX 8 you can use the Bindings API for the text too:

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

)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top