java - When Click on Hyperlinks in JavaFX ,a relevant URL should open in browser -


i developing application in have links added listview , these links keep on adding @ runtime on condition.so can't find way how open url when clicking on particular link.

this code adding links list view

    if(counter==1)                 {                     task task2 = new task<void>() {                           @override                           public void call() throws exception {                                platform.runlater(new runnable() {                                 public void run() {                                      link=new hyperlink(val);                                     link.setstyle("-fx-border-style: none;");                                     items.add(link);                                     listview.setitems(items);                                    }                               });                               return null;                            }                         };                         thread th = new thread(task2);                         th.setdaemon(true);                         th.start();                          thread.sleep(1000);                   } 

i know need use open url in browser when click on link

 gethostservices().showdocument(link.gettext());  

but don't know how listen/track click event different links

i made small example application you,

import java.util.arraylist; import java.util.list; import javafx.application.application; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.hyperlink; import javafx.scene.control.listview; import javafx.scene.control.textfield; import javafx.scene.layout.anchorpane; import javafx.scene.layout.hbox; import javafx.scene.layout.vbox; import javafx.stage.stage;  public class listlist extends application {      final listview listview = new listview();     @override     public void start(stage primarystage) {          list<hyperlink> links = new arraylist<>();          anchorpane pane = new anchorpane();         vbox vbox = new vbox();         final hyperlink link = new hyperlink("http://blog.professional-webworkx.de");         hyperlink link2= new hyperlink("http://www.stackoverflow.com");          links.add(link);         links.add(link2);          for(final hyperlink hyperlink : links) {             hyperlink.setonaction(new eventhandler<actionevent>() {                  @override                 public void handle(actionevent t) {                     gethostservices().showdocument(hyperlink.gettext());                 }             });         }          listview.getitems().addall(links);         hbox hbox = new hbox();         final textfield urlfield = new textfield();         button b = new button("add links");         hbox.getchildren().addall(b, urlfield);          b.setonaction(new eventhandler<actionevent>() {              @override             public void handle(actionevent t) {                 addlink(urlfield.gettext().trim());                 urlfield.clear();             }         });         vbox.getchildren().add(hbox);         vbox.getchildren().add(listview);         pane.getchildren().add(vbox);         scene scene = new scene(pane, 800, 600);         primarystage.settitle("hello world!");         primarystage.setscene(scene);         primarystage.show();     }      /**      * @param args command line arguments      */     public static void main(string[] args) {         launch(args);     }      private void addlink(final string url) {         final hyperlink link = new hyperlink(url);         link.setonaction(new eventhandler<actionevent>() {              @override             public void handle(actionevent t) {                 gethostservices().showdocument(link.gettext());                 //openbrowser(link.gettext());             }          });         listview.getitems().add(link);     }      private void openbrowser(final string url) {         gethostservices().showdocument(url);     } } 

if enter new url in textfield , click on button, new link added linklist , displayed on listview. everytime add new link, .setonaction() method set right url open.

maybe can use starting point further developing app.

patrick


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -