gpt4 book ai didi

javafx - 如何将列表的大小属性绑定(bind)到节点的禁用属性?

转载 作者:行者123 更新时间:2023-12-03 20:16:42 26 4
gpt4 key购买 nike

假设我有一个 ObservableListButton在同一个 Controller 类中:

private ObservableList<NameItem> _selectedList = _database.getONameList();

@FXML
private Button nextButton;
我怎样才能使 Button仅在 ObservableList 时启用是不是空的?我可以使用绑定(bind)属性来设置它吗?

最佳答案

实际上,这只需几个简单的绑定(bind)就可以很容易地完成。

首先,您要创建一个 IntegerBinding这与您的 ObservableList 的大小有关:

IntegerBinding listSize = Bindings.size(_selectedList);

然后新建一个 BooleanBinding这与 listSize 是否绑定(bind)有关绑定(bind)大于0:
BooleanBinding listPopulated = listSize.greaterThan(0);

现在,您需要做的就是绑定(bind)按钮的 disablePropertylistPopulated 的对面属性使用 not()方法(因为 listPopulated 将是 true 如果项目在列表中,您希望实际将 false 传递给按钮的 disableProperty ):
button.disableProperty().bind(listPopulated.not());

这是一个快速的 MCVE 演示:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.IntegerBinding;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

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

@Override
public void start(Stage primaryStage) {

// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

// Create an empty ObservableList
ObservableList<String> list = FXCollections.observableArrayList();

// Create a binding to extract the list's size to a property
IntegerBinding listSizeProperty = Bindings.size(list);

// Create a boolean binding that will return true only if the list has 1 or more elements
BooleanBinding listPopulatedProperty = listSizeProperty.greaterThan(0);

// Create the button we want to enable/disable
Button button = new Button("Submit");

// Bind the button's disableProperty to the opposite of the listPopulateProperty
// If listPopulateProperty is false, the button will be disabled, and vice versa
button.disableProperty().bind(listPopulatedProperty.not());

// Create another button to add an item to the list, just to demonstrate the concept
Button btnAddItem = new Button("Add Item");
btnAddItem.setOnAction(event -> {
list.add("New Item");
System.out.println(list.size());
});

// Add the buttons to the layout
root.getChildren().addAll(btnAddItem, button);

// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

在上面的示例中,“提交”按钮被禁用,直到您将项目添加到 ObservableList使用“添加项目”按钮。

编辑:正如 Lukas 在下面的评论中出色地指出的那样,这些 Bindings 也可以全部链接在一起以简化事情(任何一种方法都同样有效;它只是取决于你觉得哪种更易读,真的):
button.disableProperty().bind(Bindings.size(list).greaterThan(0).not())

Another Method



另一种方法是使用 ListChangeListener在列表更改时启用或禁用按钮:
    list.addListener((ListChangeListener<String>) c -> {
// If the size of the list is less than 1, disable the button; otherwise enable it
button.setDisable(c.getList().size() < 1);
});

这基本上与第一种方法完全相同,但您需要自己设置按钮的初始状态,然后监听器才能为您保持更新。

关于javafx - 如何将列表的大小属性绑定(bind)到节点的禁用属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52461801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com