gpt4 book ai didi

javafx-8 - 有没有办法在 JavaFX 中绑定(bind) ListProperty 的内容?

转载 作者:行者123 更新时间:2023-12-02 07:26:17 25 4
gpt4 key购买 nike

是否存在绑定(bind) ListProperty 内容的现有方法?考虑以下几点:

private final ListProperty<Worker<?>> workers = new SimpleListProperty<>(FXCollections.observableArrayList());
public ListProperty<Worker<?>> workersProperty() {return workers;}
public ObservableList<Worker<?>> getWorkers() {return workers.get();}
public void setWorkers(ObservableList<Worker<?>> workers) {this.workers.set(workers);}

private final ObservableList<Worker<?>> stateWatchedWorkers = FXCollections.observableArrayList(
new Callback<Worker<?>, Observable[]>() {
@Override
public Observable[] call(final Worker<?> param) {
return new Observable[]{
param.stateProperty()
};
}
}
);

我要做的是将 stateWatchedWorkers 列表的内容绑定(bind)到工作人员。如果整个 workers 列表被替换,我希望 stateWatchedWorkers 被更新以匹配新列表的内容。换句话说,我希望 stateWatchedWorkers 列表映射到 workers 属性所持有的当前列表。

最佳答案

你可以的

Bindings.bindContent(stateWatchedWorkers, workers);

(因为 ListProperty<T> 实现了 ObservableList<T> ,通过将 ObservableList 方法委托(delegate)给包装列表等)。

这是一个完整的例子:

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ListContentBindingExample extends Application {

private static final Random RNG = new Random();
private static final ExecutorService exec = Executors.newFixedThreadPool(5, r -> {
Thread t = new Thread(r);
t.setDaemon(true);
return t ;
});

@Override
public void start(Stage primaryStage) {
final ListProperty<Worker<?>> workers = new SimpleListProperty<>(FXCollections.observableArrayList());

final ObservableList<Worker<?>> stateWatchedWorkers = FXCollections.observableArrayList(worker -> new Observable[] { worker.stateProperty() });
Bindings.bindContent(stateWatchedWorkers, workers);

Button newWorker = new Button("New Worker") ;
newWorker.setOnAction(event -> workers.add(createTask()));

Button replaceAllWorkers = new Button("New Worker List");
replaceAllWorkers.setOnAction(event -> {
ObservableList<Worker<?>> newWorkers = FXCollections.observableArrayList();
for (int i=0; i<=10; i++) {
newWorkers.add(createTask());
}
workers.set(newWorkers);
});

ListView<Worker<?>> workerView = new ListView<>();
workerView.setCellFactory(listView -> new ListCell<Worker<?>>() {
@Override
public void updateItem(Worker<?> worker, boolean empty) {
super.updateItem(worker, empty);
if (empty) {
setText(null);
} else {
setText(worker.getState().toString());
}
}
});
workerView.setItems(stateWatchedWorkers);

workers.get().addListener((Change<? extends Worker<?>> change) -> stateWatchedWorkers.forEach(w -> System.out.println(w.getState())));
stateWatchedWorkers.addListener((Change<? extends Worker<?>> change) -> stateWatchedWorkers.forEach(w -> System.out.println(w.getState())));

HBox buttons = new HBox(5, newWorker, replaceAllWorkers);
buttons.setAlignment(Pos.CENTER);
buttons.setPadding(new Insets(10));

primaryStage.setScene(new Scene(new BorderPane(workerView, null, null, buttons, null), 250, 400));
primaryStage.show();
}

private Worker<Void> createTask() {
Service<Void> service = new Service<Void>() {

@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
public Void call() throws Exception {
Thread.sleep(1000 + RNG.nextInt(2000));
return null ;
}
};
}

};

service.setExecutor(exec);
service.start();
return service ;
}

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

关于javafx-8 - 有没有办法在 JavaFX 中绑定(bind) ListProperty 的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28285507/

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