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 ()
methodpublic 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.