0

I am trying to get a user email to verify that it is the user. Before this I run a series of test to see if the user does need to input the email. I am struggling to create create the frame once and wait for the user to input their email into the required field.

What happens is the window is not generated and eats up all available processing power. I have tried wait() / notify() and timers but neither are doing what I need.

Is there a better way to (within a if / else block) to generate a window and wait for a user input before doing anything else?

public class Security extends JFrame{

   //Code stuff here to see if this is needed

   String email = "";
   boolean windowCreation = true;

   do{
       do{
           EventQueue.invokeLater(new Runnable() {
                public void run() {
                   try {
                       Security frame = new Security();
                       frame.setVisible(true); 
                       windowCreation = false;
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
           });
       }while(windowCreation);
   } while(!email.equals(""));

   //More code stuff here to use "email" the way I need to 
}

Here is the just of what the window is doing

 Security()
 {
    getContentPane().setLayout(null);

    textField = new JTextField();
    textField.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent key) {
            if(key.getKeyCode() == KeyEvent.VK_ENTER)
            {
                Security.email = field.getText();
            }
        }
    });
    textField.setBounds(10, 115, 415, 20);
    getContentPane().add(textField);
    textField.setColumns(10);

}
  • This is what a modal JDialog is for. Use this instead of a JFrame when you want to show a sub-window that pauses the program until the dialog window is no longer visible. Get rid of the while loops -- while this is appropriate for linear console code, this sort of construct does not belong in an event-driven program. [For example](https://stackoverflow.com/questions/8351505/how-to-make-a-modal-jframe/8351509#8351509). Also look at the code [here](https://stackoverflow.com/questions/7017063/passing-values-between-jframes/7017147#7017147) – Hovercraft Full Of Eels Jul 19 '17 at 21:26
  • @HovercraftFullOfEels are you able to explain why my original code does not work? (also thank you for showing me JDialogs solved the problem quite nicely :3 ) – Max Williams Jul 20 '17 at 16:44
  • I didn't bother running your code, but all I can say is it is code that should never be used. Again that type of while loop have no place within an event-driven world. It's a hack, and a dangerous one at that. – Hovercraft Full Of Eels Jul 20 '17 at 16:46

0 Answers0