Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree2Likes

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

  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    I have previously made a pretty basic command line program with user input. I am now converting this to an insanely basic GUI style one (using swing) as this gives me the option to clear the output field whereas the command line just piles more text on underneath.

    I have been struggling fro a while on how to get java to wait for the user to press enter when entering text into the JTextField (variable called inputText).

    after lots of YouTube video tutorial watching I wrote (well... copied) this code:

    Java Code:
    public static void loginScreen(){
            final GUI loginScreenWindow = new GUI(400, 200);
            
            
               loginScreenWindow.printLine("Please enter your username...");
               /* Enter something to wait for enter to be pressed here - done as a test, remove "final" as well if it doesn't work*/
               loginScreenWindow.textInput.addActionListener(new ActionListener(){
                   @Override
                   public void actionPerformed(ActionEvent event){
                       username = loginScreenWindow.textInput.getText();
                       loginScreenWindow.textInput.setText("");
                   }
               });
               
               loginScreenWindow.printLine("Please enter your password...");
               loginScreenWindow.textInput.addActionListener(new ActionListener(){
                   @Override
                   public void actionPerformed(ActionEvent event){
                       password = loginScreenWindow.textInput.getText();
                       loginScreenWindow.textInput.setText("");
                   }
               });


    Java isn't waiting for the enter key to be pressed and the user has no time whatsoever to enter anything. I am pretty sure I copied a video perfectly but it didn't work for me. Is there something wrong with my code?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    You're adding two ActionListeners to the same JTextField, and they will be called one right after the other just as you've coded it, and in no predicable order, and without delay. If you want the same JTextField to accept two inputs (I really don't recommend this by the way), you will need to use *one* ActionListener, one that has a state, that knows if the user name has been entered yet, and if so then accepts the password. It would likely need a boolean perhaps named userNameEntered = false. It is initially false and in this state accepts the user name, which then sets it to true. Then the next time its run, when the boolean is true, it accepts the password text.

    Myself, I'd avoid all this foofry and would use a JTextField for user name and a JPasswordField for the password and add the ActionListener to a JButton that the user presses after entering data in both fields.

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    Oh yeah, I don't know why I have created two action listeners.

    So I can get rid of the second one?

    How do I tell java to pause until an action as each point then if I have a single AL?

    I'm sticking with a single JTextField at the moment, no password fields as I am just using this to learn and I am keeping it as simple as possible (No real world use)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by kkid View Post
    Oh yeah, I don't know why I have created two action listeners.

    So I can get rid of the second one?

    How do I tell java to pause until an action as each point then if I have a single AL?
    I have already told you what you should do. Please re-read my recommendations above.

    I'm sticking with a single JTextField at the moment, no password fields as I am just using this to learn and I am keeping it as simple as possible (No real world use)
    But your planned way to do this is actually introducing complexity. Again, I would recommend that if you want to keep this simple, you not do what you're trying to do.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

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

    You are trying to write a Swing application as if it were a console application. Write prompt, read input, write next prompt, read input.
    That's not how GUI apps work.
    They provide a form and react to events from the elements on that form.
    In your case (as Fubarable says) that should be reacting to a button, then processing the values held in two text fields.
    You could mimic your way by listening on something like focus on the single text field, then changing the prompt and storing the current value, but that way is a little silly.
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    Ah, ok. I'll try your suggestions. Thank you both for helping

  7. #7
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    Quote Originally Posted by Tolls View Post
    You are trying to write a Swing application as if it were a console application. Write prompt, read input, write next prompt, read input.
    That's not how GUI apps work.
    So instead of starting at the top and going down, doing each bit of code as it is read. Swing applications can read some parts and only react to them later when prompted (e.g. with an action listener on a button etc...)? Is this what you mean?

  8. #8
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

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

    Quote Originally Posted by kkid View Post
    So instead of starting at the top and going down, doing each bit of code as it is read. Swing applications can read some parts and only react to them later when prompted (e.g. with an action listener on a button etc...)? Is this what you mean?
    When you code a GUI, just focus on what the user can see. When you are done with that, you can add listeners to the fields and buttons. In this case, you would create a username and password field, and then a login-button. The listener in this case will go for the login-button, that fetches the information from the input fields and does something with it.

    Like this:

    Java Code:
    JButton okButton = new JButton("OK");
    		okButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				controller.addContact(textName.getText(), textLastname.getText(), textPhone.getText(), textAddress.getText(), textEmail.getText());
    				setVisible(false);
    			}
    		});
    In the code above, you have an OK-button that has an actionListener. As you see, when it is clicked, a controller runs the method addContact. The parameters for this method are several strings that are fetched from JTextfields (textName, textLastName etc..).

    Does this make it more clear for you?
    Last edited by Zyril; 01-31-2013 at 01:16 PM.
    Tolls likes this.

  9. #9
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    Ok, so I've managed to get the action listener to work perfectly now (I think) on the button.

    My code gets the input from the JTextField, writes this to a global variable (as this variable is to be used to be assigned to username variables and password variables etc... in other methods), prints the text in a JTextArea and then clears the input JTextField ready fro more text.

    code:
    Java Code:
    inputButton.addActionListener(new ActionListener(){
                @Override
                
                public void actionPerformed(ActionEvent e) {
                textInput = textInputField.getText();
                printLine(textInput);
                textInputField.setText("");
                
                }
            
            });

    Does this look acceptable?



    My only problem now is getting the program to wait for something to be entered before continuing execution.

    As you can see from this screenshot, the program is running through without waiting for the user to enter a username or password as I have no code indicating that I want it to wait. What do I insert after my equivalent of System.out.println("Enter your password") to tell java to wait for input?

    Why won't my action listener wait for user input?-screenshot-2013-02-05-12-00-39.png

  10. #10
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    i.e.

    Java Code:
               loginScreenWindow.printLine("Please enter your username...");
               /* Enter something to wait for enter to be pressed here*/
               loginScreenWindow.textInput = username;
               
               loginScreenWindow.printLine("Please enter your password...");
               /* Enter something to wait for enter to be pressed here*/
               loginScreenWindow.textInput = password;
    The user needs a chance to change the value of textInput

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

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

    Because you are trying to write a terminal (console) in a JTextArea.
    Please, just use 2 text fields and a button.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    I don't understand.

    DO I have to re-write my entire program?

    It was initially a terminal program but I wanted to put a very basic GUI in.

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

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

    As you have been told several times now.
    Swing /= terminal.

    I have no idea what the rest of your program does, but this login page should be 2 text fields and a button.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    So replace the JTextArea with a JTextField?

    How will that allow me to pause for user input?

  15. #15
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

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

    Why do you need to pause?
    You must have used an application at some point in the past.
    How many have you seen that have a login screen without 2 fields?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    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, if you draw a gui you have to understand that the gui just sits there. It reacts, if you are now using a button and are still dead set on using a JTextArea then why are you not parsing each line to get your input and then clearing it when the button is pressed? Whether you need a separate method for this or you can correctly fit it in an action listener is based on your program.

    The easiest way to do it is with two JTextFields and a JButton, just like everyone has suggested. However this implies that your entire program is now gui, which may not be possible depending on how in depth your terminal program is. Fubarable has already told you how difficult of a task you are trying to accomplish will be, if this is the only way you can do it then you must explain to us why and what your terminal program is doing. If you have 1000 terminal commands, then it would be incredibly difficult to convert that into a gui. There are reasons why people still write programs in a default terminal view.

    After posting that, I had an idea when scrolling back to the top of the page and saw your image again.

    You have a JTextArea, a JTextField, and a JButton. Why don't you make the JTextArea display the information that is pulled from the command, the JTextField to be where you input your commands, and the JButton how you submit your command?



    That would give you a console like gui while being relatively easy to accomplish. The issue is your program probably isn't designed around that structure. The action listener for Submit would have to interpret the string entered to your commands and then call the corresponding method. The method then would have to be able to dictate what happens to the JTextArea.
    Last edited by Dark; 02-06-2013 at 06:54 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.

  17. #17
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    Firstly, may I just say that your response was fantastic, thank you for the time and effort you put into typing it and making the image for it.


    Quote Originally Posted by Dark View Post
    You have a JTextArea, a JTextField, and a JButton. Why don't you make the JTextArea display the information that is pulled from the command, the JTextField to be where you input your commands, and the JButton how you submit your command?



    That would give you a console like gui while being relatively easy to accomplish. The issue is your program probably isn't designed around that structure. The action listener for Submit would have to interpret the string entered to your commands and then call the corresponding method. The method then would have to be able to dictate what happens to the JTextArea.
    I thought this was exactly what I do have. Using my own methods to emulate a terminal.

  18. #18
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    Quote Originally Posted by Tolls View Post
    Why do you need to pause?
    You must have used an application at some point in the past.
    How many have you seen that have a login screen without 2 fields?
    I just do for the time being, please forget about it being a login screen.
    I am making this program to learn from mainly, the purpose of it being a login screen is really just a bad example for me to have used.
    I am just learning how to accept user input for naming variables as a program runs really.

    I know how to do this with a scanner when in terminal programs (having the scanner search for the next line or next string). Ideally I want this exact same thing but for a JTextField

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by kkid View Post
    I thought this was exactly what I do have. Using my own methods to emulate a terminal.
    You can do this, but again it will ultimately be more complex and difficult from the solution recommended by Tolls and myself. Trust me on this.

  20. #20
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

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

    Quote Originally Posted by Fubarable View Post
    You can do this, but again it will ultimately be more complex and difficult from the solution recommended by Tolls and myself. Trust me on this.
    I would do exactly what you and Tolls said if I was creating a login screen purely, it is perfect for that scenario, as I said in my last post (which I don't think you would have seen because we posted at the same time), it being a login screen is just a bad example really.

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with action listener please!!!
    By ndsmith20 in forum New To Java
    Replies: 3
    Last Post: 01-17-2013, 02: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, 04:07 AM
  3. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  4. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 05:39 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08: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
  •