1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
package fxintoswing;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
public final class pieChart
{
public JFXPanel fxPanel;
public pieChart()
{
initAndShowGUI();
}
public void initAndShowGUI()
{
// This method is invoked on Swing thread
fxPanel = new JFXPanel();
Platform.runLater(new Runnable()
{
@Override
public void run()
{
initFX(fxPanel);
}
});
}
private static void initFX(JFXPanel fxPanel)
{
// This method is invoked on JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene()
{
Group root = new Group();
Scene scene = new Scene(root, Color.CORAL);
ObservableList<PieChart.Data> pieChartData
= FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", 13),
new PieChart.Data("Oranges", 25),
new PieChart.Data("Plums", 10),
new PieChart.Data("Pears", 22),
new PieChart.Data("Apples", 30));
final PieChart chart = new PieChart(pieChartData);
chart.setPrefWidth(750);
chart.setPrefHeight(550);
chart.setTitle("Imported Fruits (chart)");
final Label caption = new Label("");
caption.setTextFill(Color.WHITE);
caption.setStyle("-fx-font: 20 arial;");
chart.getData().stream().forEach((data) -> {
data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
(MouseEvent e) -> {
caption.setTranslateX(e.getSceneX());
caption.setTranslateY(e.getSceneY());
caption.setText(String.valueOf(data.getPieValue()) + "%");
});
});
root.getChildren().addAll(chart, caption);
return (scene);
}
}
} |
Partager