Re: Why am I confused on this?
R. Vince wrote On 06/12/07 15:44,:
I have an ArrayList of questions (as Strings). I simply want to step through
the arraylist, posit the question to the user (in a Swing interface), and
after he answers, move on to the next question.
Why am I so confused with this? Can someone share with me how they might do
this? Would you use one of the SwingUtilities methods
(invokeAndWait(someRunnable) or invokeLater(someRunnable)) ?
I'm just struggling with the flow of control on this (seemingly) simple
problem. Thanks, R.Vince
This could be done in a very simple-minded way, or
(if warranted) could be made quite elaborate. I'll
describe a simple-minded approach, adequate (probably)
for what you've described but perhaps not as extensible
or flexible as some higher-octane methods.
First, load your list of questions from whatever
source, and create an Iterator for the collection.
Next, build the GUI: A JFrame or JApplet containing
JPanels, assorted buttons and other doodads, and a gadget
to display the questions -- a JTextArea, perhaps, although
it could be something else. To initialize the question
area, retrieve the first String from the Iterator. Set
up listeners to monitor the buttons and so on through
which the user will answer the questions. Pack the GUI
and make it visible. Orthodoxy says that all this should
be done on Swing's Event Dispatch Thread; invokeLater()
is one way to get that to happen.
When the user presses the "Next Question" button or
takes whatever the action is that prompts you to move
onward, the listener for that action should get control.
It uses the Iterator's hasNext() method to learn whether
there are more questions, and if there are it calls the
next() method to get the next one, which it then uses to
replace the previous contents of the question area. When
there are no more questions, it can fill the display area
with "TGIF: Go Home" and maybe disable most of the buttons
and things. Since the original listener is called on the
EDT, everything it does or that it calls is also executing
on the EDT and no special provisions need be made.
You haven't said what you want to do with the answers
the user provides. If I were you, I think I wouldn't just
store a bare list of question texts. Rather, I'd make an
object that held a question and the user's answer, and
maybe some other useful information about the Q-and-A pair
(e.g., the correct answer, for scoring and/or feedback).
There's a start, albeit a simplistic one. It can be
elaborated and improved -- and obfuscated -- almost without