In short, I have a ScrollPane with a TextFlow that contains hyperlinks (maven repository). When I click on a hyperlink, I want to see the a-tags that the link contains on the same ScrollPane in the same window (or even in a new window). JavaFX is really new to me and I am struggling.
I am using jsoup to extract a-tags from a url.
More detailed: Here is my Main:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("TestFetch.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
There I call the TestFetch.fxml:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="575.0" prefWidth="787.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TestFetch1Controller">
<children>
<Button fx:id="btnFetchData" layoutX="368.0" layoutY="54.0" mnemonicParsing="false" onAction="#fetchData" text="Fetch Data" />
<ScrollPane fx:id="scrollPane" layoutX="46.0" layoutY="131.0" prefHeight="384.0" prefWidth="682.0">
<content>
<TextFlow fx:id="txtFlow" prefHeight="381.0" prefWidth="680.0" />
</content>
</ScrollPane>
</children>
</AnchorPane>
This contains a Button (btnFetchData), a ScrollPane and a TextFlow (txtFlow).
The controller is TestFetch1Controller:
public class TestFetch1Controller {
@FXML
private Button btnFetchData;
@FXML
private ScrollPane scrollPane;
@FXML
private TextFlow txtFlow;
public void fetchData(ActionEvent event) throws IOException {
final String url = "https://repo.maven.apache.org/maven2/avalon/";
Document docConnect = Jsoup.connect(url).get();
Elements links = docConnect.getElementsByTag("a");
for (Element link : links) {
String linkString = link.attr("href").toString();
System.out.println(linkString);
Hyperlink hyperlink = new Hyperlink(linkString);
hyperlink.setOnAction(e -> {
try {
goToLink(event, hyperlink.getText());
} catch (IOException e1) {
e1.printStackTrace();
}
});
txtFlow.getChildren().add(hyperlink);
}
}
public void goToLink(ActionEvent event, String hyperlinkURL) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("TestFetch2.fxml"));
Parent root2 = loader.load();
TestFetch2Controller testFetch2Controller = loader.getController();
testFetch2Controller.addHyperlinksToScrollPane(txtFlow);
Stage stage2 = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene2 = new Scene(root2);
stage2.setScene(scene2);
stage2.show();
}
}
Here I grabbed a public Maven repository, which I filter by a-tags. These then become "hyperlink" which are all added to txtFlow. Now I would like to have the same process again when I click on one of the hyperlinks. So that the a-tags of the hyperlink, which was clicked, are shown to me in the txtFlow.
Since I don't know how to reset the txtFlow, I created a new FXML, the TestFetch2.fxml:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="476.0" prefWidth="778.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TestFetch2Controller">
<children>
<ScrollPane fx:id="scrollPane2" layoutX="68.0" layoutY="22.0" prefHeight="432.0" prefWidth="642.0" />
</children>
</AnchorPane>
Here I have only one ScrollPane "scrollPane2". Using "setOnAction" ["goToLink()" in TestFetch1Controller] on hyperlink I wanted to tell each hyperlink to open the "TestFetch2.fxml" when clicked. This works once and then there is a NullPointerException ("stage2" is null). For your information, this is the TestFetch2Controller:
public class TestFetch2Controller {
@FXML
private ScrollPane scrollPane2;
public void addHyperlinksToScrollPane(TextFlow textFlow) {
scrollPane2.setContent(textFlow);
}
}
So basically my problem is that I don't know how to extract the data (a-tags) of the hyperlink to the next window. I want to be able to keep clicking on the hyperlinks till I came to the .jar.
Please ask any question if something is unclear.