Created
August 30, 2013 16:07
-
-
Save skrb/6391436 to your computer and use it in GitHub Desktop.
Exaple of ListCell and async task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import javafx.application.Application; | |
| import javafx.application.Platform; | |
| import javafx.beans.property.DoubleProperty; | |
| import javafx.beans.property.SimpleDoubleProperty; | |
| import javafx.collections.FXCollections; | |
| import javafx.collections.ObservableList; | |
| import javafx.concurrent.Task; | |
| import javafx.event.EventHandler; | |
| import javafx.scene.Scene; | |
| import javafx.scene.control.ListCell; | |
| import javafx.scene.control.ListView; | |
| import javafx.scene.control.ProgressBar; | |
| import javafx.scene.layout.StackPane; | |
| import javafx.stage.Stage; | |
| import javafx.stage.WindowEvent; | |
| import javafx.util.Callback; | |
| // ProgressBarを表示するListCell | |
| class PBListCell extends ListCell<DoubleProperty> { | |
| private ProgressBar bar; | |
| @Override | |
| public void updateItem(DoubleProperty item, boolean empty) { | |
| super.updateItem(item, empty); | |
| if (isEmpty()) { | |
| setText(null); | |
| setGraphic(null); | |
| } else { | |
| if (bar == null) { | |
| bar = new ProgressBar(); | |
| setGraphic(bar); | |
| // itemとプログレスをバインドさせて | |
| // itemが変化したらプログレスを更新する | |
| bar.progressProperty().bind(item); | |
| // バインドを使わない場合は、変化の変化のイベントを使用します | |
| // item.addListener(new InvalidationListener() { | |
| // @Override | |
| // public void invalidated(Observable item) { | |
| // bar.setProgress(((DoubleProperty)item).get()); | |
| // } | |
| // }); | |
| } | |
| } | |
| } | |
| } | |
| // 非同期に行うタスク | |
| class BGTask extends Task<Void> { | |
| public DoubleProperty value; | |
| public BGTask(DoubleProperty value) { | |
| this.value = value; | |
| } | |
| @Override | |
| protected Void call() throws Exception { | |
| try { | |
| while(true) { | |
| Thread.sleep(100); | |
| Platform.runLater(new Runnable() { | |
| @Override | |
| public void run() { | |
| double v = value.get() + 0.01; | |
| if (v >= 1.0) { | |
| v = 1.0; | |
| } | |
| value.set(v); | |
| } | |
| }); | |
| } | |
| } catch(InterruptedException ex) {} | |
| return null; | |
| } | |
| } | |
| public class Test extends Application { | |
| ExecutorService service = Executors.newFixedThreadPool(5); | |
| @Override | |
| public void start(Stage stage) { | |
| ObservableList<DoubleProperty> items = FXCollections.observableArrayList( | |
| new SimpleDoubleProperty(0.0), | |
| new SimpleDoubleProperty(0.1), | |
| new SimpleDoubleProperty(0.2), | |
| new SimpleDoubleProperty(0.3), | |
| new SimpleDoubleProperty(0.4) | |
| ); | |
| for (DoubleProperty item: items) { | |
| BGTask task = new BGTask(item); | |
| service.submit(task); | |
| } | |
| StackPane root = new StackPane(); | |
| ListView<DoubleProperty> list = new ListView<>(items); | |
| list.setEditable(true); | |
| list.setCellFactory(new Callback<ListView<DoubleProperty>, | |
| ListCell<DoubleProperty>>() { | |
| @Override | |
| public ListCell<DoubleProperty> call(ListView<DoubleProperty> list) { | |
| return new PBListCell(); | |
| } | |
| } | |
| ); | |
| root.getChildren().add(list); | |
| Scene scene = new Scene(root, 200, 200); | |
| stage.setScene(scene); | |
| stage.show(); | |
| stage.setOnCloseRequest(new EventHandler<WindowEvent>() { | |
| @Override | |
| public void handle(WindowEvent t) { | |
| service.shutdownNow(); | |
| } | |
| }); | |
| } | |
| public static void main(String... args) { | |
| launch(args); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment