How to select by default an element of a jcombobox that is loaded from a class?

2

I have a jcombobox that loads the regions of Chile, I use a class called Regions to also save the ID. I need to consult the data of a person to load the jcombobox (which does it without problems), but I also need to select the region of the person. I can not simply use a .setSelectedItem ("Region-name") because the combobox only receives objects from the Regions class.

The class code is this:

public class Regiones {

    public int id;
    public String nombre, num_r;

    Connection connect;


    public Regiones(int id, String nombre) {
        this.id = id;
        this.nombre = nombre;
    }

    public Regiones() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNum_r() {
        return num_r;
    }

    public void setNum_r(Integer id) {
        this.num_r = num_r;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }


    public void cargarR(JComboBox<Regiones> cbx_region){
        connect = Conexion.getConnection();
        String sql = "SELECT * FROM region WHERE ACTIVO_REGION = 'SI';";
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(sql);
            rs = ps.executeQuery();

            for (int i=0; i<=1; i++){
                if (i == 0){
                    int a = 0; String b ="", c ="Seleccionar Región";
                    cbx_region.addItem(
                        new Regiones(
                            a,
                            c
                        )
                    );
                }
                else{
                while(rs.next()){
                    cbx_region.addItem(
                            new Regiones(
                                rs.getInt("ID_REGION"),
                                rs.getString("NUM_REGION")+" - "+ rs.getString("NOMBRE_REG")
                            )
                    );
                }
            }
        }

    } catch (Exception ex) {
        Logger.getLogger(Regiones.class.getName()).log(Level.SEVERE, null, ex);
    }

}


@Override
public String toString(){

    return nombre;
}

}

And to load the combobox, the code is this:

Regiones rg = new Regiones();
rg.cargarR(cbx_region);

But I do not know how to select a default region.

    
asked by Sagara 13.01.2017 в 06:49
source

1 answer

1

To select an element of the combobox, use:

In your case you say that the second is not worth it, so you can use the first one.

Ex:

JComboBox test = new JComboBox();
test.addItem(new ComboItem(0, "Pan"));
test.addItem(new ComboItem(1, "leche"));
test.addItem(new ComboItem(2, "huevos"));
test.addItem(new ComboItem(3, "manzanas"));

//para seleccionar el de ID = 2, huevos
test.setSelectedIndex(2);

//para seleccionar el de valor "manzanas
test.setSelectedItem("manzanas");

Note: if some instruction does not correspond (ex: test.setSelectedIndex(5); // no hay index 5 en test ) the program would not fail and it would be selected the one that already was and the execution of the program would continue.

    
answered by 13.01.2017 в 08:10