如何获取 Java BlockingQueue 的空 属性,以便将其绑定到 JavaFX 元素的某些 属性?
How do I get the empty property of a Java BlockingQueue, in order to bind it to some property of a JavaFX element?
我有一个 BlockingQueue
(特别是 LinkedBlockingQueue
)并想获得这个集合的空 属性(布尔值),以便能够将它绑定到一个JavaFX 禁用了 属性 按钮。
我能找到的只是 ListBinding
中的 emptyProperty()
,但我不确定如何继续进行下去。
您可以为其创建 BooleanBinding
with the help of the Bindings
utility class and bind the disableProperty
of a Button
。
但首先,问题是您想要一个 BlockingQueue
而没有 built-in 可观察队列。使用 James_D in 中的代码,我们可以有一个可观察的队列:
ObservableQueue<String> queue = new ObservableQueue<>(new LinkedBlockingQueue<>());
然后像这样使用它:
Button button = new Button();
button.disableProperty().bind(Bindings.createBooleanBinding(queue::isEmpty, queue));
您将在下面找到示例 JavaFX 应用程序。这是一个非常简单的应用程序,有 3 个按钮:一个用于向队列添加一个元素,一个用于删除一个元素,第三个按钮将根据队列是否为空更改其禁用 属性:如果队列为空, 按钮将被禁用,否则将被启用。
public class Main extends Application {
private ObservableQueue<String> queue = new ObservableQueue<>(new LinkedBlockingQueue<>());
@Override
public void start(Stage primaryStage) {
Button btn = new Button("Add to queue");
btn.setOnAction(event -> queue.add("value"));
Button btn2 = new Button("Remove to queue");
btn2.setOnAction(event -> queue.remove());
Button btn3 = new Button("Button");
btn3.disableProperty().bind(Bindings.createBooleanBinding(queue::isEmpty, queue));
FlowPane root = new FlowPane();
root.getChildren().addAll(btn, btn2, btn3);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我有一个 BlockingQueue
(特别是 LinkedBlockingQueue
)并想获得这个集合的空 属性(布尔值),以便能够将它绑定到一个JavaFX 禁用了 属性 按钮。
我能找到的只是 ListBinding
中的 emptyProperty()
,但我不确定如何继续进行下去。
您可以为其创建 BooleanBinding
with the help of the Bindings
utility class and bind the disableProperty
of a Button
。
但首先,问题是您想要一个 BlockingQueue
而没有 built-in 可观察队列。使用 James_D in
ObservableQueue<String> queue = new ObservableQueue<>(new LinkedBlockingQueue<>());
然后像这样使用它:
Button button = new Button();
button.disableProperty().bind(Bindings.createBooleanBinding(queue::isEmpty, queue));
您将在下面找到示例 JavaFX 应用程序。这是一个非常简单的应用程序,有 3 个按钮:一个用于向队列添加一个元素,一个用于删除一个元素,第三个按钮将根据队列是否为空更改其禁用 属性:如果队列为空, 按钮将被禁用,否则将被启用。
public class Main extends Application {
private ObservableQueue<String> queue = new ObservableQueue<>(new LinkedBlockingQueue<>());
@Override
public void start(Stage primaryStage) {
Button btn = new Button("Add to queue");
btn.setOnAction(event -> queue.add("value"));
Button btn2 = new Button("Remove to queue");
btn2.setOnAction(event -> queue.remove());
Button btn3 = new Button("Button");
btn3.disableProperty().bind(Bindings.createBooleanBinding(queue::isEmpty, queue));
FlowPane root = new FlowPane();
root.getChildren().addAll(btn, btn2, btn3);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}