Search code examples
javaswingwhile-loopjtextareaupdating

Updating jTextArea in while loop


I am having a problem writing/updating the textarea. I am getting a value from the readtemp function, and i can see the result after calling the system out function, but nothing appears in the Textarea. What could be the problem?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    URL temp;
    try 
    {
            temp = new URL("http://192.168.1.25/status.xml");
            while (true)
            {
                System.out.println("Homerseklet: " + readtemp(temp));
                jTextArea1.append(readtemp(temp));
            }                   
    } 
    catch (MalformedURLException ex) 
    {
        Logger.getLogger(Download.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                        

Solution

  • Correction: This won't help since the infinite loop will still block the EDT forever... Nevermind!

    Your while loop is a really bad idea, but if you insist, you can at least give the EDT a chance to update the UI by dispatching your append asynchronously:

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            jTextArea1.append(readtemp(temp));    
        }
    });