Why am I confused on this?

Discussion in 'Java' started by R. Vince, Jun 12, 2007.

  1. R. Vince

    R. Vince Guest

    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. Vince, Jun 12, 2007
    #1
    1. Advertising

  2. 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.
     
    RedGrittyBrick, Jun 12, 2007
    #2
    1. Advertising

  3. R. Vince

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Jun 12, 2007
    #3
  4. R. Vince

    Tom Hawtin Guest

    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
     
    Tom Hawtin, Jun 13, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    905
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,974
    Smokey Grindel
    Dec 2, 2006
  3. Ross Culver

    Why is ASP Session State Confused?

    Ross Culver, Aug 14, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    374
    Ross Culver
    Aug 14, 2007
  4. Replies:
    3
    Views:
    265
  5. George2
    Replies:
    2
    Views:
    320
    Erik Wikström
    Jan 11, 2008
Loading...

Share This Page