Created
March 25, 2014 14:44
-
-
Save james-d/9763272 to your computer and use it in GitHub Desktop.
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.Random; | |
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.ThreadFactory; | |
| import javafx.application.Application; | |
| import javafx.beans.property.FloatProperty; | |
| import javafx.beans.property.SimpleFloatProperty; | |
| import javafx.beans.property.SimpleStringProperty; | |
| import javafx.beans.property.StringProperty; | |
| import javafx.concurrent.Task; | |
| import javafx.concurrent.WorkerStateEvent; | |
| import javafx.event.ActionEvent; | |
| import javafx.event.EventHandler; | |
| import javafx.geometry.Insets; | |
| import javafx.geometry.Pos; | |
| import javafx.scene.Scene; | |
| import javafx.scene.control.Button; | |
| import javafx.scene.control.TableColumn; | |
| import javafx.scene.control.TableView; | |
| import javafx.scene.control.cell.PropertyValueFactory; | |
| import javafx.scene.layout.BorderPane; | |
| import javafx.scene.layout.HBox; | |
| import javafx.stage.Stage; | |
| public class StockPriceTable extends Application { | |
| private final Random rng = new Random(); | |
| @Override | |
| public void start(Stage primaryStage) { | |
| final TableView<StockPrice> priceTable = new TableView<>(); | |
| final TableColumn<StockPrice, String> codeCol = new TableColumn<>("Code"); | |
| codeCol.setCellValueFactory(new PropertyValueFactory<>("code")); | |
| final TableColumn<StockPrice, Float> priceCol = new TableColumn<>("Price"); | |
| priceCol.setCellValueFactory(new PropertyValueFactory<>("price")); | |
| priceTable.getColumns().addAll(codeCol, priceCol); | |
| for (int i=0; i<20; i++) { | |
| priceTable.getItems().add(randomStock()); | |
| } | |
| final BorderPane root = new BorderPane(); | |
| root.setCenter(priceTable); | |
| final ExecutorService exec = Executors.newFixedThreadPool(4, new ThreadFactory() { | |
| @Override | |
| public Thread newThread(Runnable r) { | |
| Thread t = new Thread(r); | |
| t.setDaemon(true); | |
| return t ; | |
| } | |
| }); | |
| final Button updateButton = new Button("Refresh"); | |
| updateButton.setOnAction(new EventHandler<ActionEvent>() { | |
| @Override | |
| public void handle(ActionEvent event) { | |
| for (final StockPrice sp : priceTable.getItems()) { | |
| final float currentPrice = sp.getPrice(); | |
| final Task<Float> task = new Task<Float>() { | |
| @Override | |
| public Float call() throws Exception { | |
| Thread.sleep(rng.nextInt(500)); | |
| return currentPrice * (0.9f + rng.nextFloat() * 0.2f); | |
| } | |
| }; | |
| task.setOnSucceeded(new EventHandler<WorkerStateEvent>() { | |
| @Override | |
| public void handle(WorkerStateEvent event) { | |
| sp.setPrice(task.getValue()); | |
| } | |
| }); | |
| exec.execute(task); | |
| } | |
| } | |
| }); | |
| final HBox controls = new HBox(5); | |
| controls.setPadding(new Insets(10)); | |
| controls.setAlignment(Pos.CENTER); | |
| controls.getChildren().add(updateButton); | |
| root.setBottom(controls); | |
| final Scene scene = new Scene(root, 400, 600); | |
| primaryStage.setScene(scene); | |
| primaryStage.show(); | |
| } | |
| private StockPrice randomStock() { | |
| String code = "" ; | |
| for (int i=0; i<4; i++) { | |
| code = code + (char)('A' + rng.nextInt(25)); | |
| } | |
| float price = rng.nextFloat() * 200 ; | |
| return new StockPrice(code, price); | |
| } | |
| public final static class StockPrice { | |
| private final StringProperty code ; | |
| private final FloatProperty price ; | |
| public StockPrice(String code, float price) { | |
| this.code = new SimpleStringProperty(this, "code", code); | |
| this.price = new SimpleFloatProperty(this, "price", price); | |
| } | |
| public float getPrice() { | |
| return price.get(); | |
| } | |
| public void setPrice(float price) { | |
| this.price.set(price); | |
| } | |
| public FloatProperty priceProperty() { | |
| return price ; | |
| } | |
| public String getCode() { | |
| return code.get() ; | |
| } | |
| public void setCode(String code) { | |
| this.code.set(code); | |
| } | |
| public StringProperty codeProperty() { | |
| return code ; | |
| } | |
| } | |
| 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