Validate editable combo box input as integer and not empty
I have an editable combo box. The user can either pick an item from the dropdown or enter a new one. I have already validated that they only enter an integer, but i can't figure out how to determine if the field is empty?? If this were a TextField it would be simple. (JtextField.isEmpty)
Base question: How do i detect editable combo box is not empty?
Please don't point me to the java tutorial page because i've already looked at it and it doesn't seem to be what i need.
Here is my code block for what i have so far:
//Check that service Id input is valid
if(serviceIDComboBox.getInputContext() == null)
{
setupInvalidServiceId.setVisible(true);
}
try
{
int num = Integer.parseInt(serviceIDComboBox.getSelectedItem ().toString());
}
catch(NumberFormatException e)
{
setupInvalidServiceId.setVisible(true);
}
Re: Validate editable combo box input as integer and not empty
How about checking the length of the String value of the selected item.
Re: Validate editable combo box input as integer and not empty
Well i got my code working, but this would still be a good question.
My code worked because i put the selected into an int. If this is a letter or if this is empty it throws NumberFormatException.
int num = Integer.parseInt(serviceIDComboBox.getSelectedItem ().toString());
Therefore If i catch that error and set a flag i can determine whether to display my error message or continue
setupInvalidServiceId.setVisible(false);
try
{
int num = Integer.parseInt(serviceIDComboBox.getSelectedItem ().toString());
}
catch(NumberFormatException e)
{
serviceIdError = true;
setupInvalidServiceId.setVisible(true);
}
Re: Validate editable combo box input as integer and not empty
Programming by exception is frowned on. There are other ways to check a valid input String. One could be by using Scanner and its method hasNextInt.
db
Re: Validate editable combo box input as integer and not empty
K I will try that and get back to you. No coding on the weekends though :)