How to get an empty Java property of BlockingQueue to bind it to some property of a JavaFX element?

I have BlockingQueue(in particular a LinkedBlockingQueue) and would like to get the empty property (boolean) of this collection in order to be able to bind it to the disabled JavaFX button value.

All I could find is emptyProperty()in ListBinding, but I'm not sure how to proceed beyond this point.

+4
source share
1 answer

Using . disableProperty Button

, BlockingQueue, . James_D , :

ObservableQueue<String> queue = new ObservableQueue<>(new LinkedBlockingQueue<>());

:

Button button = new Button();
button.disableProperty().bind(Bindings.createBooleanBinding(queue::isEmpty, queue));

JavaFX. : , , disable : , , .

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

}
+3

Source: https://habr.com/ru/post/1625032/


All Articles