Skip to content

Commit

Permalink
test(SwingAppWithJavaFX, SwingFXWebView): JFXPanel
Browse files Browse the repository at this point in the history
"JFXPanel is a component to embed JavaFX content into Swing applications. The content to be displayed is specified with the setScene(javafx.scene.Scene) method that accepts an instance of JavaFX Scene. After the scene is assigned, it gets repainted automatically. All the input and focus events are forwarded to the scene transparently to the developer."

See https://docs.oracle.com/javase/8/javafx/api/javafx/embed/swing/JFXPanel.html
  • Loading branch information
RalfBarkow committed Jun 6, 2024
1 parent 20363c0 commit dccb503
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@
<configuration>
<toolchains>
<jdk>
<version>[8, 11, 17, 21]</version>
<vendor>temurin</vendor>
</jdk>
<jdk>
<version>18.0.2+101</version>
<version>11</version>
<vendor>temurin</vendor>
</jdk>
</toolchains>
Expand Down Expand Up @@ -215,6 +211,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down Expand Up @@ -373,6 +377,12 @@
<version>11</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>16-ea+7</version>
<type>jar</type>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/playground/swing/SwingAppWithJavaFX.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package playground.swing;

import javax.swing.*;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;

public class SwingAppWithJavaFX {
public static void main(String[] args) {
// Create the main Swing frame
JFrame frame = new JFrame("Swing and JavaFX Application");
JPanel panel = new JPanel();
JButton swingButton = new JButton("Swing Button");

swingButton.addActionListener(e -> System.out.println("Swing Button Clicked"));

panel.add(swingButton);

// Create and add JFXPanel
JFXPanel jfxPanel = new JFXPanel();
panel.add(jfxPanel);

// Load JavaFX content into JFXPanel
Platform.runLater(() -> {
Button fxButton = new Button("JavaFX Button");
fxButton.setOnAction(e -> System.out.println("JavaFX Button Clicked"));

StackPane root = new StackPane();
root.getChildren().add(fxButton);

Scene scene = new Scene(root, 150, 100);
jfxPanel.setScene(scene);
});

frame.add(panel);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
113 changes: 113 additions & 0 deletions src/main/java/playground/swing/SwingFXWebView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package playground.swing;

import com.sun.javafx.application.PlatformImpl;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
* SwingFXWebView
*/
public class SwingFXWebView extends JPanel {

private Stage stage;
private WebView browser;
private JFXPanel jfxPanel;
private JButton swingButton;
private WebEngine webEngine;

public SwingFXWebView(){
initComponents();
}

public static void main(String ...args){
// Run this later:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame = new JFrame();

frame.getContentPane().add(new SwingFXWebView());

frame.setMinimumSize(new Dimension(640, 480));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}

private void initComponents(){

jfxPanel = new JFXPanel();
createScene();

setLayout(new BorderLayout());
add(jfxPanel, BorderLayout.CENTER);

swingButton = new JButton();
swingButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Platform.runLater(new Runnable() {

@Override
public void run() {
webEngine.reload();
}
});
}
});
swingButton.setText("Reload");

add(swingButton, BorderLayout.SOUTH);
}

/**
* createScene
*
* Note: Key is that Scene needs to be created and run on "FX user thread"
* NOT on the AWT-EventQueue Thread
*
*/
private void createScene() {
PlatformImpl.startup(new Runnable() {
@Override
public void run() {

stage = new Stage();

stage.setTitle("Hello Java FX");
stage.setResizable(true);

Group root = new Group();
Scene scene = new Scene(root,80,20);
stage.setScene(scene);

// Set up the embedded browser:
browser = new WebView();
webEngine = browser.getEngine();
webEngine.load("http://wiki.ralfbarkow.ch");

ObservableList<Node> children = root.getChildren();
children.add(browser);

jfxPanel.setScene(scene);
}
});
}
}
46 changes: 46 additions & 0 deletions src/main/java/playground/swing/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package playground.swing;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;

public class Test {

private static void initAndShowGUI() {
// This method is invoked on Swing thread
JFrame frame = new JFrame("FX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setVisible(true);

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() {
// TODO Auto-generated method stub
return null;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGUI();
}
});
}
}

0 comments on commit dccb503

Please sign in to comment.