How to recover combobox index from a string?

Asked

Viewed 173 times

1

I have the String for a combobox item, but when the user clicks on the table, the combobox should automatically select the category for that particular item. However, I found that there is no getIndex of function as I put below. How can I recover the index to use of this string?

private void TabelaMouseClicked(java.awt.event.MouseEvent evt) {                                    
    // Click na tabela para selecionar produto:
    if (Tabela.getSelectedRow() != -1){

        txt_descricao.setText(Tabela.getValueAt(Tabela.getSelectedRow(), 1).toString());
        cb_marca.getIndexOf("teste");

    }
} 

1 answer

4


How are you using the DefaultComboBoxModel, the method to retrieve the index of an item if it exists in the combo is by the method getIndexOf:

((DefaultComboBoxModel)seucombo.getModel()).getIndexOf(anObject);

Just pointing out that it returns -1 if the object is not found among Combobox items.

  • I do not have the entry of the item only have what it is worth example

  • Item1, Item2, Item3 - I need something like this: getIndexOf("Item2) and this should return me the number 1 that would be the entry of this item

  • @Gabrielribeiro his combo features a custom comboboxmodel?

  • I’m using the dafault model of combobox

  • @Gabrielribeiro see the edition.

  • 1

    Now it’s worked out Thank you very much

  • 1

    Google’s ranking algorithm is based on links. Therefore, I suggest only put links to Javadoc from Java 9 from now on and update the old ones too, so that everyone falls into the newest references and google puts it higher up on the old ones.

  • The link to getIndexOf is pointing to something that is not the getIndexOf

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.