This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How can I show new text in JTextArea while program is executing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How can I show new text in JTextArea while program is executing? " Watch "How can I show new text in JTextArea while program is executing? " New topic
Author

How can I show new text in JTextArea while program is executing?

Dave Wilmsmeyer
Greenhorn

Joined: Feb 10, 2012
Posts: 7

Hi all, first post here, and I most definitely am a greenhorn.

I have a program that reads in data from files, and then it analyzes that data, and then spits out an analysis of the data into an html page. This all works just fine. However, sometimes it takes the program a while to analyze the data file, as some of the data files that users read can be rather large. . And, if the data file IS a large file, I want status updates to be sent to the user, so they know the program is not hung.

So, the GUI of my program is a JPanel with a TextPanel that has a JTextArea, and that works fine too. That is, for the most part. Because, anytime I 'add' or 'append' text to the area it does work. However, and this is my problem, if the data file IS a large file, my status messages added to the JTextArea do not show up until AFTER the entire file has been analyzed, and by that point they are obviously useless.

TextPanel = new JPanel();
TextPanel.setLayout(new BorderLayout());
Text = new JTextArea("blah, blah",x,y);

Now, my program has a function where it determines the size of the file that the user specified for it to read. If the file is over a certain size, then it reads in chunks of the file at a time and then does analysis on each chunk, and after all chunks have been analyzed, it puts the file back together and all that works just fine, except for status updates.

It is in the part of the code that I want the status updates . . .

int numOFbuffers = filesize/buffersize;

if (numOFbuffers > 0)
{
moreData = true;
while (numOFbuffers >= 0)
{
Text.append("\nWorking...\n"); <-------------- write a status update

byte[] buffer = new byte[buffersize];
infile.read(buffer);
String fileData = new String(buffer);
analyzeData(fileData);
numOFbuffers--;
}

Alas, as I said, it will not show the updates until AFTER all of the buffers have been analyzed. So, I have tried repaint, requestfocus, etc and I cannot get this to work. How can I make this update while the program is executing?

Thanks in advance!

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html -- especially the section on SwingWorker


luck, db
There are no new questions, but there may be new answers.
Dave Wilmsmeyer
Greenhorn

Joined: Feb 10, 2012
Posts: 7
Ok, so I was wondering if I would need to put this on its own thread. But, I was just checking to see if there was some other EZ way to do this, that I was missing.

Thank you!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Dave Wilmsmeyer wrote:EZ

Please UseRealWords: "easy", not "EZ".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Rob Spoor wrote:
Dave Wilmsmeyer wrote:EZ

Please UseRealWords: "easy", not "EZ".

Oh, is that what it means?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Well, that's what I make of it in this case. Pronunciation matches, and in this context it makes sense as well. In other cases / contexts EZ may mean something different.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Rob Spoor wrote: Pronunciation matches

Not if you're British it doesn't.


Joanne
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Joanne Neal wrote:
Rob Spoor wrote: Pronunciation matches

Not if you're British it doesn't.

Not if you're Indian either.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I show new text in JTextArea while program is executing?
 
Similar Threads
opening a video file in a browser
How to implement file downloading functionality ?
mmm.. music
reducing latency in real time audio on windows
Writing in chunks