Results 1 to 4 of 4
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    240
    Rep Power
    3

    Default 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
    Java Code:
    JButton submitButton = new JButton("Submit");
            submitButton.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                BookingsSystem.username = usernameField.getText();
                BookingsSystem.password = passwordField.getText();
                }
            
            });
    Main .java file
    Java Code:
    LoginScreen LoginScreen = new LoginScreen();
           
           
           if (username.equals("Jenny") && password.equals("tomato")){
               Window testWindow = new Window();
               
           }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default 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:
    Java Code:
    create controller.
    create login, using the constructor to register the controller.
    The controller will have a method called, say, login() which would take the username and password as parameters.
    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.

    ** This space for rent **

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default 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.

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

    Default Re: Have java wait for user input

    Quote Originally Posted by Tolls View Post
    Something needs to "listen" on the LoginScreen.
    That something would be a controller.
    So your start up code would be something like:
    Java Code:
    create controller.
    create login, using the constructor to register the controller.
    The controller will have a method called, say, login() which would take the username and password as parameters.
    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.
    Where can i learn how to create a controller?

Similar Threads

  1. Replies: 20
    Last Post: 02-06-2013, 08:27 PM
  2. Java User-Input bug (not continuing); NOVICE
    By Shzylo in forum New To Java
    Replies: 8
    Last Post: 12-13-2012, 05:30 AM
  3. Java Date input from User,
    By Shahnawaz in forum New To Java
    Replies: 22
    Last Post: 09-07-2011, 01:20 AM
  4. Is there a way to wait for input from a button?
    By KingOfClubs in forum Networking
    Replies: 4
    Last Post: 04-21-2011, 09:14 PM
  5. Why doesnt my code wait for input?
    By Addez in forum New To Java
    Replies: 7
    Last Post: 08-19-2009, 10:25 PM

Posting Permissions

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