• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to update a JTextArea in real time?

 
knzo lobos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well until now i was working with
log.append(line);

but i can only see the final result of the textarea, but i need
to see it in real time because i am simulating a console

thanks for any help

cya!
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a situation where text is sent to the JTextArea one line at a time, but the lines only actually appear in the text area all at once at the end of program execution?

If so, it is possible that as soon as you issue the command to display a line of text in the text area, then your code starts churning away on the next task, and it hogs the CPU, and nothing gets displayed until the CPU is free at the end. Just a guess.

If this is so, then depending on your program design, the solution would probably involve the appropriate use of threads, or Swing Timers, or something like that.
 
knzo lobos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for aswer

well thi is not because of the pc

other people have said the same to me but i don`t really understand thread very well
y will continue trying

cya
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
knzo lobos wrote:thanks for aswer
well thi is not because of the pc

Please re-read what Fred wrote as he never said that this was because of the pc. It's possibly because of a threading issue, and I strongly agree with him. Again, use a background thread such as a SwingWorker. Please read up on Concurrency In Swing.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This does not indicate a problem or limitation with your PC, it is about how your program design affects the allocation of your computer's resources.

If you research threads a bit, and maybe give us a better idea of your program design, probably it could be sorted out pretty easily.

p.s. as Pete said.

Also, if you find the Sun java materials on Concurrency a little difficult, the following article provides a decent introduction. Just be aware that the article is kind of old, and makes reference to deprecated mathods, such as stop(). But the eplanation of the basic ideas is good, and would make the more uptodate material in the Java Tutorial easier to understand

http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html
 
knzo lobos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well people
first i need to say that as i don`t manage completely english i had some problems
as you recommended me i saw that web and many others

even if i use the run(), the text doesn`t appear in real time, only at the end

if you can check it ... ^^



thannks!!!
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I see it, the issue is not so much that your code needs to execute in a thread, but that the the code that updates the text area needs to happen in a different thread than the continuously running part of the code.

I don't totally understand what is going on with your program, but it appears that you have not separated into separate threads those parts of the code that are in conflict for CPU time,

It's not an easy topic to understand, it may require some research of the basic concepts of multithreading for you to appreciate what I am saying. Anyways, I am not an expert in threads, maybe someone else can describe it better.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) The first step is to create and display the Jframe. The frame should contain your empty text area.

2) Once that is done you create a Thread (SwingWorker) to login to the database. As you receive messages from the login process you append the messages to the text area.

Your problem is that you are trying to combine the above two steps into one step and the code is executing in the same Thread so the GUI can't repaint itself.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I have a suggestion. Start from scratch and try to design a very simple prototype that duplicates the problem without all the bells and whistles that your existing program has. Maybe your prototype will involve a very simple continuously running loop that does some computations and outputs results to the JText area. Do it without using any special thread programming or run() method.

Once you have done that give an effort to fixing the problem in the simple situation, that will give you the idea how to solve the problem in your console simulator program. It will also be easier for us to help you with the prototype than with your console program.

p.s. OK Rob has a similar idea, he snuck in ahead of me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!