1

I am making a GUI, in which I am using a number of JTextfield instances to enter the input from the user. I want to specify that this particular (for example : user name) text field is mandatory to fill. How can I do this?

 JLabel label_2 = new JLabel("User Name");
            label_2.setBounds(23, 167, 126, 21);
            panel_1.add(label_2);

    textField_2 = new JTextField();
            textField_2.setColumns(10);
            textField_2.setBounds(178, 129, 210, 20);
        panel_1.add(textField_2);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2845399
  • 101
  • 2
  • 10
  • i guess you will need to go through the mandatory fields on submit-action and check if they contain values – JohnnyAW Oct 15 '13 at 10:32
  • How you are processing values entered in all text fields? – nullptr Oct 15 '13 at 10:33
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 3) What was the purpose of the blank image in the question? – Andrew Thompson Oct 15 '13 at 10:35

3 Answers3

2

you can also use JFormattedTextField http://docs.oracle.com/javase/7/docs/api/javax/swing/JFormattedTextField.html

Tom
  • 1,203
  • 4
  • 21
  • 36
1

When the user is done with the textfield, for example when he submits the data, you can validate the textfield to see if it's empty.

For example, if you are using a submit button.

submitButton.addActionLister(new ActionListner(){
    public void actionPerformed(ActionEvent ae){
       if(textField.getText().length() == 0){
            //notify user that mandatory field is empty.
       }

       //other validation checks.
    }  
}

OR

You can add a focus listener to the textfields. And every field can have different focus lost implementations depending on the constraint.

 textfield.addFocusListener(new FocusListener(){
           @Override
          public void focusGained(FocusEvent fe) {
             // do whatever want like highlighting the field
          }

          @Override
          public void focusLost(FocusEvent fe) {
              //check for constraint as the user is done with this field.
          }
 });
Sorter
  • 9,704
  • 6
  • 64
  • 74
0
JLabel label_2 = new JLabel("User Name");
label_2.setBounds(23, 167, 126, 21);
panel_1.add(label_2);

textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(178, 129, 210, 20);
panel_1.add(textField_2);

submit.addActionListener(this);

}

  public void actionPerformed(ActionEvent e)
   {
      //check the text field
       if(textField_2.getText().length()==0)
              {
                   //user_name not set
              }
       else
              {
               //user_name is set
              }

   }
Dev
  • 3,410
  • 4
  • 17
  • 16