Page 2 of 2 FirstFirst 12
Results 21 to 21 of 21
Like Tree2Likes

Thread: Why won't my action listener wait for user input?

  1. #21
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Why won't my action listener wait for user input?

    Ok, well disregard all typing in the JTextArea. No matter what is in your JTextArea, your program will never read it so its just a display. Just like your terminal.

    So when your terminal program requires you to log in to use certain commands, it will display "Username:" Your next input in the JTextField will be where the user enters this information.

    Now that is defined, your submit JButton action listener is going to need to call a different method. A method that can handle the information that is incoming and understand the information plugged in. If your command is "login", when you press submit then your action listener called interpretInformation(String JTextField.getText()); From there, interpretInformation(String tempStringThatWeNeedToKeep){//stuff} needs to compare the entered string to see if it a known command. I recommend an array of strings, and if tempStringThatWeNeedToKeep != commandArray[i] all the way to the end then it needs to kick back saying "Yo brah, this is not right. L2EnterInfo." If it does find one, say login is the command, then it needs to call the login() method. How you go about determining how each command string that is found knows how to call the proper method is up to you. There may be an easier way to do it, but I can't think of it right now.

    No where in your code should any of the GUI objects directly call methods other than interpretInformation(). Don't forget to check to make sure that JTextField.getText() isn't null.

    The tricky part is to find a way to make that one JTextField able to take commands and arguments, I would probably use a program state to determine how the information knows where it's supposed to go properly. This gets more complex as you add more and more dynamic commands in. I don't know how involved your program is going to become, but your syntax on how information is entered in the JTextField is a huge factor in separating command data. I'll leave you to figure out how you want to handle this complex issue in your own way.

    That's how I would do it, and it makes things a bit more complicated by doing it in gui but after thinking about what you want I don't think it's as difficult as your first idea.

    I hope this helped clear anything up or pointed you in the right direction. Sometimes I start typing and get sidetracked from my original thought, so hopefully that didn't happen here.
    Last edited by Dark; 02-06-2013 at 09:35 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Need help with action listener please!!!
    By ndsmith20 in forum New To Java
    Replies: 3
    Last Post: 01-17-2013, 03:44 AM
  2. Can anyone help me with an action listener?
    By mdCollins1 in forum New To Java
    Replies: 5
    Last Post: 03-21-2012, 05:07 AM
  3. Replies: 2
    Last Post: 12-11-2011, 11:44 PM
  4. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •