Results 1 to 5 of 5
  1. #1
    Rogue45 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default 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);
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Validate editable combo box input as integer and not empty

    How about checking the length of the String value of the selected item.

  3. #3
    Rogue45 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default 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);
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Rogue45 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default 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 :)

Similar Threads

  1. How validate input against abstract class?
    By FadedAura in forum New To Java
    Replies: 1
    Last Post: 12-03-2011, 04:53 PM
  2. Editable combo box that adds new entries
    By monica in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2011, 09:01 PM
  3. How do I validate user input from forms with Java?
    By rickywh in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 06:49 AM
  4. validate time input format
    By Alairmon in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 07:39 AM
  5. Creating a non-editable combo box
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •