I recently needed to collect and display a list of hyperlinks. This helpful example illustrates using jsoup and a Task<List<JSoupData>> to collect anchor tags for display in a TableView. As I wanted to reuse the Task, I created a Service<List<Element>> that follows the API example:
private static class SoupService extends Service<ObservableList<Element>> {
private final StringProperty url = new SimpleStringProperty();
@Override
protected Task<ObservableList<Element>> createTask() {
final var site = url.get();
ObservableList<Element> list = FXCollections.observableArrayList();
return new Task<ObservableList<Element>>() {
@Override
protected ObservableList<Element> call() throws Exception {
Document document = Jsoup.connect(site).get();
Elements elements = document.select("a[href]");
for (Element element : elements) {
list.add(element);
}
return list;
}
};
}
…
}
In this case, List is an ObservableList and Element contains the data to construct and use a Hyperlink. I think I can use a ListCell<Element> as a cell factory to separate the link itself from the text of the link, but I'm not sure how to proceed. Any guidance would be welcome.
