Empty String validation for Multiple JTextfield

后端 未结 4 1402
说谎
说谎 2020-12-19 08:02

Is there a way to validate a number of JTextfields in java without the if else structure. I have a set of 13 fields, i want an error message when no entry is given for any o

4条回答
  •  粉色の甜心
    2020-12-19 08:31

    Take an array of these three JTextField, I am giving an overview

    JTextField[] fields = new JTextField[13] 
    field[0] = firstname;
    field[1] = lastname; //then add remaining textfields
    
    for(int i = 0; i < fields.size(); ++i) {
    if(fields[i].getText().isEmpty())
       JOptionPane.showMessageDialog(null, "No data entered");
    }
    

    Correct me if i'm wrong, I am not familiar with Swing or awt.HTH :)

提交回复
热议问题