Java Spring 2006 - HW6 FAQ

Click on a question to expand it (for the details).
Click on the question title again to hide the details.

+ Do they have to press Enter in textfield?

Question:

Is it OK if our program assumes the user hits the Enter key after changing each text field?


Answer:

Yes, although it's not difficult to receive events each time a change is made by the user (check out the DocumentListener interface).

+ How do I get rid of the window and the GUI thread?

Question:

Once the user presses the done button, I want the window to disappear and the GUI thread to stop - how do I do this?


Answer:

Call the dispose() method on your JFrame. Note this is actually a java.awt.Window method, it releases the resources in use by the JFrame and if it's the late GUI component can shut down the GUI thread (the docs say it can, it does this for me).

NOte that you don't want to call dispose from within the GUI thread (so don't call it from your actionHandler!).

+ How do I wait?

Question:

My edit() method is not supposed to return until the user presses the done button, how do I do this?


Answer:

You need a loop that waits until the user has pressed done (has indicated they are done changing the fields). You do something like this at the end of your edit() method:

while (done==false) {
}

Assuming the actionhandler for the button can set the variable done to true, this would work. However, this is bad because your thread will spend lots of CPU time going through the loop over and over (this is called busy waiting). The following would be much better:

while (done==false) {
	Thread.sleep(500);
}

This makes the loop take 500ms each time, so you won't be using up all the CPU.