Why am I confused on this?

R

R. Vince

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
 
R

RedGrittyBrick

R. Vince said:
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.

Presumably the questions are loaded from a file. If the questions are
hard-coded constants you might as well use a String[]?
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 wouldn't. Other than using invokeLater once in main() to construct a
JFrame in the EDT.
I'm just struggling with the flow of control on this (seemingly) simple
problem. Thanks, R.Vince

Well, why wouldn't you just iterate over the ArrayList (e.g. using a
foreach loop, iterator or index)

For each Q you can either use a JOptionPane, update a JLabel or do
whatever seems appropriate for your user interface.

The detail depends on whether you are checking the answers or just
recording them and whether the answers are text, yes/no, multiple choice
or something else.

I'd try to simplify the objective then construct a SSCCE in maybe fifty
lines of code and post it in comp.lang.java.help if I got stuck.
 
E

Eric Sosman

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
limit.
 
T

Tom Hawtin

R. Vince said:
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

You could use threads, but I wouldn't recommend it.

You need to think about GUI programs differently. When you are running a
GUI program, there isn't a point it has stopped in the code. It's event
based. When the pplication is idle, the GUI thread will usually be at
the same point no matter what is shown on screen. You need to maintain
the state in a non-local variable, so that even when the GUI is idle and
outside of your code the state remains.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top