Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.Point;
- import javafx.application.Platform;
- import javafx.beans.value.ChangeListener;
- import javafx.beans.value.ObservableValue;
- import javafx.embed.swing.JFXPanel;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.web.WebEngine;
- import javafx.scene.web.WebView;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.SwingUtilities;
- public class Test implements ChangeListener<String> {
- /* Start application */
- public static void main(final String[] args) {
- new Test();
- }
- public Test() {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- initAndShowGUI();
- }
- });
- }
- /* Create a JFrame with a JFXPanel containing the WebView. */
- private JFrame frame;
- private void initAndShowGUI() {
- // This method is invoked on Swing thread
- frame = new JFrame("FX");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null); // do the layout manually
- final JFXPanel fxPanel = new JFXPanel();
- frame.add(fxPanel);
- frame.setVisible(true);
- fxPanel.setSize(new Dimension(300, 300));
- frame.getContentPane().setPreferredSize(new Dimension(300, 300));
- frame.pack();
- Platform.runLater(new Runnable() { // this will run initFX as
- // JavaFX-Thread
- @Override
- public void run() {
- initFX(fxPanel);
- }
- });
- }
- private WebEngine webEngine;
- /* Creates a WebView and fires up google.com */
- private void initFX(final JFXPanel fxPanel) {
- Group group = new Group();
- Scene scene = new Scene(group);
- fxPanel.setScene(scene);
- WebView webView = new WebView();
- group.getChildren().add(webView);
- webView.setMinSize(300, 300);
- webView.setMaxSize(300, 300);
- // Obtain the webEngine to navigate
- webEngine = webView.getEngine();
- webEngine.load("http://www.google.com/");
- // Add a new listener to the URL property
- webEngine.locationProperty().addListener(this);
- }
- public void exit() {
- final Test self = this;
- Platform.runLater(new Runnable() {
- @Override
- public void run() {
- Platform.exit();
- }
- });
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- self.frame.dispose();
- }
- });
- }
- /**
- * Fires when URL changes
- */
- @Override
- public void changed(ObservableValue<? extends String> arg0, String oldUrl, String newUrl) {
- // Conditions should be here (if url.equals(successUrl) ..
- this.exit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement