Results 1 to 4 of 4
Thread: Have java wait for user input
- 02-14-2013, 02:57 PM #1
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Have java wait for user input
I am making a program which has a login screen (JTextField, JPasswordField and JButton) and then, in a separate window, the actual program once the correct credentials have been entered.
I am struggling to make java wait for input though.
The action listener on the button sets global variables "password" and username" to whatever was entered in the fields. I then have an if statement in another .java file (the main program file) after the creation of the login screen saying if the user name and password equals... ...then open a new window with the main application in it.
However, as java won't wait for the user to input data and instead is just creating the login window and then immediately after it is running the if statement, I am getting a null error as nothing has been entered, how do I overcome this?
Some of my code:
ActionListener
Main .java fileJava Code:JButton submitButton = new JButton("Submit"); submitButton.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { BookingsSystem.username = usernameField.getText(); BookingsSystem.password = passwordField.getText(); } });
Java Code:LoginScreen LoginScreen = new LoginScreen(); if (username.equals("Jenny") && password.equals("tomato")){ Window testWindow = new Window(); }
- 02-14-2013, 03:32 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Have java wait for user input
Something needs to "listen" on the LoginScreen.
That something would be a controller.
So your start up code would be something like:
The controller will have a method called, say, login() which would take the username and password as parameters.Java Code:create controller. create login, using the constructor to register the controller.
The action listener for your button will call this method.
The login() method will decide if the uname and pword are valid and, if so, tell your view to open the main app window, presumably closing the login one as well.
That's the basics, off the top of my head.Please do not ask for code as refusal often offends.
- 02-14-2013, 05:21 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Have java wait for user input
Use a modal JDialog as your login window then the code will wait until the dialog is closed.
- 02-14-2013, 06:02 PM #4
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Similar Threads
-
Why won't my action listener wait for user input?
By kkid in forum New To JavaReplies: 20Last Post: 02-06-2013, 08:27 PM -
Java User-Input bug (not continuing); NOVICE
By Shzylo in forum New To JavaReplies: 8Last Post: 12-13-2012, 05:30 AM -
Java Date input from User,
By Shahnawaz in forum New To JavaReplies: 22Last Post: 09-07-2011, 01:20 AM -
Is there a way to wait for input from a button?
By KingOfClubs in forum NetworkingReplies: 4Last Post: 04-21-2011, 09:14 PM -
Why doesnt my code wait for input?
By Addez in forum New To JavaReplies: 7Last Post: 08-19-2009, 10:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks