Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why am I confused on this?

Reply
Thread Tools

Why am I confused on this?

 
 
R. Vince
Guest
Posts: n/a
 
      06-12-2007
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


 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      06-12-2007
R. Vince wrote:
> 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.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-12-2007
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      06-12-2007
R. Vince wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Still confused why working set larger than virtual memory George2 C++ 2 01-11-2008 05:46 PM
confused about why i get a type error when i call an object's method pepper@rumbalski.com Python 3 08-21-2007 06:38 PM
Why is ASP Session State Confused? Ross Culver ASP .Net 0 08-14-2007 03:19 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments