0

everyone, I am writing an interactive graphical user interface program. basically it accepts all inputs and display information on TextArea below. but it will validate all inputs if it is empty or null. My question is that, for example, first name is empty, how can it loop back at textField (first name field)? and retype again without further going ?

I was using a do while loop, but it only works when I use JOptionPane windows to pop out. let me clarify my question: What I want is that when the first name field is null or empty, the user needs to retype again from textfield instead of a window, I don't want "JOPtionPane.showmessageDialog " window coming out Thanks

image is here

graphic interface

do while loop

do{
        whetherContinue=true;

        FName=TxtFname.getText();
        if(TxtFname.getText().isEmpty()){
            FName = JOptionPane.showInputDialog("Please type your First Name");
            TxtFname.setText(FName);
        }
        else{

        }
        if (FName==null)
            System.exit(1);
    try{
        if (FName.isEmpty())
            throw new ArithmeticException("First name can not be empty!");
        whetherContinue = false;
    }catch (Exception ex) 
    {
        JOptionPane.showMessageDialog(null,"The first name can not be empty! Please type again");
    }
    }while(whetherContinue); //First Name while
Jason.Bao
  • 31
  • 1
  • 4
  • 1
    Can you clarify your question ? – Romain Hippeau Jul 26 '16 at 15:24
  • We hope you post your codes and images here directly for easy surfing. – Young Emil Jul 26 '16 at 15:47
  • 1
    question in this form isn't answerable here, for more informations to read FAQ, especially the part about MCVE – mKorbel Jul 26 '16 at 15:58
  • sorry for any confusion here, I posted my code here, But I don't know why you have to click to view it. – Jason.Bao Jul 26 '16 at 17:30
  • let me clarify my question: What I want is that when the first name field is null or empty, the user needs to retype again from textfield instead of a window, I don't want "JOPtionPane.showmessageDialog " window coming out. – Jason.Bao Jul 26 '16 at 17:35

2 Answers2

0

try the focus listner for the empty input problem when the focus is lost from your component check it's text i the text is empty than go back to the same component by requestFocus method

 FocusListener focus_event = new FocusListener() {

            @Override
            public void focusLost(FocusEvent arg0) {
                if(textField.getText().isEmpty())
                {
                    JOptionPane.showConfirmDialog(null, "Please enter","erruer",JOptionPane.ERROR_MESSAGE);
                    textField.requestFocus();
                }
            }

            @Override
            public void focusGained(FocusEvent arg0) {
                // TODO Auto-generated method stub

            }
        };
        textField.addFocusListener(focus_event);
Charif DZ
  • 14,415
  • 3
  • 21
  • 40
-1

Try this method, it prevents the user from entering non-numerical characters as they type. You capture user's input from keyboard, like this if you are taking numbers:

JTextField textField = new JTextField(10);
textField.addKeyListener(new KeyAdapter() {
   public void keyTyped(KeyEvent e) {
      char c = e.getKeyChar();
      if ( ((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
         e.consume();  // ignore event
      }
   }
});

Based on this approach, you can do for characters that you want.

OR change your JTextField with a JFormattedTextField, like this:

try {
   MaskFormatter mascara = new MaskFormatter("##.##");
   JFormattedTextField textField = new JFormattedTextField(mascara);
   textField.setValue(new Float("12.34"));
} catch (Exception e) {
   ...
}

Here is a reference with other approaches as well.

Community
  • 1
  • 1
Young Emil
  • 2,220
  • 2
  • 26
  • 37
  • Consider using a custom [DocumentFilter](https://docs.oracle.com/javase/7/docs/api/javax/swing/text/DocumentFilter.html) or a [JFormattedTextField](https://docs.oracle.com/javase/8/docs/api/javax/swing/JFormattedTextField.html) - KeyListener won't handle things like paste actions and/or dnd – copeg Jul 26 '16 at 16:36
  • That is very true but I added a reference with other approaches – Young Emil Jul 26 '16 at 16:44
  • Maybe I would have to add that up there. – Young Emil Jul 26 '16 at 17:00