import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Side; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.PieChart; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.animation.Timeline; import javafx.event.ActionEvent; import javafx.event.EventHandler; public class CustomLegend1 extends Application { private void init(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root); primaryStage.setScene(scene); ObservableList pieChartData = FXCollections.observableArrayList(new PieChart.Data("Sun", 20), new PieChart.Data("IBM", 12), new PieChart.Data("HP", 25), new PieChart.Data("Dell", 22), new PieChart.Data("Apple", 30)); final Button customLegend = new Button("setLabelsVisible"); final PieChart chart = new PieChart(pieChartData) { { setLegend(customLegend); } }; customLegend.setOnAction(new EventHandler() { @Override public void handle(ActionEvent arg0) { System.out.println("Change visible"); chart.setLabelsVisible(!chart.getLabelsVisible()); } }); chart.setClockwise(false); chart.setTitle("Title"); chart.setLegendSide(Side.RIGHT); VBox vBox = new VBox(); vBox.getChildren().addAll(chart, customLegend); root.getChildren().add(vBox); } @Override public void start(Stage primaryStage) throws Exception { init(primaryStage); primaryStage.show(); } public static void main(String[] args) { launch(args); } }