-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(SwingAppWithJavaFX, SwingFXWebView): JFXPanel
"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
1 parent
20363c0
commit dccb503
Showing
4 changed files
with
216 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} | ||
} |