Created
March 28, 2015 16:41
-
-
Save orekyuu/8ae8ba48501d141017cf to your computer and use it in GitHub Desktop.
カスタムListCellでマルチスレッドを扱うサンプル
This file contains 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
package sample; | |
import javafx.concurrent.Service; | |
import javafx.concurrent.Task; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.ListCell; | |
import javafx.scene.control.ListView; | |
import javafx.scene.control.ProgressBar; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
public ListView<Status> listView; | |
@Override | |
public void initialize(URL location, ResourceBundle resources) { | |
listView.setCellFactory(param -> new ListCell<Status>() { | |
@Override | |
protected void updateItem(Status item, boolean empty) { | |
super.updateItem(item, empty); | |
ProgressBar root = (ProgressBar) getGraphic(); | |
if (item == null) { | |
if (root != null) { | |
ProgressBar progressBar = (ProgressBar) getGraphic(); | |
progressBar.progressProperty().unbind(); | |
setGraphic(null); | |
} | |
return; | |
} | |
if (root == null) { | |
root = new ProgressBar(); | |
setGraphic(root); | |
} | |
root.progressProperty().bind(item.statusProperty()); | |
} | |
}); | |
} | |
public void addStatus() { | |
StatusService service = new StatusService(); | |
Status status = new Status(); | |
status.statusProperty().bind(service.progressProperty()); | |
listView.getItems().add(status); | |
service.start(); | |
service.setOnSucceeded(e -> listView.getItems().remove(status)); | |
} | |
private static class StatusService extends Service<String> { | |
@Override | |
protected Task<String> createTask() { | |
return new Task<String>() { | |
@Override | |
protected String call() throws InterruptedException { | |
for (int i = 0; i <= 100; i++) { | |
Thread.sleep(100); | |
updateProgress(i, 100); | |
} | |
return "A"; | |
} | |
}; | |
} | |
} | |
} |
This file contains 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
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("ListView Task Sample"); | |
primaryStage.setScene(new Scene(root, 300, 275)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.*?> | |
<?import java.lang.*?> | |
<?import javafx.scene.layout.*?> | |
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<children> | |
<ListView fx:id="listView" VBox.vgrow="ALWAYS" /> | |
<Button mnemonicParsing="false" text="追加" onAction="#addStatus"/> | |
</children> | |
</VBox> |
This file contains 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
package sample; | |
import javafx.beans.property.DoubleProperty; | |
import javafx.beans.property.SimpleDoubleProperty; | |
public class Status { | |
private DoubleProperty status = new SimpleDoubleProperty(); | |
public Status(double start) { | |
status.set(start); | |
} | |
public Status() { | |
} | |
public double getStatus() { | |
return status.get(); | |
} | |
public DoubleProperty statusProperty() { | |
return status; | |
} | |
public void setStatus(double status) { | |
this.status.set(status); | |
} | |
@Override | |
public String toString() { | |
final StringBuilder sb = new StringBuilder("Status{"); | |
sb.append("status=").append(status); | |
sb.append('}'); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment