See the following code snippet. I have added full package names for all classes other than javafx to avoid import clutter.
public void start(final Stage primaryStage) throws Exception { VBox root = new VBox(); final Scene scene; primaryStage.setScene(scene = new Scene(root)); NumberAxis xAxis = new NumberAxis("X-Axis", 0d, 8.0d, 1.0d); NumberAxis yAxis = new NumberAxis("Y-Axis", 0.0d, 5.0d, 1.0d); ObservableList<XYChart.Series> data = FXCollections.observableArrayList( new ScatterChart.Series("Series 1", FXCollections.<ScatterChart.Data>observableArrayList( new XYChart.Data(0.2, 3.5), new XYChart.Data(0.7, 4.6), new XYChart.Data(7.8, 4.0)))); final ScatterChart chart = new ScatterChart(xAxis, yAxis, data); Button btnShoot = new Button("screenshot"); btnShoot.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent t) { try {
NB: this approach involves using AWT side by side with JavaFX, which is usually not a good idea and may not work in all configurations. It is better to use GlassRobot instead of AWTRobot . Unfortunately, while it is not stable enough.
Sergey Grinev
source share