Results 1 to 20 of 21
- 01-27-2013, 06:36 PM #1
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
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?
-
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.
- 01-29-2013, 12:55 PM #3
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
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)
-
Re: Why won't my action listener wait for user input?
I have already told you what you should do. Please re-read my recommendations above.
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.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)
- 01-29-2013, 02:02 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 01-29-2013, 04:49 PM #6
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
Re: Why won't my action listener wait for user input?
Ah, ok. I'll try your suggestions. Thank you both for helping
- 01-31-2013, 12:45 PM #7
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
Re: Why won't my action listener wait for user input?
- 01-31-2013, 01:13 PM #8
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Why won't my action listener wait for user input?
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:
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..).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); } });
Does this make it more clear for you?Last edited by Zyril; 01-31-2013 at 01:16 PM.
- 02-05-2013, 01:01 PM #9
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
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?
- 02-05-2013, 01:43 PM #10
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
Re: Why won't my action listener wait for user input?
i.e.
The user needs a chance to change the value of textInputJava 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;
- 02-05-2013, 02:04 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-05-2013, 02:11 PM #12
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
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.
- 02-05-2013, 02:53 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-06-2013, 05:05 PM #14
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
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?
- 02-06-2013, 05:34 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-06-2013, 06:44 PM #16
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.
- 02-06-2013, 07:53 PM #17
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
Re: Why won't my action listener wait for user input?
- 02-06-2013, 07:55 PM #18
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
Re: Why won't my action listener wait for user input?
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
-
Re: Why won't my action listener wait for user input?
- 02-06-2013, 07:58 PM #20
Senior Member
- Join Date
- Jul 2012
- Posts
- 166
- Rep Power
- 1
Re: Why won't my action listener wait for user input?
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.
Similar Threads
-
Need help with action listener please!!!
By ndsmith20 in forum New To JavaReplies: 3Last Post: 01-17-2013, 02:44 AM -
Can anyone help me with an action listener?
By mdCollins1 in forum New To JavaReplies: 5Last Post: 03-21-2012, 04:07 AM -
Action-Listener with multiple Arguments? (Button Listener, specifically)
By Kevinw778 in forum AWT / SwingReplies: 2Last Post: 12-11-2011, 10:44 PM -
Action Listener
By greatmajestics in forum AWT / SwingReplies: 8Last Post: 03-25-2010, 05:39 PM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks