Reputation: 29
I am not able to check whether the JComboBox
is empty. The below code is not working.
if (t_m_priority.getSelectedItem() != null &&
t_m_priority.getSelectedItem().equals("SELECT")){
msg = "Triage Time Is Not Entered";
saveflag = false;
}
Upvotes: 1
Views: 6789
Reputation: 324207
Instead of adding the dummy item "Select" to the combo box, use a custom renderer to display "Select" when no item is actually selected. Then you can just check if the selected index of the combo box is not -1.
Check out Combo Box Prompt for a renderer that implements this functionality for you.
Upvotes: 0
Reputation: 774
JComboBox.getItemCount()
If this method returns 0, the component is empty.
if (t_m_priority.getItemCount() == 0) {
msg = "Triage Time Is Not Entered";
saveflag = false;
}
UPDATE: Try this to check if no item is selected in the combo box.
if(t_m_priority.getSelectedItem() == null ) {
msg = "Triage Time Is Not Entered";
saveflag = false;
}
Upvotes: 0
Reputation: 347332
JComboBox#getSelectedItem
will return null
when no items are selected
You could use something like...
if (t_m_priority.getSelectedItem() != null &&
t_m_priority.getSelectedItem().equals("SELECT")) {
To first check to see if the anything is selected and then test to see what the selected item actually is...
Upvotes: 3