how to make combobox modify a combobox with java desktop database items?

Asked

Viewed 295 times

0

I managed to do but only works with pre-selected items.

    CidadeDAO cidadeDAO = new CidadeDAO(connection);
    EstadoDAO estadoDao = new EstadoDAO(connection);
    List<ModeloEstado> estados = estadoDao.lista();
    jComboBoxEstado.removeAllItems();
    jComboBoxCidade.removeAllItems();

    for(ModeloEstado c : estados ){

       jComboBoxEstado.addItem(c.getNome());
   }

    jComboBoxEstado.setSelectedIndex(4);

    System.err.println(jComboBoxEstado.getSelectedItem());
    List<ModeloCidades> cidadeCombo = cidadeDAO.buscaIdPorEstado((String) jComboBoxEstado.getSelectedItem());
    for(ModeloCidades c : cidadeCombo){
        jComboBoxCidade.addItem(c.getNome());
   }

but I would like to make sure that when selecting an item in the state combobox, modify the city cobobox with the items already registered in the bank.

1 answer

0


You should add an event to the first combobox and when you select some value on it, you should call another method that searches the database for the data relating to the first one (passing a search ID or similar).

Example:

combobox1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        /* Chame aqui o método de busca no banco de dados passando o valor 
           selecionado no combobox1 e povoe o combobox2.
        */
    }
});
  • It worked here thanks so much for the help.

Browser other questions tagged

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