当TableView为空时禁用Button

时间:2016-02-04 11:17:30

标签: java button javafx tableview bind

noob再次需要帮助。 :)

我有一个名为tblTabela的TableView和一个名为btnIzracunaj的Button。我需要的是将Button禁用属性与TableView绑定,这样当TableView没有内容时Button被禁用。

当TextFields为空时,我与另一个Button进行了类似的绑定:How to disable Button when TextField is empty?

    BooleanBinding bb = new BooleanBinding() {
{
    super.bind(txtPovrsina.textProperty(),
            txtPrvi.textProperty(),
            txtDrugi.textProperty());
}

@Override
protected boolean computeValue() {
    return (txtPovrsina.getText().isEmpty()
            || txtPrvi.getText().isEmpty()
            || txtDrugi.getText().isEmpty());
}

};
btnDodaj.disableProperty().bind(bb);

但我的问题是使用TableView,我不知道如何设置绑定属性。应该使用TableView的哪些属性? 我试过这个,它没有返回错误,但也没有按预期工作。我相信getItems()应该有别的东西,但无法弄清楚是什么。 :(

    BooleanBinding ee = new BooleanBinding() {
{
    super.bind(tblTabela.getItems());
}

@Override
protected boolean computeValue() {
    return (tblTabela.getItems().isEmpty());
}

};
btnIzracunaj.disableProperty().bind(ee);

提前致谢。

2 个答案:

答案 0 :(得分:5)

将按钮的disabled属性绑定到可观察列表,如下所示:

button.disableProperty().bind(Bindings.size(list).isEqualTo(0));

示例代码:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        ObservableList<String> list = FXCollections.observableArrayList();

        HBox root = new HBox();

        // add button
        Button addButton = new Button("Add");
        addButton.setOnAction(e -> {

            list.add("Text");

            System.out.println("Size: " + list.size());

        });

        // remove button
        Button removeButton = new Button("Remove");
        removeButton.setOnAction(e -> {

            if (list.size() > 0) {
                list.remove(0);
            }

            System.out.println("Size: " + list.size());

        });

        root.getChildren().addAll(addButton, removeButton);

        // bind to remove button
        removeButton.disableProperty().bind(Bindings.size(list).isEqualTo(0));

        Scene scene = new Scene(root, 800, 600);

        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

答案 1 :(得分:2)

curl --location --request GET 'https://xxx.xxx.xxx.xx/cwa/api/v1/Computers/' --header 'Authorization: Bearer 1<token>'
相关问题