Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions xchart-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<properties>
<!-- skip test compilation during release:prepare's clean verify subprocess -->
<maven.test.skip>true</maven.test.skip>
<javafx.version>17.0.20</javafx.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -49,15 +50,17 @@
<artifactId>webp-imageio</artifactId>
<version>0.11.0</version>
</dependency>
<!-- JavaFX 17 is the newest LTS line that still runs on Java 11 (the minimum JDK only
rose to 17 with JavaFX 21) and the first with Apple Silicon (mac-aarch64) natives. -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>11.0.2</version>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
<version>${javafx.version}</version>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.demo.charts.area.AreaChart01;

/** Class showing how to integrate a chart into a JavaFX Stage */
/**
* Class showing how to integrate a chart into a JavaFX Stage, embedded in a layout container
* (VBox) alongside other controls so that everything resizes with the window.
*/
public class JavaFXDemo extends Application {

public static void main(String[] args) {
Expand All @@ -21,10 +29,24 @@ public static void main(String[] args) {
public void start(Stage stage) {

final SwingNode swingNode = new SwingNode();
JPanel chartPanel = new XChartPanel<>(new AreaChart01().getChart());
swingNode.setContent(chartPanel);
Scene scene = new Scene(new StackPane(swingNode), 640, 480);
stage.setScene(scene);
// Swing content must be created on the Swing Event Dispatch Thread
SwingUtilities.invokeLater(
() -> swingNode.setContent(new XChartPanel<>(new AreaChart01().getChart())));

// A VBox gives each child its preferred height only; wrap the SwingNode in a resizable pane
// and mark it to grow so the chart fills the space left over by the other controls.
StackPane chartHolder = new StackPane(swingNode);
VBox.setVgrow(chartHolder, Priority.ALWAYS);

Button closeButton = new Button("Close");
closeButton.setOnAction(event -> stage.close());
ButtonBar buttonBar = new ButtonBar();
buttonBar.getButtons().add(closeButton);

VBox root = new VBox(10, chartHolder, buttonBar);
root.setPadding(new Insets(10));

stage.setScene(new Scene(root, 640, 480));
stage.show();
}
}
Loading