comboGender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female" }));
comboCivilStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Single", "Married", "Widow / Widower", "Divorced" }));
if(txtAddress.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "INVALID ADDRESS");
}else if(comboGender.getSelectedItem().toString().equals("")){
JOptionPane.showMessageDialog(null, "SELECT A GENDER");
}else if(comboCivilStatus.getSelectedItem().toString().equals("")){
JOptionPane.showMessageDialog(null, "SELECT A CIVIL STATUS");
}
*just a portion of my code *
hello stackoverflow. i am new here. and i am also a novice in java programming. i seem to be having a problem that i cannot figured out how solve.
i am trying to make if and else if statements to check whether my textfields and comboboxes are empty. and then trying to return a showMessageDialog telling me to input something. i already figured out how to do the textboxes. but the combobox i cannot seem to understand how to do the syntax. can anyone please help? thank you.
by the way i am using java netbeans.
You can use JComboBox#getSelectedIndex
, which will return -1
if nothing is selected or JComboBox#getSelectedItem
which will return null
if nothing is selected.
//...
}else if(comboGender.getSelectedIndex() == -1){
//...
Take a look at How to Use Combo Boxes for more details