I am developing an application in which I have some links added to the Listview and these links will keep on adding at runtime on some condition.So what I can't find is a way to how to open a url when clicking on a particular link.
This is the code for adding links into 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 I need to use something like this to open a url in browser when click on link
getHostServices().showDocument(link.getText());
but I don't know how to listen/track the click event for different links
I made a very small example Application for you,
If you enter a new URL in the TextField and click on the Button, the new Link will be added to your LinkList and will be displayed on the ListView. Everytime you add a new Link, the
.setOnAction()
Method is set with the right URL to open.Maybe you can use this as starting point for further developing your app.
Patrick
I put a Tooltip to the hyperlink and read the url from there, and seems to work. i.e.:
Old question, old answer which is working fine - but has a slight smell: it's adding views (Hyperlinks) as data to the ListView.
The cleaner alternative is to add URLs as data and implement a custom cell that uses Hyperlinks to show (and allow interaction with) the URLs.
A quick code example: