• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to stop a thread until a ActionEvent happend?  RSS feed

 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello anybody!

Today I've completly written my Vocabulary program from scratch, dividing things into different classes instead of just two.
That worked fine, but now I'm stuck at a part:
Let me describe what happens.

Program starts, creates a new thread, starts the run() of the main class, main class loads data out of textfiles into ArrayLists, for-loop starts, the gui opens and now the thing happens:

The GUI opens, but, it continues with the whole thing, opening the same gui over and over until it reaches the maximum of X that I told him to be the max before.
Now, I want the program to WAIT at the point where it is, until the user typed the text and then pushed the button to activate the ActionEvent telling the frame to either close or the panel to close and open another panel.
Here is the code, but the gui is not 100% finished. Oh and please mind that most of the words are german.




Do not forget the main problem, I want the thread to stop at the point after the GUI is set to visible and then continue after the button is pressed.
And as a side-note: Do you see places where I could improve the code? Make it smaller?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Degler wrote:Hello anybody!

Today I've completly written my Vocabulary program from scratch, dividing things into different classes instead of just two.
That worked fine, but now I'm stuck at a part:
Let me describe what happens.

Program starts, creates a new thread, starts the run() of the main class, main class loads data out of textfiles into ArrayLists, for-loop starts, the gui opens and now the thing happens:

The GUI opens, but, it continues with the whole thing, opening the same gui over and over until it reaches the maximum of X that I told him to be the max before.
Now, I want the program to WAIT at the point where it is, until the user typed the text and then pushed the button to activate the ActionEvent telling the frame to either close or the panel to close and open another panel.
Here is the code, but the gui is not 100% finished. Oh and please mind that most of the words are german.

{code cut out}

Do not forget the main problem, I want the thread to stop at the point after the GUI is set to visible and then continue after the button is pressed.
And as a side-note: Do you see places where I could improve the code? Make it smaller?


Try taking a step back and telling us, in one paragraph, what your program is supposed to do and what it does now. Specific things that puzzle me about the explanation you've written so far:

* Program starts, creates thread, starts run of the main class...

You normally create a thread other than the one you are running in when you want two things to proceed in parallel. You state that the main class loads array lists with data from text files, and I guess that's in the thread you create. What is the other thread doing?

* for-loop starts

I gather you wanted something to loop -- what is it? Do you loop once per word the user enters? Once per panel? Once per month?

* it continues with the whole thing, opening the same gui over and over

Now I'm lost -- what is 'the whole thing' you are referring to? Is opening a new gui at all something the program is supposed to do,you just want to control it?

And the logical guess doesn't make any sense to me either; why would you get rid of a panel and open a new one, even under your control?

I'm sorry, but trying to read incomplete code in German is something I'm only liable to do if I think I can tell, from your description, the general sort of things I'm looking for.

rc
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook wrote:

Try taking a step back and telling us, in one paragraph, what your program is supposed to do and what it does now. Specific things that puzzle me about the explanation you've written so far:

* Program starts, creates thread, starts run of the main class...

You normally create a thread other than the one you are running in when you want two things to proceed in parallel. You state that the main class loads array lists with data from text files, and I guess that's in the thread you create. What is the other thread doing?

The other does simply nothing. I just made a new one because I wasn't sure if laying the main thread to sleep would cause problems.

* for-loop starts

I gather you wanted something to loop -- what is it? Do you loop once per word the user enters? Once per panel? Once per month?

After all Actions have been done, the programm shall go to sleep for 5 minutes, when it wakes up again it finishes the method's and the loop starts from new.

* it continues with the whole thing, opening the same gui over and over

Now I'm lost -- what is 'the whole thing' you are referring to? Is opening a new gui at all something the program is supposed to do,you just want to control it?

It keeps reopening the same GUI because the loop finishes itself as there is no "Close gui when method is finished"

And the logical guess doesn't make any sense to me either; why would you get rid of a panel and open a neven under your control?

First I want a JTextField Panel which checks if the word is written right, if it's written wrong, the JTextField panel closes and a Panel with JRadioButton is where the other used to be before and more, but all depending on the same princip.

I'm sorry, but trying to read incomplete code in German is something I'm only liable to do if I think I can tell, from your description, the general sort of things I'm looking for.

No problem.

rc


Answers in Italic under the questions.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing I asked for was a one-paragraph summary of the program. I still have very little idea what the program is about, and none about how it goes about it. Appearing and disappearing panels? "Got to sleep for 5 minutes"?

I realize it's almost a lost art in software engineering, but what we need here is a functional description. How would you describe to a user what the program does?

rc
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook wrote:The first thing I asked for was a one-paragraph summary of the program. I still have very little idea what the program is about, and none about how it goes about it. Appearing and disappearing panels? "Got to sleep for 5 minutes"?

I realize it's almost a lost art in software engineering, but what we need here is a functional description. How would you describe to a user what the program does?

rc


Start the program. The program will start a new Thread and then it will read the Vocabulary Data out of your Text data. Then it starts a loop. Inside of the loop there will be 3 random numbers generated, that will choose which Vocabulary will be tested. r1 beeing the tested one and r2 and r3 beeing random ones out of another data. Now create a GUI, Put the german word as a Label in one panel and create another panel with a TextField with a button next to it. Now you type the right Translation into the Textfield. If the translation is right, the program will close the GUI, put the Thread into sleep mode for 5 minutes, wake up, close the current loop and then get reopened. If not, the Panel with the TextField and the Button will be removed from the Frame, and a new Panel will instead be added, having the 2 random words and the 1 right word, each with its own JRadioButton. If the User chooses the right option, see above. If its wrong, remove the JPanel with the JRadioButton and add a Panel which shows a JLabel with the right Translation, a JTextField where you have to type the word into again and a Button to check the typed in.

I tryed to make it as detailed as I could... is it enough?
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if it's allowed, but.. push?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to have a horrid hybrid which combines a GUI with one of those beginner command-line programs which ask the user for input and do things with that input.

And when you were asked to provide a description which explains to a user what the program does, you instead provided a description which explains to a programmer how it does whatever it does. Which in itself wasn't all that helpful, because it looked to me like you had a thread whose purpose was to initialize a GUI but then gets involved butting in to the normal processing of the GUI.

In other words, I can't figure out what your application is supposed to be doing, but my suspicion is that you've designed it inside-out. Normally a GUI application is driven by what the user does, like clicking the mouse and pressing the keys. It isn't driven by the application, and it looks like your thread is trying to drive the processing.

So I may be wrong, but my impression is that the whole thing should be tossed out and you should start again, this time writing an ordinary GUI application. (It's possible to user timers in one of those, by the way.) Unless you can provide a clear explanation of why what you're doing is preferable to the ordinary GUI application.
 
Manny Hernandez
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible for you to translate the German words into English?
It's really hard to debug a code that you don't know the purpose
of each variable and method.
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try to explain it for a user:

First off, you open the "Vocabulary.txt" file, and then you write your words insite of that like this: German$Spanish and do a new line after that.
Now you open the "Random.txt" file and insert all Vocabulary that you want, only the Spanish ones and all seperated by $, like this: Spanish1$Spanish2$Spanish3...
After you're finished, you open the program. It will open a GUI which shows your German word and it wants that you type in the Spanish word in the field underneath it, then press the Check button to check it. If it's right, it will dissappear, and reappear after 5 minutes giving you a new word. (Everything after this is not yet in the code, due to failure to solve the problem yet) If you however been wrong, it will show you 3 words, one of them is right, two of them are out of the Random.txt. You choose one by clicking the RadioButton and it gets checked again. If its right, it'll dissappear for 5 minutes again. If it's wrong, it will show you the right translation and you're supposed to type it in right then send it off. Now it will (finally) dissappear and return in 5 minutes.

The "return in 5 minutes" is the part where I got the Idea to use "sleep()", to put the thread to sleep and let the loop finish after 5 minutes, instantly starting it again.

But thats the point where my logic failed. Even if I program everything like I described and even if it's working, the thread finishes itself before I'm able to do anything, as the ActionListener are not valid anymore as there is nothing there to stop the thread to finish.

It's at this part:



After the addActionListener has been registered, "haupt()" which was on top of the stack, is finished and all other methods are finishing itself, not giving any chance for the ActionListener and the user to even have a Chance to do something.

Now the thing that I need is something like this:



Now a Idea I just got to put the whole GUI and actionlistener into a different class and a different thread, but first I want to know if there is any shorter solution.

Still thanks for everyone yet,

Joe Degler
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "return in 5 minutes" is the part of the design I don't understand either. What's the point of that? Why not just ask the next question immediately? Let the user decide if he wants to answer immediately or not.

And this idea of GUIs appearing and disappearing is completely non-standard too. You're designing your application as if you never saw any other applications, which surely isn't the case. As a beginner you should design your applications to work in a standard way, because it's a lot easier to do that. When you have more experience, then you can try writing applications which do weird things.
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:The "return in 5 minutes" is the part of the design I don't understand either. What's the point of that? Why not just ask the next question immediately? Let the user decide if he wants to answer immediately or not.

And this idea of GUIs appearing and disappearing is completely non-standard too. You're designing your application as if you never saw any other applications, which surely isn't the case. As a beginner you should design your applications to work in a standard way, because it's a lot easier to do that. When you have more experience, then you can try writing applications which do weird things.


Now, here is the thing:

I want them to appear over the day, not to guess them nonstop as it's non-fun in that way. Letting it like reappear every 5 minute's is not gonna annoy you.

I'm not sure why it's that hard to understand, either....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!