Is it possible to find a specific Hyperlink within VBox container?
Say i have code that dymanically adds hyperlinks into a VBox element but i dont want to add the same link twice, easiest way here any ideas?
What i thought of was to search and see if the Hyperlink already is within the VBox by doing something like:
myContainer.getChildren().forEach(node -> {
if(node.getClass().getSimpleName().equals("Hyperlink") {
Node n = node.getClass();
// Do my stuff
}
});
You code compares the class name (without the package), so you cannot distinguish the various hyperlinks that way.
What you can do is assign each Hyperlink
an ID that is based on the URL:
String url = ...
String urlToID = String.valueOf(url.hashCode());
...
// check if that urlToID is already present
boolean present = false;
for (Node child : myContainer.getChildren()) {
if (child.getId().equals(urlToID)) {
present = true;
break;
}
if (!present) {
Hyperlink link = ...
link.setId(urlToID);
myContainer.getChildren().add(link);
}
The for loop can be further optimized using streams, but I think this is more readable:
boolean present = myContainer.getChildren().stream().filter(node ->
return node.getId().equals(urlToId);)
.findFirst().isPresent();