A button in my app should only get the text in 8 text fields and send it to a table IF all fields are filled in

前端 未结 3 451
半阙折子戏
半阙折子戏 2021-01-29 15:21

A button in my app gets all the text you enter in 8 text fields and sends it to a table. I need code so that you need to fill in ALL fields before you can send the info. How do

3条回答
  •  滥情空心
    2021-01-29 16:04

    You can check, if a field is empty by getting the text length and comparing it to zero.

    If you don't want to check every text field in one single if-statement, you could check them separately and return, if one field has an invalid value.

    if (jTextField1.getText().length() == 0) 
    {
        //Tell the user, that the first field has to be filled in.
        return;
    }
    if (jTextField2.getText().length() == 0) 
    {
        //Tell the user, that the second field has to be filled in.
        return;
    }
    //...
    

    Otherwise, you could check them all in one if-statement:

    if (jTextField1.getText().length() != 0
        && jTextField2.getText().length() != 0
        && ...) 
    {
        //Fill in the table
    }
    else
    {
        //Tell the user, that the all fields have to be filled in.
    }
    

提交回复
热议问题