Type and search in Combobox

Asked

Viewed 46 times

0

I wonder if there is a way and how to do: type and appear the names in a Combobox, is already listing the database data in the combobox, but I would like to type the initial of the name and go listing. The code:

public void listarAssociadosnoComboboxV() throws Exception {
        try {

            Cad_Veiculo.cmbfk_associadoV.addItem("SELECIONE");
            String sql = "SELECT ID_ASSOCIADO, NOME FROM ASSOCIADO";



            preparedStatement = (PreparedStatement) conexao.prepareStatement(sql);
            rs = preparedStatement.executeQuery();
            while (rs.next()) {
                  Associado a = new Associado();
                  a.setIdAssociado(rs.getInt("ID_ASSOCIADO") );
                  a.setNome( rs.getString("NOME") );
                  Cad_Veiculo.cmbfk_associadoV.addItem(a) ;        
            }
        } catch (SQLException sqlEx) {
        }finally {
            Conexao.closeConnection(conexao, preparedStatement);
        }
    }
  • I could add information in the question of how the association of the data with the combobox is taking place?

1 answer

0

  • Thinking his database might change after the first population, the best way to do what you want is whenever someone enters some character in your combobox will be made a request to the server and will search with a like 'character%' your table in the database and populate again the combobox.

Query you need to use:

SELECT ID_ASSOCIADO, NAME FROM ASSOCIATED WHERE NAME LIKE 'character%'

  • Another way is to have in Javascript a list of objects and on these filter according to the characters typed in the combobox.

Now choose the option that suits you best.

Browser other questions tagged

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