Obtaining data from JCombobox

0

I'm generating a form where I need to get some value from a Jcombobox Currently, this is my code:

private void llenarMatriz() {
    String cmb = "SELECT DISTINCT nombre_matriz FROM matriz ORDER BY nombre_matriz ASC";
      try {
        Statement st = cn.createStatement();
        ResultSet set = st.executeQuery(cmb);
           while (set.next()) {
            cmbMatriz.addItem(set.getString("nombre_matriz"));
        } // cn.close();
    } catch (SQLException e) {
        System.err.println("error consulta");
    }
}

so I need help in being able to take the FIRST WORD of the combobox that is: "water", "sediments", "weaving"

    
asked by Juan Pablo 25.10.2018 в 16:11
source

1 answer

1

I know a possible solution, unfortunately it is a bit long, but if it works for you, perfect. To begin, you need a class that includes two values, the point is that you create a list with the word that starts, example: "Seawater" and its value "Water".

public class Tipo {
    private String nombre, valor;

    public Tipo(String nombre, String valor) {
        this.nombre = nombre;
        this.valor = valor;
    }
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    public String getValor() {
        return valor;
    }
    public void setValor(String valor) {
        this.valor = valor;
    }
    @Override
    public String toString() {
        return nombre;
    }
}

The important thing here is that the toString () method only shows the value you want to show inside the JComboBox, so you can create several objects I recommend using an ArrayList and enter it with a for the JcomboBox

public class Valor extends JFrame {

    private JComboBox<Tipo> cbCaja; //Tiene que ser del mismo tipo que tu clase
    private JButton btnBoton;//El botón es solo para crear un evento
    private ArrayList<Tipo> lista; //La lista también debe ser del tipo de tu clase

    public Valor() throws HeadlessException {
        setSize(100, 200);
        setDefaultCloseOperation(3);
        setLocationRelativeTo(null);
        setLayout(null);

        //Aquí creo los objetos, pero en tu caso los puedes crear con Query
        lista = new ArrayList<>();
        lista.add(new Tipo("Agua de Mar", "Agua"));
        lista.add(new Tipo("Sedimiento Marino", "Sedimiento"));
        lista.add(new Tipo("Tejido Biologico", "Tejido"));

        cbCaja = new JComboBox<>();
        cbCaja.setBounds(10, 10, 100, 20);//Se agregan al combo
        for (int i = 0; i < lista.size(); i++) {
            cbCaja.addItem(lista.get(i));
        }

        btnBoton = new JButton("Boton");
        btnBoton.setBounds(10, 100, 80, 20);
        btnBoton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
//En el evento obtengo el objeto seleccionado
                Tipo seleccionado = (Tipo) cbCaja.getSelectedItem();
//Para llamar el valor solo invocas el metodo .getValor()
                JOptionPane.showMessageDialog(null, ""
                        + "El nombre: " + seleccionado.getNombre() + "\n"
                        + "El valor: " + seleccionado.getValor());
            }
        });
        add(btnBoton);
        add(cbCaja);
        setVisible(true);
    }

    public static void main(String[] args) {
        Valor v = new Valor();
    }
}

I know it's a bit extensive, but it's functional. Greetings

    
answered by 26.10.2018 / 04:13
source