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:
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.
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.
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.
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.
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(...).
There are no new questions, but there may be new answers.
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.
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.
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.