Waiting on a user's response (from the gui)
This should be a really simple problem, but I can't work it out.
I have two classes, my Engine and my Gui. The Engine tells the Gui what to display, the gui displays it and has methods to fire events when a user presses a button. I want to do something like the following
publicclass Engine{
Gui gui;
String[] pictures;
// ... a bunch of stuff
publicvoid showPictures{
for (int i=0; i<pictures.length; i++)
gui.showPicture(pictures[i]);
// wait on the user's response
}
}
}
Here's the question: preferably without using threading (though I could if I had to), how can I wait on a user's response from within a loop?
I considered having a boolean flag "hasResponded" in the gui, and then having a do-while loop just spinning until it saw that gui.hasResponded was equal to true, but it seems that this would add a lot of unnecessary processing load.
Any thoughts?
Thanks!>
[1383 byte] By [
Asbestosa] at [2007-10-2 20:27:36]

I don't do GUIs, but I don't see why you even need that loop.
You just have a listener that listens for a button click (or whatever form "user's response" takes), and then as part of its action, simply advances to the next picture. It's state--or the state of a class that it uses--keeps track of the list of pix and which one we're currently on. An iterator, essentially.
jverda at 2007-7-13 23:10:30 >

Hi Jverd,
The example I gave was greatly simplified. The Engine actually has to do a bunch of stuff, from writing the user's responses to a file, to decided what needs to be displayed next, to deciding which gui it needs to be working on.
The way I have the system set up now, the gui calls a method in Engine, recordResponse(xyz), and the Engine goes on from there. This ends up being really messy, however, and it would be much nicer to be able to stay within a simple loop within one method.
So question is still just the simple one above: can I (easily) wait on some event, like the gui recording an action or anything else, from within a loop?
No. The question is, who controls the process, your code or the GUI? The answer is that the GUI controls the process. So when the user clicks a button, your code has to respond to it. In the example you gave, the response is to display the next picture. And whatever other complicated stuff has to happen at the same time.
You aren't going to get anywhere fast with your loop, so it's best if you just drop the idea right now. It's only going to get in the way of the eventual solution.