package javaapplication10; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Orientation; import javafx.scene.Scene; import javafx.scene.chart.PieChart; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Separator; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class JavaApplication10 extends Application { public static void main(String[] args) { launch(JavaApplication10.class, args); } protected Scene getScene() { return new PieChartScene(); } @Override public void start(Stage stage) throws Exception { stage.setScene(getScene()); stage.show(); } class PieChartScene extends Scene { //PieChart to be tested. PieChart testedPieChart; int controlContainerWidth = 400; int controlContainerHeight = 400; ObservableList data; VBox properties; public PieChartScene() { super(new VBox(), 600, 600); prepareScene(); } final protected void prepareScene() { testedPieChart = getNewChart(); testedPieChart.setPrefSize(controlContainerHeight, controlContainerWidth); properties = new VBox(10); Button addDataButton = new Button("Add data at index 0"); addDataButton.setOnAction(new EventHandler() { public void handle(ActionEvent t) { testedPieChart.getData().add(0, new PieChart.Data("Added data", Math.random() * 100)); } }); Button removeDataButton = new Button("Remove data from index 0"); removeDataButton.setOnAction(new EventHandler() { public void handle(ActionEvent t) { if (testedPieChart.getData().size() > 0) { testedPieChart.getData().remove(0); } } }); Button changeDataButton = new Button("Change data at index 0"); changeDataButton.setOnAction(new EventHandler() { public void handle(ActionEvent t) { if (testedPieChart.getData().size() > 0) { PieChart.Data data = testedPieChart.getData().get(0); data.setPieValue(Math.max(0, data.getPieValue() + (Math.random() - 0.5) * 200)); } } }); VBox vb = (VBox) this.getRoot(); vb.getChildren().addAll(testedPieChart, new Separator(Orientation.HORIZONTAL), new Label("Add new data : "), addDataButton, new Separator(Orientation.HORIZONTAL), new Label("Remove existing data : "), removeDataButton, new Separator(Orientation.HORIZONTAL), new Label("Change existing data : "), changeDataButton); } public PieChart getNewChart() { data = FXCollections.observableArrayList(); for (int i = 0; i < 4; i++) { data.add(new PieChart.Data("Data item " + i, 50)); } PieChart chart = new PieChart(data); chart.setTitle("PieChart"); chart.setStyle("-fx-border-color: darkgray;"); return chart; } } }