PhoenixRising666
PhoenixRising666

Reputation: 25

Updating Text Area Swing Java

I have a Swing GUI with a run button with the following code

 private void runButtonActionPerformed(java.awt.event.ActionEvent evt) { 
    staticLinkTextArea.append("Building Site Map (this can take a while depending on depth) \n");
    crawl.CrawlerInit("http://www.asquithnurseries.co.uk/", "asquithnurseries.co.uk",1);
    crawl.runCrawler();
    staticLinkTextArea.append("Building Complete Searching for Static Links \n");

    crawl.runListURL();

}

I want to let the user know that the site is being crawled and this could take a long time. However the staticLinkTextArea.append is not taking effect until after crawl.CrawlerInit and crawl.runCrawler complete. How can I force it to happen first?

Upvotes: 1

Views: 132

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

I want to let the user know that the site is being crawled and this could take a long time. However the staticLinkTextArea.append is not taking effect until after crawl.CrawlerInit and crawl.runCrawler complete. How can I force it to happen first?

Yours is a classic example of running long-running code on the Swing event thread, and thereby freezing that thread. The solution is the same as always: run the long-running code in a background thread such as a new Thread(...) or a SwingWorker.

e.g.,

private void runButtonActionPerformed(java.awt.event.ActionEvent evt) { 
    staticLinkTextArea.append("Building Site Map (this can take a while depending on depth) \n");
    crawl.CrawlerInit("http://www.asquithnurseries.co.uk/", "asquithnurseries.co.uk",1);
    new Thread(new Runnable() {
        public void run() {
            crawl.runCrawler();
            crawl.runListURL();
        }
    }).start();
    staticLinkTextArea.append("Building Complete Searching for Static Links \n");

}

Your use of static in your method names does worry me, and hopefully they don't mean that you're using static methods here.

Upvotes: 6

Related Questions