If have a java program with 4 JTextField. In my JButton add, the event code goes like this:
add.addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
try {
String add = "insert into person (firstName,middleName,familyName,age) values ('"+inputs[0].getText()+"','"+inputs[1].getText()+"','"+inputs[2].getText()+"',"+inputs[3].getText()+")";
st.execute(add); //Execute the add sql
Integer.parseInt(inputs[3].getText()); //Convert JTextField Age in to INTEGER
JOptionPane.showMessageDialog(null,"Item Successfully Added","Confirmation", JOptionPane.INFORMATION_MESSAGE);
}catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"Please enter an integer on the Field AGE","Error Input", JOptionPane.ERROR_MESSAGE);
}catch (Exception ei) {
JOptionPane.showMessageDialog(null,"Failure to Add Item. Please Enter a number on the Field AGE","Error Input", JOptionPane.ERROR_MESSAGE);
}
}
}
);
The code above is 100% working, it detects if the field "Age" is not an integer or if it is empty. Now the problem is I want to detect also if the other fields are empty which means all fields must have an inputs, if one field is empty a JOptionPane message will display "All Fields must be filled up". I've already tried using the if condition like this
add.addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
if (inputs[0].getText() == "" || inputs[1].getText() == "" || inputs[2].getText() == "")
JOptionPane.showMessageDialog(null,"Fill up all the Fields","Error Input", JOptionPane.ERROR_MESSAGE);
else
try {
String add = "insert into person (firstName,middleName,familyName,age) values ('"+inputs[0].getText()+"','"+inputs[1].getText()+"','"+inputs[2].getText()+"',"+inputs[3].getText()+")";
st.execute(add); //Execute the add sql
Integer.parseInt(inputs[3].getText()); //Convert JTextField Age in to INTEGER
JOptionPane.showMessageDialog(null,"Item Successfully Added","Confirmation", JOptionPane.INFORMATION_MESSAGE);
}catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"Please enter an integer on the Field AGE","Error Input", JOptionPane.ERROR_MESSAGE);
}catch (Exception ei) {
JOptionPane.showMessageDialog(null,"Failure to Add Item. Please Enter a number on the Field AGE","Error Input", JOptionPane.ERROR_MESSAGE);
}
}
}
);
but it didn't work. Even if the other fields are empty it will still save. I just want to know what is the problem of my if condition.