I have the following problem, when selecting a row of my JTable to modify its data, I call a form whose data of the table is filled in the fields of the same, inside the form there is a JCombobox that is filled with the stored data of the table of my BD, my question is, How can I show the string of said id stored in the table of my database in the combo? Because I get the id number, that is, it shows me the id number inside the combo and what I want to show me the string associated with that id.
The combo is loaded in this way.
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;
}
The locality class.
public class Localidad{
public int idlocalidad;
public String localidad;
public Localidad(int idlocalidad, String localidad) {
this.idlocalidad = idlocalidad;
this.localidad = localidad;
}
////////////////////////////////////////////////////
public int getIdlocalidad() {
return idlocalidad;
}
public void setIdlocalidad(int idlocalidad) {
this.idlocalidad = idlocalidad;
}
public String getLocalidad() {
return localidad;
}
public void setLocalidad(String localidad) {
this.localidad = localidad;
}
@Override
public String toString(){
return localidad;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (!(obj instanceof Localidad)) {
return false;
}
Localidad localidad = (Localidad) obj;
if (idlocalidad != localidad.idlocalidad) {
return false;
}
if (this.localidad != null ? !this.localidad.equals(localidad.localidad) : localidad.localidad != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + (this.localidad != null ? this.localidad.hashCode() : 0);
hash = 89 * hash + this.idlocalidad;
return hash;
}
}
I fill in the form data.
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());
cliente.setIdlocalidad(Integer.parseInt(cboLocalidad.getSelectedItem().toString()));
}
The method get ()
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"));
cboLocalidad.setSelectedItem(rs.getInt("idlocalidad"));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error al intentar obtener el cliente:\n"
+ e, "Error en la operación", JOptionPane.ERROR_MESSAGE);
}
}
Which is responsible for executing the MySQL stored procedure, which is:
CREATE DEFINER='root'@'localhost' PROCEDURE 'obtener_cliente'(IN id int)
SELECT idcliente,
nombre,
apellido,
domicilio,
telefono,
facebook,
idlocalidad
FROM Cliente
WHERE idcliente= id
When loading the form with all the corresponding fields, the JComboBox is displayed as such.
I hope you made me understand. I would greatly appreciate your help. Thanks