This discussion is archived
6 Replies Latest reply: Mar 28, 2013 2:25 PM by Alaric RSS

open URL in default browser

Alaric Newbie
Currently Being Moderated
Hi All,
I'm very new to this (java(fx) programming) and the last days I'm trying to figure out how I can open the default browser when the user clicks the image (loaded from webpage) in the JFXPanel. This image comes from an openx (ads) server and holds a URL as destination link.
So far all my attempts to accomplish this have failed and I'm lost in this.

Currently when clicking the image, the destination webpage opens in in the same JFXpanel (ofcourse), but I would like to open this in the default browser.

this is what I currently have for initiating the scene from swing
            ap = new AdsPanel();
            ap.init();
            JPanel ad = new JPanel();
            ad.add(AdsPanel.fxContainer);
the class for loading the webpage:
    //@Override
    public void init()
    {
        fxContainer = new JFXPanel();
        fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
        // create JavaFX scene
        Platform.runLater(new Runnable()
        {
            @Override
            public void run()
            {
                createScene();
            }

        });
    }

private void createScene()
    {
        Platform.runLater(new Runnable()
        {
            @Override
            public void run()
            {
                loadURL(FixedVars.SPONSORLINK);
                WebView view = new WebView();

                webEngine = view.getEngine();
                view.setContextMenuEnabled(false);

                Scene scene = new Scene(view);
                fxContainer.setScene(scene);
            }
        });
Hope some can help me out.

Thank you very much in advance.
  • 1. Re: open URL in default browser
    Alaric Newbie
    Currently Being Moderated
    oke, after some experiments, I came up with following for creating the scene. This does almost what I want.
    It opens the target URL twice in a separate browser... (one is more than enough....)

    However, the fxpanel also opens the new link too and should just display same URL/ad.

    Anyone?
    private void createScene()
        {
            Platform.runLater(new Runnable()
            {
                @Override
                public void run()
                {
                    loadURL(FixedVars.SPONSORLINK);
    
                    WebView view = new WebView();
    
                    webEngine = view.getEngine();
                    view.setContextMenuEnabled(false);
    
                    webEngine.locationProperty().addListener(new ChangeListener<String>()
                    {
    
                        @Override
                        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
                        {
                            // trigger the open default browser class
                            if ( !FixedVars.SPONSORLINK.equals(newValue) )
                            {
                                System.out.println("IN THE LISTENER oldvalue: " + oldValue);
                                System.out.println("IN THE LISTENER newvalue: " + newValue);
    
                                BrowserOpener bo = new BrowserOpener();
                                try
                                {
                                    bo.open(newValue);
                                }
                                catch ( Exception ex )
                                {
                                    Logger.getLogger(AdsPanel.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        }
    
                    });
    
                        Scene scene = new Scene(view);
                        fxContainer.setScene(scene);
                }
    
            });
    
        }
    thanks in advance, any help is very appreciated
  • 2. Re: open URL in default browser
    jsmith Guru
    Currently Being Moderated
    I don't advise mixing JavaFX and Swing unless you have to, but I am guessing you have to for reasons outside of this thread.

    In JavaFX you can open a url in a browser using the HostServices showDocument(uri) method:
    http://docs.oracle.com/javafx/2/api/javafx/application/HostServices.html#showDocument%28java.lang.String%29

    In Swing you can accomplish the same thing using the Desktop browse(uri) method:
    http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#browse%28java.net.URI%29

    As you are using both JavaFX and Swing either method should work (just be careful that you execute any JavaFX code on the JavaFX application thread and any Swing code on the Swing event dispatch thread).

    If instead of launching the URL in a browser, you wanted to just display the web content inline in your app, then you could use WebView:
    http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm
    http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm#CHDIEEJE

    I see in the sample code that you posted that you use a WebView, but I don't know why you would do so rather than using the hostServices.showDocument or desktop.browse APIs if you intention is to open the URL in the default browser rather than inline in your application.
  • 3. Re: open URL in default browser
    Alaric Newbie
    Currently Being Moderated
    Thank you for the reply.
    I started with this app in Swing, Later I had to include a webpage which included js. After Googling I found that the only way of doing that is through the webview and thus needed JavaFX. Is this correct? or are there better ways?

    What I try to achieve;
    1. display a small image from webpage with js (advertisement or company messages) served from a website.
    2. when user clicks the ad, the destination link will open a new default browser
    3. the already showed image does not change -> still same ad/message

    After checking the links you provide, I doubt have choosen the right solution for my challenge. I'm doing this the right way? if not can you show me the direction to go? I'm strugling already for some days now....

    How can I use the showDocument and does this support js?

    thanks for the help in advance
    regards

    Edited by: Alaric on Mar 26, 2013 9:40 PM
  • 4. Re: open URL in default browser
    peterz Newbie
    Currently Being Moderated
    Here's one take on the problem:
    Display the ad in a WebView, inspect DOM to get its target URL. Then add a DOM event listener to the ad's image element, and from that listener call showDocument() with the target URL.
    You'll also need to suppress following the URL in the WebView's itself, e.g. by setting onclick attribute.
  • 5. Re: open URL in default browser
    Alaric Newbie
    Currently Being Moderated
    Thank you for the reply

    I have it almost running now.... found a similar issue at: [http://stackoverflow.com/questions/15555510/javafx-stop-opening-url-in-webview-open-in-browser-instead]
    Following code does what I expect it to do. It opens the destination URL in the default browser BUT also crashes the jvm...

    I think this has to do something with threads... So I just need to find a way in order to have the jvm not to crash.
    Hope someone sees the issue and solution for this.
    private void createScene()
        {
            Platform.runLater(new Runnable()
            {
                @Override
                public void run()
                {
                    loadURL(FixedVars.SPONSORLINK);
                    WebView view = new WebView();
                    webEngine = view.getEngine();
                    view.setContextMenuEnabled(false);
    
    
                    webEngine.locationProperty().addListener(new ChangeListener<String>()
                    {
                        @Override
                        //Override public void changed(ObservableValue<? extends String> ov, final String oldLoc, final String loc)
                        public void changed(ObservableValue<? extends String> observable, final String oldValue, final String newValue)
                        {
                            Desktop d = Desktop.getDesktop();
                            try
                            {
                                URI address = new URI(newValue);
    
                                if ( !newValue.contains(FixedVars.SPONSORLINK) )
                                {
                                    System.out.println("newval =: " + FixedVars.SPONSORLINK);
                                    Platform.runLater(new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {
                                            loadURL(oldValue);
                                        }
    
                                    });
                                    d.browse(address);
                                }
                            }
                            catch ( IOException | URISyntaxException e )
                            {
                                System.out.println(e);
                            }
                        }
    
                    });
    
                    Scene scene = new Scene(view);
                    fxContainer.setScene(scene);
                }
    
            });
        }
  • 6. Re: open URL in default browser
    Alaric Newbie
    Currently Being Moderated
    Just found my solution!! OPening the default browser in a separate thread. Please feel free to comment me, on the code (I do not earn my money as a developer)
    private void createScene()
        {
            Platform.runLater(new Runnable()
            {
                @Override
                public void run()
                {
                    loadURL(FixedVars.SPONSORLINK);
                    WebView view = new WebView();
    
                    webEngine = view.getEngine();
                    view.setContextMenuEnabled(false);
    
                    webEngine.locationProperty().addListener(new ChangeListener<String>()
                    {
                        @Override
                        public void changed(ObservableValue<? extends String> observable, final String oldValue, final String newValue)
                        {
                                if ( !newValue.contains(FixedVars.SPONSORLINK) )
                                {
                                    System.out.println("newval =: " + FixedVars.SPONSORLINK);
                                    Platform.runLater(new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {
                                            loadURL(oldValue);
                                        }
    
                                    });
                                    // open the destination URl in the default browser
                                    // class will open a new thread
                                    BrowserOpener.main(newValue);
                                }
                            }
                    });
    
                    Scene scene = new Scene(view);
                    fxContainer.setScene(scene);
                }
            });
        }
    and the class opening the default browser:
    public abstract class BrowserOpener implements Runnable {
    
        /**
         * url string
         */
        private static String uriString;
    
        public void open(String url) throws Exception{
            URI u = new URI(url);
            java.awt.Desktop.getDesktop().browse(u);
        }
    
    
        public void run() {
            try
            {
                open(uriString);
            }
            catch ( Exception ex )
            {
                Logger.getLogger(BrowserOpener.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        public static void main(String args) {
            (new Thread(new BrowserOpener() {})).start();
            uriString = args;
        }
    
    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points