JavaFx 8: open a link in a browser without reference to Application

前端 未结 3 1364
醉梦人生
醉梦人生 2020-12-09 05:37

Have got a Hyperlink. When clicked I want a link to be opened in an external browser.

The usual method cited on the web seems to be:

final Hyperlink         


        
3条回答
  •  渐次进展
    2020-12-09 05:48

    Another way would be to use java.awt.Desktop

    Try (untested):

    URI uri = ...;
    if (Desktop.isDesktopSupported()){
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)){
            desktop.browse(uri);
        }
    }
    

    Note however that this will introduce a dependency to the AWT stack. This is probably not an issue if you're working with the full JRE, but it might become an issue if you want to work with a tailored JRE (Java SE 9 & Jigsaw) or if you want to run your application on mobile device (javafxports).

    There is an open issue to support Desktop in JavaFX in the future.

提交回复
热议问题