0

I am trying to create a GUI for a webcrawler. When they press the search button, I want a text to appear that says "Searching..."

public class Main extends Application {

    private CrawlerTest c;
    private Scene scene1, scene2;
    private String urls;
    private Text urlsText, searchText;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        this.c = new CrawlerTest();
        primaryStage.setTitle("WebCrawler");



        //layout1
        TextField searchInput = new TextField();
        TextField keyword = new TextField();
        Label searchLabel = new Label("Website URL");
        searchLabel.setLabelFor(searchInput);
        Label keywordLabel = new Label("Search for keyword");
        keywordLabel.setLabelFor(keyword);
        Button searchButton = new Button();
        searchButton.setText("Search!");
        this.searchText = new Text("");
        this.searchText.setFont(new Font(20));
        searchButton.setOnAction(e -> {
            if (this.checkURL(searchInput.getText())) {
                this.searchText.setText("Searching... This may take a while.");
                this.urls = this.c.run(searchInput.getText(), keyword.getText());
                primaryStage.setScene(scene2);
                this.urlsText.setText(this.urls);
            }
            else {
                AlertError.display("Error!", "Entered URL format is incorrect");
            }
        });

        FlowPane container = new FlowPane();
        container.getChildren().addAll(searchInput, keyword, searchButton);
        VBox layout1 = new VBox(10);
        layout1.setPadding(new Insets(20, 20, 20, 20));
        layout1.getChildren().addAll(searchLabel, searchInput, keywordLabel, keyword, this.searchText, searchButton);
        scene1 = new Scene(layout1, 300, 250);

The problem is that this.searchText.setText("Searching... This may take a while."); does not change the text when it is in the if statement if (this.checkURL(searchInput.getText())) but it does work outside of the if statement. I know this if statement is entered as I tried debugging it and it worked. Can someone help me change the text of searchText when the button is clicked? Thanks!

  • 1
    Does CrawlerTest.run return immediately? If it’s a lengthy operation, it’s preventing your UI from updating, because it blocks the JavaFX application thread. – VGR May 14 '19 at 23:31

1 Answers1

-1

This entry will help you: How do I update Text Using Java.fx?

It is because you need to use:

Platform.runLater(() -> {
    textArea.appendText("New text");
});

Do all your GUI updates into Platform.runLater(runnable); method, that way it will get updated.

Jeremy Then
  • 525
  • 2
  • 12
  • 1
    Applying this approach to the question directly will simply result in `Searching... This may take a while.` being displayed in the end instead of some other text. Actually explaining the issue and giving attention to the question may have made this a decent answer, but since you don't do this, it's just a low quality answer that's proably of little help to the OP... – fabian May 15 '19 at 02:31