[英]Disable Button when TableView is empty
菜鳥再次需要幫助。 :)
我有一個名為 tblTabela 的 TableView 和一個名為 btnIzracunaj 的按鈕。 我需要的是將 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);
提前致謝。
將按鈕的 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);
}
}
button.disableProperty().bind(Bindings.isEmpty(list));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.