Obtain the code (primary key) of a combobox

0

I would like to know how to obtain the code of a combobox at the time of registering, and keep it in the database with its corresponding code, not the description. This is the code that filled the combos:

//METODO PARA CARGAR EL COMBOBOX "cboNivelArchivo"
@SuppressWarnings({ "unchecked", "rawtypes" })
void CargarComboNivelArchivo() {
    Connection cn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
        combo = new DefaultComboBoxModel();
        cn = new MySqlConexion().getConectar();
        String sql = "select * from TB_NIVEL_ARCHIVO_SA";
        pstm = cn.prepareStatement(sql);
        rs = pstm.executeQuery();
        while (rs.next()) {
            combo.addElement(rs.getString(2));
        }
        cboNivelArchivo.removeAllItems();
        cboNivelArchivo.setModel(combo);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (rs != null)
                rs.close();
            if (pstm != null)
                pstm.close();
            if (cn != null)
                cn.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
}
    
asked by Bruno 09.11.2016 в 19:04
source

1 answer

1

To work with more than one value within a JComboBox I recommend that you make a class that contains the type of Objeto that you want to access after the selection.

public class Objeto {
    private String nombre;
    private String codigo;

    public Objeto (String nombre, String codigo) {
        this.nombre = nombre;
        this.codigo = codigo;
    }

    public String getNombre() {
        return nombre;
    }

    public String getCodigo() {
        return codigo;
    }

    @Override
    public String toString(){
      return nombre;
    }
}

Then when you want to add an object to your ComboBox

comboBox.addItem(new Objeto (rs.getString(1), rs.getString(2)));

And finally when you want to get the value (s) of what was selected in your comboBox

EDIT

Since your comboBox is of type Object when the user selects an item, you can get getSelectedItem() and save it in a variable of type Object , so you can access its properties.

Objeto object = comboBox.getSelectedItem();
String codigo = ((Objeto)object).getCodigo();
String nombre = ((Objeto)object).getNombre();

EDIT 2

To check whether or not there is an item selected in your comboBox :

if (comboBox.getSelectedItem() == null) {
    //Error
} 

EDIT 3

Since you want to have a "reference" value in your comboBox , for example "Select", I advise you to add it as an object too.

comboBox.addItem(new Objeto ("Seleccione....", "0"));

And to evaluate if this was selected (therefore it should not serve and send an alert to the user) simply verify

Objeto object = comboBox.getSelectedItem();
String codigo = ((Objeto)object).getCodigo();
String nombre = ((Objeto)object).getNombre();
if(codigo.equeals("0")){
   //error
}
    
answered by 09.11.2016 / 19:33
source