Aki K
Aki K

Reputation: 1262

How to check if a selected item from an editable Combobox is empty

I have an editable ComboBox where the user can select a database or type the name of a new one and press Enter or the button Create to create it.

What I have found is a strange behavior of the Editable ComboBox, if say there was something selected in the combobox like this:

1

and then the user deletes it like this:

2

and presses the Enter key or the Create button, then this code is not enough to check if there is no selected item in the ComboBox:

if (jDatabaseComboBox.getSelectedItem() == null) {
    return;
}

So I opted to check it like this:

if (jDatabaseComboBox.getSelectedItem() == null
       || jDatabaseComboBox.getSelectedItem().toString().isEmpty()) {
    return;
}

My question is: is my way of checking the selected item cheap and is there a more elegant way?


Here is how I did it in the end:

ComboBox and Create button

private void jDatabaseComboBoxActionPerformed(java.awt.event.ActionEvent evt)                                                  
    {                                                      
        final String selectedDatabaseName = jDatabaseComboBox.getSelectedItem().
                toString().trim();
        if (selectedDatabaseName.isEmpty()) {
            return;
        }

        if (databaseAlreadyExistsInServer(selectedDatabaseName)) {
            _currentDatabase = new SQLDatabase(selectedDatabaseName);
            updateTableComboBoxes();
        } else {
            createANewDatabase(selectedDatabaseName);
            updateDatabaseComboBoxes();
        }
        System.out.println();
    } 

Delete Button

private void jDeleteDatabaseButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                      
        final String selectedDatabaseName = jDatabaseComboBox.getSelectedItem().
                toString().trim();

        if (selectedDatabaseName.isEmpty()) {
            return;
        }

        if (!databaseAlreadyExistsInServer(selectedDatabaseName)) {
            return;
        }

        deleteDatabase(selectedDatabaseName);
        updateDatabaseComboBoxes();
        System.out.println();
    }      

I don't check for null anymore because I have it so that if the comboBoxes are empty then they will be grayed out.

Upvotes: 2

Views: 8042

Answers (2)

Braj
Braj

Reputation: 46841

A better way:

comboBox.getSelectedIndex()

it will return -1 if the value is empty or null or not valid(matched) value.

Find the sample code here Editable ComboBox


--EDIT--

Delete button is clicked

if (jDatabaseComboBox.getSelectedIndex() == -1) {
    System.out.println("Please select a database name.");
} else {
    System.out.println("database name is deleted successfully.");
}

Upvotes: 2

bgth
bgth

Reputation: 460

add trim to your check

 jDatabaseComboBox.getSelectedItem().toString().trim().isEmpty() 

to check for only spaces as input. Also try using StringUtils of lang of apache. You can replace the whole line with just one line

if(StringUtils.isEmpty(jDatabaseComboBox.getSelectedItem()))

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

Upvotes: 1

Related Questions