Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTextArea append issue

Status
Not open for further replies.

Ascalonian

Programmer
Jan 4, 2008
264
US
I am sure several of you have had this issue:

I have a JTextArea in one of my swing apps. At a given point, I am pumping a lot of text to the JTextArea via the append() method.

The issue I see is that it doesn't display right away. Rather, it hangs and won't display until the very end. I would like this to display each append individually.

I am pretty sure this is a threading issue. Anyone know a solution so that each line I am appending to the JTextArea can be seen?

Thanks!
 
I have tried all exampled I could find online and have not been able to get this to work. I tried invokeLater, using Runnable classes, and more.

Does anyone have a specific example for this?

Here is more info:

Throughout my Swing code, instead of writing anything to System.out, I write it to the JTextArea. I created a method to do the writing, which is below:
Code:
private void writeOutput(String newOutput) {
    outputData.setForeground(Color.BLACK);
		
    if(outputData.getText() != null) {
        saveButton.setEnabled(true);
    }
		
    outputData.append(newOutput + "\n");
    outputData.setForeground(Color.BLACK);
}

I am then calling this in a for-loop that has over 700 items. So this can be called 700+ times. However, it will not display until the very end.

Thank you all for your help.
 
I actually got this to work. I was just looking at this incorrectly. I was trying to wrap the method writeOutput in a runnable... when I should have been wrapping the method that calls all those.

I had a method called validate() that had the for-loop inside. I had a JButton that had an ActionListener on it that called this method. I just had to make validate() be called from a Runnable.

So I had:
Code:
runButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        // Some code checked on some radio buttons
        if(radioButton.isSelected()) {
            if(radioButton.getText().equals("VALIDATE")) {
               validate();
            }
        }
    }

});

And now I have:
Code:
class ValidateThread implements Runnable {
    public void run() {
        validate();
    }
}

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! This worked just fine. I added some other stuff to make sure the runButton will not be clicked again, but this is the important part.

Hope this helps anyone else!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top