• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Open the browser and a web page from the SecondaryController in FXML JavaFX Maven Archetype (Gluon)

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I open the browser and a web page from the PrimaryController, and not from the main App, of a Javafx FXML application?
 
Bartender
Posts: 246
12
IntelliJ IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AFAIK The same that you'd do in Swing/AWT, unless you mean something different. (Inside an event handler, ex https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ButtonBase.html#setOnAction-javafx.event.EventHandler- )


 
Alberto Cimaroli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lou Hamers wrote:AFAIK The same that you'd do in Swing/AWT, unless you mean something different. (Inside an event handler, ex https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ButtonBase.html#setOnAction-javafx.event.EventHandler- )




Thanks for the help. I tried but an error is encountered on the Desktop class.

I found this alternative:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
What functionality do you need? if you're looking for a way to open web pages like with Desktop.getDesktop().browse(new URI("www.stackoverflow.com"));, you can use this function:


(this needs to be an instance of Application)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I wrote this code:
--------------------------------
                       
-------------------------------
that does not give errors, but unfortunately it doesn't work!

 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alberto Cimaroli wrote:I tried but an error is encountered on the Desktop class.


What was the error?
 
Alberto Cimaroli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error is reported but the import of the Desktop class in Java AWT is not proposed from Netbeans 13.
 
Lou Hamers
Bartender
Posts: 246
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh right, I forgot about that method! (Which is funny because I use it in a production application...) My fault for replying in a hurry.

Anyway, for your new code that doesn't work, there isn't context provided, but I'm guessing that new Application instance goes out of scope immediately and it "does nothing" (i.e. doesn't work).

If you do this it works fine:

I would use the real Application instance, rather than trying to create a new one. If you don't have access to it, there's a few ways around that I can suggest:

  • Make your main Application instance accessible by passing it to the class creating this Alert
  • Create a static utility method on your main class which does the work of showing the Alert and opening  the page
  • Create a static utility method on your main Application class which returns itself, so you can call getHostServices() on it
  • Make your HostServices available to the code creating the dialog, same methods as the first two options

  • Which of these makes more sense depends a little on your application structure. The static utility methods will work for any. Without a dependency injection framework that works for JFX like Afterburner.fx, you might be better off using one of those methods inside your main Application extending class:

    (Some of the options above are "sloppier" than the others so I'll suggest one that's generally reliable but doesn't entirely ignore best practices.)

    That Button code can live pretty much anywhere. If you use Afterburner.fx, I'd suggest injecting HostServices into the controller(s) it's needed in.
     
    Alberto Cimaroli
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks. I have already tried an example that like yours works in the App.java class.
    My problem is that I should open the webpage from the PrimaryController.java of the JavaFX FXML application.
    I also tried to integrate the code into a method of the App.java class but the call from PrimaryController.java fails.
     
    Alberto Cimaroli
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Finally I solved the problem with the WebView and VebEngine classes  in application
    FXML JavaFX Maven Archetype (Gluon) as attached code:

    module-info.java
    -----------------------



    App.java
    ------------



    primary.fxml
    -------------------------------


    PrimaryController.java
    -------------------------------



    secondary.fxml
    -----------------------


    SecondaryController.java
    -----------------------------------

     
    Lou Hamers
    Bartender
    Posts: 246
    12
    IntelliJ IDE Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    HostServices method will work fine from a different controller too. That's exactly why I used the static methods. You didn't say what it did wrong, but maybe you just forgot something? Here it is in the example code you pasted, test works fine here:



     
    No more fooling around. Read this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic