Java - BUSQUEDA EN COMBOBOX

 
Vista:

BUSQUEDA EN COMBOBOX

Publicado por ANTONIO GZZ (3 intervenciones) el 03/04/2020 08:44:04
ENCONTRE ESTA DESCRIPCION EN OTRO FORO DE AYUDA, SIN EMBARGO NO ENCONTRE RESUPUESTAS, ES EL MISMO PROBLEMA QUE PRESENTO EN LA IMPLEMNTACION DE MI CRUD.

Bueno tengo una duda, estoy tratando pero no puedo quisiera que me ayuden, tengo un formulario registrar producto con su respectivo ComboBox para seleccionar su categoria y tambien tengo un boton buscar que si a la hora de llenar una caja de texto que contiene el codigo de producto presiono el boton buscar me carguen las demas cajas de texto que son su nombre precio stock, pero tambien quiero que cambie el ComboBox a la cual pertenece pero no me sale, ayudaa:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Connection conn = null;
try{
    conn = Conexion.getConexion();
    ProductoDao prodao = new ProductoDao(conn);
    List<Producto> lista = prodao.obtenerLista();
    boolean condicion = true;
    for (int i = 0; i < lista.size(); i++) {
        if(String.valueOf(lista.get(i).getIdproducto()).equals(txtcodigo.getText())){
            txtnombre.setText(lista.get(i).getNombre());
            txtstock.setText(String.valueOf(lista.get(i).getStock()));
            txtprecio.setText(String.valueOf(lista.get(i).getPrecio()));
            itemcategoria.setSelectedItem(lista.get(i).getCategoria());
 
           condicion = false;
        }
    }
 
    if(condicion){
       JOptionPane.showMessageDialog(this, "CODIGO NO ENCONTRADO", "MENSAJE", JOptionPane.ERROR_MESSAGE);
    }
 
}catch(Exception ex){
    ex.getMessage();
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

BUSQUEDA EN COMBOBOX

Publicado por Costero (148 intervenciones) el 03/04/2020 19:39:47
Que tipo de object getCategoria() retorna?. Un String?.

Abajo una demostracion que "setSelectedItem" funciona pero son Strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import javax.swing.*;
 
public class ComboBoxExample {
    JFrame f;
    JComboBox cb;
 
    ComboBoxExample() {
        f = new JFrame("ComboBox Example");
        String country[] = {"India", "Aus", "U.S.A", "England", "Newzealand"};
        cb = new JComboBox(country);
        cb.setBounds(50, 50, 200, 20);
        f.add(cb);
        f.setLayout(null);
        f.setSize(400, 500);
        f.setVisible(true);
    }
 
    public static void main(String[] args) {
        ComboBoxExample comboBoxExample = new ComboBoxExample();
        comboBoxExample.cb.setSelectedItem("Aus");
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

BUSQUEDA EN COMBOBOX

Publicado por ANTONIO GZZ (3 intervenciones) el 03/04/2020 21:16:46
ES DE TIPO INT.

ESTE ES MI CODIGO DE BUSCAR POR ID:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public TCliente buscarID(String id) {
    TCliente cte = new TCliente();
    Cliente btn = new Cliente();
    String sql = "SELECT * FROM CLIENTE WHERE idCliente = ?";
    try {
        con = cn.Conectar();
        ps = con.prepareStatement(sql);
        ps.setString(1, id);
        rs = ps.executeQuery();
        if (rs.next()) {
            cte.setIdCliente(rs.getInt(1));
            cte.setNombre(rs.getString(2));
            cte.setaPaterno(rs.getString(3));
            cte.setaMaterno(rs.getString(4));
            cte.setSexo(rs.getString(5));
            cte.setfNacimiento(rs.getString(6));
            cte.setTelefono(rs.getString(7));
            cte.setEmail(rs.getString(8));
            cte.setTipo(rs.getInt(9));
        } else {
            JOptionPane.showMessageDialog(null, "No existe información registrada con esa clave", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (Exception e) {
 
    }
    return cte;
}

ESTE ES EL CODIGO DE LA VISTA:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private void btnbuscarActionPerformed(java.awt.event.ActionEvent evt) {
    String id = txtid.getText();
    if (txtid.getText().equals("")) {
        JOptionPane.showMessageDialog(null, "Debe ingresar una clave", "Mensaje", JOptionPane.WARNING_MESSAGE);
    } else {
        TCliente cte = dao.buscarID(id);
        txtnombre.setText(cte.getNombre());
        txtpaterno.setText(cte.getaPaterno());
        txtmaterno.setText(cte.getaMaterno());
        String sexo = cte.getSexo();
        if ("M".equals(sexo)) {
            rdmasculino.setSelected(true);
        } else if ("F".equals(sexo)) {
            rdfemenino.setSelected(true);
        }
        txtnacimiento.setText(cte.getfNacimiento());
        txttelefono.setText(cte.getTelefono());
        txtemail.setText(cte.getEmail());
        //AQUI FALTA POR AGREAGAR EL COMBOBOX
        if (txtnombre.getText().equals("")) {
            btnguardar.setEnabled(true);
            btneditar.setEnabled(false);
            btneliminar.setEnabled(false);
        } else {
            btnguardar.setEnabled(false);
            btneditar.setEnabled(true);
            btneliminar.setEnabled(true);
        }
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar