How to get the selected item from a JComboBox and in turn to list everything from that Item

0

What I need to know is how do I show the selected item in a JComboBox and in turn list everything loaded in that combo from that previously stored item, for example:

That combo belongs to a ComboBox which is written in C #, I want to do the same but in java, is that understood? So far and managed to do this only, only show the item that is previously selected stored but I do not list the other data stored in my BD which makes my JComboBox is full.

Only the data is shown if I delete that item and as I go erasing it goes listing the items that comply with the autoComplete of it.

The combo is loaded:

private DefaultComboBoxModel comboLocalidad() {
        DefaultComboBoxModel valor = new DefaultComboBoxModel();
        try {
            Connection miComando = AdministradorConfiguracion.obtenerComandoMySql();
            CallableStatement obtenerLocalidades = miComando.prepareCall("call obtener_localidades()");
            ResultSet rs = obtenerLocalidades.executeQuery();
            while (rs.next()) {
                cboLocalidad.setModel(valor);
                valor.addElement(new Localidad(rs.getInt("Nro"), rs.getString("Localidad")));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error al intentar auto completar busqueda:\n"
                    + e, "Error en la operación", JOptionPane.ERROR_MESSAGE);
        }
        return valor;
    }

To load the form data I do:

private void cargarDatos() {
        obtener(idModificado);
        cliente.setNombre(txtNombre.getText());
        cliente.setApellido(txtApellido.getText());
        cliente.setDomicilio(txtDomicilio.getText());
        cliente.setTelefono(txtTelefono.getText());
        cliente.setFacebook(txtFacebook.getText());
        Localidad cbo = (Localidad) cboLocalidad.getSelectedItem();
        int id = cbo.getIdlocalidad();
        cliente.setIdlocalidad(id);
    }

The get ()

method
public void obtener(int idCli) {
    try {
        DefaultComboBoxModel valor = new DefaultComboBoxModel();
        Connection miComando = AdministradorConfiguracion.obtenerComandoMySql();
        CallableStatement obtenerCliente = miComando.prepareCall("call obtener_cliente(?)");
        obtenerCliente.setInt(1, idCli);
        obtenerCliente.execute();
        ResultSet rs = obtenerCliente.executeQuery();
        while (rs.next()) {
            jtxtId.setText(String.valueOf(rs.getInt("idcliente")));
            txtNombre.setText(rs.getString("nombre"));
            txtApellido.setText(rs.getString("apellido"));
            txtDomicilio.setText(rs.getString("domicilio"));
            txtTelefono.setText(rs.getString("telefono"));
            txtFacebook.setText(rs.getString("facebook"));
            valor.addElement(new Localidad(rs.getInt("idlocalidad"), rs.getString("localidad")));
            cboLocalidad.setModel(valor);
        }
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Error al intentar obtener el cliente:\n"
                + e, "Error en la operación", JOptionPane.ERROR_MESSAGE);
    }
}

The autoComplete:

private void autoCompletarLocalidad(String cadBus) {
        DefaultComboBoxModel valor = new DefaultComboBoxModel();
        try {
            Connection miComando = AdministradorConfiguracion.obtenerComandoMySql();
            CallableStatement obtenerLocalidades = miComando.prepareCall("call obtener_localidades_por_cadena(?)");
            obtenerLocalidades.setString(1, cadBus);
            ResultSet rs = obtenerLocalidades.executeQuery();
            while (rs.next()) {
                valor.addElement(new Localidad(rs.getInt("Nro"), rs.getString("Localidad")));
            }
            if (valor.getSize() > 0) {
                cboLocalidad.setModel(valor);
                cboLocalidad.setSelectedItem(cadBus);
                cboLocalidad.showPopup();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error al intentar auto completar busqueda:\n"
                    + e, "Error en la operación", JOptionPane.ERROR_MESSAGE);
        }
    }

The method that makes the keyReleased:

private void cboAutoComplete() {
        final JTextField textoBuscado = (JTextField) cboLocalidad.getEditor().getEditorComponent();
        textoBuscado.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent ke) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        autoCompletarLocalidad(textoBuscado.getText());
                    }
                });
            }
        });
    }

Any idea how I can do what I want to do? And searched, researched but not given with the solution. Thanks again.

    
asked by Gerardo Ferreyra 15.05.2017 в 03:34
source

2 answers

0

As it occurs to me that you do this, it is as follows:

1) After the query to the database, go through the ResultSet but instead of filling the JComboBox directly, fill in a List (java.util.List)

2) Having the loaded list we fill the JComboBox with the data of the list

3) When selecting the value of the JComboBox we look for its position in the list and we display from its index to the end of the list

I hope you serve

Greetings.

    
answered by 15.05.2017 в 03:46
0

Well what you could do:

  • Bring your query and place it in a resultset.
  •   
  • Instance a new object at the moment of traversing it.
  •   
  • You add it to the combo
  • With the change event of your combo you capture the item you select.
  • You invoke your method to fill the dependent combo, which would be another result the only thing that changes is that you pass as parameter the id of your selected object.
  • I hope it serves you

        
    answered by 15.05.2017 в 06:55