Activate a combobox depending on the result of another combobox

0

I want to upload the values in this combo box depending on the selection made in another combobox.

I mean, I have an application that allows you to add a car and I would like to select the values through queries depending on the value taken in a previous combobox. That is, I make a SQL query to add in the items of this first cmbobox (brand) , and automatically load a second combobox with the values of the value of the first combobox ** (model). **

CONEXIONES.JAVA

    /**
     * Realizamos una consulta para cargar todas las marcas en el combobox_marcas de Alta_vehiculo.java.
     */
    public static ArrayList cargar_marcas() {
        ArrayList<String> marcas = new ArrayList<String>();
        String bd = Conexiones.bbdd;
        Connection c = (Connection) Conexiones.conexion_a_BBDD(bd);
        Statement stm;
        ResultSet rs;
        try {
            //Consulta para sacar todas las marcas.
            stm = c.createStatement();
            String consulta_marcas = "SELECT descripcion FROM marca;";
            rs = stm.executeQuery(consulta_marcas);
            System.out.println("CONSULTA TODAS LAS MARCAS: Mostramos todas las marcas de la tabla vehiculos.\n");
            int i = 0;
            while (rs.next()) {
                String marca = rs.getString("descripcion");
                marcas.add(marca);
                i++;           
            }
            c.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return marcas;
    }



    /**
     * Realizamos una consulta para cargar todos los modelos en el combobox_modelos de Alta_vehiculo.java.
     */
    public static ArrayList cargar_modelos() {
        ArrayList<String> modelos = new ArrayList<String>();
        String bd = Conexiones.bbdd;
        Connection c = (Connection) Conexiones.conexion_a_BBDD(bd);
        Statement stm;
        ResultSet rs;
        try {
            //Consulta para sacar todos los modelos
            stm = c.createStatement();
            String consulta_modelos = "SELECT modelo FROM vehiculos;";
            rs = stm.executeQuery(consulta_modelos);
            System.out.println("CONSULTA TODOS LOS MODELOS: Mostramos todoso los modelos de la tabla vehiculos.\n");
            int i = 0;
            while (rs.next()) {
                String modelo = rs.getString("modelo");
                modelos.add(modelo);
                System.out.println(modelo);
                i++;           
            }
            c.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return modelos;
    }

ALTA_VEHICULO.JAVA

public class Alta_vehiculo extends javax.swing.JDialog {
    public Alta_vehiculo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);

        //Declaramos un arrayList de Strings llamado "marcas".
        ArrayList<String> marcas = new ArrayList<String>();
        //Cargamos los valores de la función "cargar_marcas()" de Conexiones.java en el arrayList "marcas".
        marcas = Conexiones.cargar_marcas();
        //Creamos un iterator para recorrer el ArrayList.
        Iterator<String> i = marcas.iterator();
        //Recorremos el iterator "i".
        while(i.hasNext()){
            //Añadimos a los items cada marca.
            combobox_marca.addItem(i.next());
        }
    }
}

Well, my question is: how do I load the combobox_modelos items so that each model goes with its brand?

    
asked by omaza1990 22.12.2016 в 09:55
source

1 answer

2

Yes, but see carefully why it is executed when selecting and "deselecting"

Through the event of the ItemStateChanged method you can take the moment of the selection, I give you a simple example with two String comboBox.

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {                                            

    if (evt.getStateChange() == ItemEvent.SELECTED) 
    {
         //Aqui deberias coger el item seleccionado
         String a=(String)jComboBox1.getSelectedItem();
         //Y aquí a tu función para dar valores al comboBox2 a partir del seleccionado
         if(a.equals("SEAT"))
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "LEON","IBIZA"}));
          if(a.equals("VW"))  
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "GOLF","POLO"}));
          if(a.equals("Mercedes"))  
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Serie 1","Serie 5"}));

    }
}    

Greetings

EDIT, in the ItemStateChanged function:

  if (evt.getStateChange() == ItemEvent.SELECTED) 
    {
         String marcaSeleccionada=(String)combobox_marcas.getSelectedItem();
          //Creamos un Array de String para los modelos
          ArrayList<String> modelos=new ArrayList<String>();
          //la función obtenerModelos nos devuelva una lista de Strings con los modelos de la BBDD dada una marca
          modelos.obtenerModelos(marcaSeleccionada);   

             Iterator<String> i = modelos.iterator();
            //Borramos los datos anteriores del comboBox
            jComboBox2.removeAllItems();
            while(i.hasNext()){
                //Añadimos a los items con cada modelo.
                jComboBox2.addItem(i.next());
            }
         }
    
answered by 23.12.2016 / 09:23
source