views:

1686

answers:

8

I am trying to create a very simple Swing UI that logs information onto the screen via a JTextArea as processing occurs in the background. When the user clicks a button, I want each call to:

textArea.append(someString + "\n");

to immediately show up in the UI.

At the moment, the JTextArea does not show all log information until the processing has completed after clicking the button. How can I get it to refresh dynamically?

+1  A: 

You'll need multithreading for this. Ready to take the plunge?

Spencer Ruport
I've got my hard hat on. So yes.
digiarnie
+1  A: 

If it's multithreading you need try this! http://www.devarticles.com/c/a/Java/Multithreading-in-Java/

Graham
+3  A: 

As others have said this requires multithreading. Have a look at Concurrency in Swing.

One solution would be to implement the processing using a SwingWorker. The doInBackground method would implement the processing and you would invoke the publish method with the String to be appended as the argument. Your SwingWorker would then override the process method to take a String argument and append it to the text area.

Mark
+2  A: 

I ran into the same issue with my application. I had a "Run" button my application that performed some actions and outputted the results to a JTextArea. I had to call the method from a Thread. Here is what I did.

I have several radio buttons of actions that can be done, and then one "Run" button to execute that particular action. I have an action called Validate. So when I check that radio button and click the "Run" button, it calls the method validate(). So I first placed this method into an inner class that implemented Runnable

class ValidateThread implements Runnable {
    public void run() {
        validate();
    }
}

I then called this thread in the ActionListener of the "Run" button as so

runButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        // Some code checked on some radio buttons
        if(radioButton.isSelected()) {
            if(radioButton.getText().equals("VALIDATE")) {
               Runnable runnable = new ValidateThread();
               Thread thread = new Thread(runnable);
               thread.start();
            }
        }
    }

});

Voila! The output is now sent to the JTextArea.

Now, you will notice that the JTextArea will not scroll down with the text. So you need to set the caret position like

textArea.setCaretPosition(textArea.getText().length() - 1);

Now when the data is added to the JTextArea, it will always scroll down.

Ascalonian
A: 

Your best bet is to take a look at swing-worker (OSS lib for Java5, and part of java6). Sorry cannot tell you more now (time to go to bed;-)). A Google search should be straightforward I think.

jfpoilpret
A: 

Check dis it will useful for you

"technobuz.com/2009/05/update-jtextarea-dynamically/"

A: 

Try this: jTextArea.update(jTextArea.getGraphics());

petr
A: 

Try this: jTextArea.update(jTextArea.getGraphics());

--> Thanx, it worked for me.

Jericho
Hello, welcome to Stack Overflow. Please review the [FAQ](http://stackoverflow.com/faq) to learn more about how this site works. This site is not a discussion board, this is a place for questions to be asked and answered. As such, you should not post a new answer if what you want to say doesn't actually answer the question.
Gnoupi