Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Repetetively Append to JTextArea? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Repetetively Append to JTextArea?" Watch "Repetetively Append to JTextArea?" New topic
Author

Repetetively Append to JTextArea?

D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
As an exercise, I am writing a program that continuously generates prime numbers. There is a button on the GUI to (hopefully) start and stop the process. When the numbers are being generated, they are to continuously append to a JTextArea in the center region of Border Layout. I have created the outline, but have not yet entered the algorithm to find the prime numbers. (I have a command-line model that works. But I want it in a GUI.) Instead, for developmental purposes, I have nested loops that advance integers: 1, 2, 3, etc. -- In place of the prime numbers. Same concept, though, as far as writing to the JTextArea goes.

Here is my problem. When I click the button, the number advances by one, then waits for the next click. The next two clicks advance the number, but only by one more. (The idea should be that one click should stop it and the next click resume. Hence the two clicks to advance one number.) I thought the loops would let it continue to append numbers until I click to stop it, but that's not what happens. So -- have I done something incorrectly, or is there some characteristic of the loops I've used or JTextArea that is keeping me from getting what I envision? Here is my code:

Jeyram Tamate
Greenhorn

Joined: Sep 29, 2012
Posts: 9
Here is the resolved code. I guess this would be helpful. I have changed class name because it was there in my netbeans so i did some changes and your code is working as it needs to be. Best of luck then.




Here i have used a thread to start putting numbers into textfield ..this thread would run in background giving you effect which you wanted.
D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
Jeyram, thanks. But why wouldn't my code work?
Jeyram Tamate
Greenhorn

Joined: Sep 29, 2012
Posts: 9
Actually if you have anyway managed get your code in working condition then there would have been another problem. You could have placed a while loop with flipper as exit condition in action performed but that would have been stopped to do anything in your application. Then you wouldn't have been able to press that stop and resume button. That's why there was a need of threading. I hope you have got what I wanted to say.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Jeyram Tamate, welcome to the Ranch!

I must point you to this page: NotACodeMill to keep some facts in mind for future posts. Avoid posting complete solutions, because that's not how the forum works. Rather give tips and point OP in the right direction, so he/she will be able to solve the issue on their own.


The quieter you are, the more you are able to hear.
D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
Thanks, to all. Yes, I'm looking for insight as opposed to being given the solution. And it's still not clear to me why my approach doesn't work. The numbers advance correctly with each pair of mouse clicks, but I don't see why the loop won't continue to append new numbers without the clicks.

Thanks for any explanations.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

It's not exactly a small topic, do I won't try to post an 'answer in a nutshell.' Rather, I'll point you to a tutorial which goes into adequate detail: Concurrency in Swing. You can also find some valuable and interesting insights, on the Oracle site and elsewhere, by searching the net for "Swing single threaded rule."+

edit: Note that Jeyram Tamate's sample does not respect the single threaded rule and should never be used in production code. The minimum change needed in that code, using a background thread, would be to wrap the call to setText(...) in a SwingUtilities#invokeLater(...).


luck, db
There are no new questions, but there may be new answers.
D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
BTW -- I've been wondering. What is the reference to "OP?"

Thanks.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

D. Brown wrote:BTW -- I've been wondering. What is the reference to "OP?"

Thanks.

Original Poster, in this case you.
Jeyram Tamate
Greenhorn

Joined: Sep 29, 2012
Posts: 9
Thanks Kemal Sokolovic
for telling me about how this forum works.
Actually I am entrant in this forum kind of thing. But I can tell with only two days experience this "Big Moose Saloon" rocks.
D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
Ok, so I read the tutorial on "Concurrency in Swing." Also the prerequisite tutorials, "Concurrency." I'm not sure I quite ever found a sentence that said, "Your program won't work because..." but I see that if I want anything to change in Swing, say with an animation, I will need to run it in a thread. So I now have a thread for calculating prime numbers and appending them to the JTextArea. But there are some strange things happening with it.

!. If I initialize boolean variable flipper as "true," the program launches, calculating primes right out of the gate. But I want the user to start with flipper = false, so that the user will start the program generating primes by clicking on the JButton. But if I initialize flipper as "false," I don't see any primes being generated. I can't figure out why not.

2. If I compile the program in Eclipse, it runs OK. If I click on the button, the generation of prime numbers stops until I click the button again. Then the process picks up where it left off. That's what I want. But if I compile the program in NetBeans, I get a different behavior. If I stop the generation of prime, it cannot be resumed. Both are using the same JDK. Why would it be different between the two?

3. In the South Region, the line .setAlignmentX(Component.RIGHT_ALIGNMENT) for the button and the label seems to produce a left alignment, but setAlignmentX(Component.RIGHT_ALIGNMENT) seems to produce left alignment.

Any insight is appreciated!

Here's my code:

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1077
    
  10

This code simply won't work:
a) Darryl pointed you to the "Concurrency in Swing" tutorial. You say you have read it. But, I don't see SwingWorker anywhere in your code or even a javax.swing.Timer.
b) In the same post, Darryl talked about SwingUtilities.invokeLater when updating components. I don't see that in the code either.
c) In the same post, Darryl talked about single-thread rule. I don't think that has been understood or applied.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
perhaps better suited to a Timer, try these changes and see if your other issues continue

D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
Michael, this does run the way I want it to. Thanks. I'm still studying it to understand how it works. (I'm pretty new at this.)

I'm also still wondering two things. Why would the program behave differently if it was compiled using NetBeans vs. with Eclipse? And, why would the program run with flipper=true, but not run with flipper=false? If I come to understand your version of the program, I don't need the answers to these questions, but I am curious about them.

Thanks,
Dave

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Why would the program behave differently if it was compiled using NetBeans vs. with Eclipse?

I don't use either, so no idea on that issue.

> why would the program run with flipper=true, but not run with flipper=false?

look at the flow of the code:
in main()
PrimeFinderMain gui = new PrimeFinderMain();
gui.start();

now in start()
t = new Thread(this);
t.start();// which calls run()

and in run()
if (flipper)

so, if flipper is false, nothing will happen, and as the thread/run() is not called again,
changing flipper to true by clicking the button will not trigger the thread.

change the 'else' part of ButtonListener's actionPerformed to this and it should work
D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
Michael, thank you. This is very helpful.

Dave
 
wood burning stoves
 
subject: Repetetively Append to JTextArea?