Last active
August 29, 2015 13:58
-
-
Save james-d/9956865 to your computer and use it in GitHub Desktop.
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
import java.util.List; | |
import java.util.Random; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import javafx.application.Application; | |
import javafx.beans.Observable; | |
import javafx.beans.property.DoubleProperty; | |
import javafx.beans.property.SimpleDoubleProperty; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.beans.property.StringProperty; | |
import javafx.collections.FXCollections; | |
import javafx.geometry.Insets; | |
import javafx.geometry.Pos; | |
import javafx.stage.Stage; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.ListView; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.HBox; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
try { | |
BorderPane root = new BorderPane(); | |
ListView<Item> list = new ListView<>(); | |
final Random rng = new Random(); | |
// twenty items with random prices | |
final List<Item> items = IntStream.range(1, 20) | |
.mapToObj(i -> new Item("Item " + i, rng.nextDouble() * 100)) | |
.collect(Collectors.toList()); | |
list.setItems(FXCollections.observableList(items, | |
item -> new Observable[] { item.nameProperty(), // This is the extractor: | |
item.priceProperty() })); // list will fire updates if the name or price change | |
Button button = new Button("Increase prices"); | |
button.setOnAction(event -> list.getItems().forEach( // Add 10% to | |
item -> item.setPrice(item.getPrice() * 1.1))); // all prices | |
HBox controls = new HBox(button); | |
controls.setPadding(new Insets(10)); | |
controls.setAlignment(Pos.CENTER); | |
root.setCenter(list); | |
root.setBottom(controls); | |
Scene scene = new Scene(root, 400, 400); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static class Item { | |
private final StringProperty name = new SimpleStringProperty(this, "name"); | |
public final String getName() { | |
return name.get(); | |
} | |
public final void setName(String name) { | |
this.name.set(name); | |
} | |
public final StringProperty nameProperty() { | |
return name; | |
} | |
private final DoubleProperty price = new SimpleDoubleProperty(this, "price"); | |
public final double getPrice() { | |
return price.get(); | |
} | |
public final void setPrice(double price) { | |
this.price.set(price); | |
} | |
public final DoubleProperty priceProperty() { | |
return price; | |
} | |
public Item(String name, double price) { | |
this.setName(name); | |
this.setPrice(price); | |
} | |
@Override | |
public String toString() { | |
return String.format("%s $%.2f", name.get(), price.get()); | |
} | |
} | |
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