Created
January 19, 2012 15:41
-
-
Save anjackson/1640654 to your computer and use it in GitHub Desktop.
Embedding a JavaFX WebView in a Swing panel.
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
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://www.google.com"); | |
ObservableList<Node> children = root.getChildren(); | |
children.add(browser); | |
jfxPanel.setScene(scene); | |
} | |
}); | |
} | |
} |
Awesome code! Thanks for sharing!
Works fine. Thanks a lot !
How can I set browers's size?
very good, thanks a lot !
I use javafx 13 and netbeans IDE 22. Project: maven application
Error:
java.lang.NoSuchMethodError: 'void com.sun.javafx.stage.EmbeddedWindow.setNodeOrientation(javafx.geometry.NodeOrientation)'
Please suport
Many thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried this gist but the page is very slow.
Is this happening with you too?