0

I am trying to implement embedded browser in my java Netbeans project so far so good but still the browser is not sophisticated enough to store cookies or any advanced features my goal is basically to be able to access google account and check email or calendar or any similar operation.. can someone please direct me to the right code? thanks in advance. here is my code:

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.beans.value.ObservableValue;
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;
import javax.swing.event.ChangeListener;

/** 
* 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("https://www.google.com");


                //https://calendar.google.com
                ObservableList<Node> children = root.getChildren();
                children.add(browser);                     

                jfxPanel.setScene(scene);  
            }  
        });  
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
MIKE PERETZ
  • 149
  • 2
  • 17
  • Why are you doing `PlatformImpl.startup(...)`? Did you intend `Platform.runLater(...)`? – James_D Sep 25 '17 at 21:58
  • Maybe `Desktop::browse` instead? – trashgod Sep 25 '17 at 22:49
  • There are a couple of questions discussing cookie management and `WebView`: https://stackoverflow.com/questions/14385233/setting-a-cookie-using-javafxs-webengine-webview, and https://stackoverflow.com/questions/45827482/access-all-the-cookies-from-a-javafx-webview (and maybe others) – James_D Sep 26 '17 at 00:56
  • Besides cookie managing any other elements requested? – MIKE PERETZ Sep 26 '17 at 06:42

0 Answers0