Problema con comboBox
Publicado por Juan (18 intervenciones) el 11/10/2018 18:17:14
Buenas tardes.Tengo un problema con un comboBox.Al arrancar el programa relleno un comboBox desde una consulta de una Base de datos.Hasta hay todo bien.
El caso es que quiero hacer que cuando se elige un fabricante del comboBox rellenar un label con una foto y no se con qué evento lo puedo hacer.
Os pongo una parte del código.
Si hago este evento en cuanto ejecuto el programa me viene aquí y me pone una foto sin elegir ningún fabricante.
Si pongo este otro evento me pasa lo mismo:
Alguien me puede ayudar?
Gracias.
El caso es que quiero hacer que cuando se elige un fabricante del comboBox rellenar un label con una foto y no se con qué evento lo puedo hacer.
Os pongo una parte del código.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Maquinas extends javax.swing.JDialog {
ArrayList<Fabricante> listaFabricantes = new ArrayList<Fabricante>();
public Maquinas(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
this.setLocationRelativeTo(null);
crearListaFabricantes();
}
public void crearListaFabricantes() {
this.listaFabricantes.clear();
HashMap<Integer, Fabricante> resultados;
try {
resultados = FabricantesDAO.consultarTodos();
if (resultados.size() > 0) {
for (Fabricante obj : resultados.values()) {
int id = obj.getId();
String nombre = obj.getNombre();
String direccion = obj.getDireccion();
String codigoPostal = obj.getCodigoPostal();
String poblacion = obj.getPoblacion();
int provincia = obj.getProvincia();
String telefono = obj.getTelefono();
String telefonoMovil = obj.getTelefonoMovil();
String email = obj.getEmail();
Fabricante objFabricante = new Fabricante(id, nombre, direccion, codigoPostal, poblacion, provincia, telefono, telefonoMovil, email);
this.listaFabricantes.add(objFabricante);
}
//Insertamos en el comboBox la lista de los fabricantes.
for (int indx = 0; indx < this.listaFabricantes.size(); indx++) {
this.cbxFabricante.addItem(this.listaFabricantes.get(indx).getNombre());
}
//Seleccionamos el indice -1 del comboBox para que no aparezca ningun Fabricante por defecto.
this.cbxFabricante.setSelectedIndex(-1);
} else {
JOptionPane.showMessageDialog(null, "Ningun fabricante encontrado!!!!. ", "ERROR!!!",
JOptionPane.ERROR_MESSAGE);
}
} catch (Exception ex) {
Logger.getLogger(Fabricante.class.getName()).log(Level.SEVERE, null, ex);
}
}
Si hago este evento en cuanto ejecuto el programa me viene aquí y me pone una foto sin elegir ningún fabricante.
1
2
3
4
5
6
7
8
9
10
11
private void cbxFabricanteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
// TODO add your handling code here:
String fabricante = this.cbxFabricante.getSelectedItem().toString();
Fabricante obj = FabricantesDAO.filtrarFabricanteNombre(fabricante);
this.lblLogoFabricante.setIcon(obj.getLogo());
} catch (Exception ex) {
Logger.getLogger(Maquinas.class.getName()).log(Level.SEVERE, null, ex);
}
}
Si pongo este otro evento me pasa lo mismo:
1
2
3
4
5
6
7
8
9
10
11
private void cbxFabricanteKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
try {
// TODO add your handling code here:
String fabricante = this.cbxFabricante.getSelectedItem().toString();
Fabricante obj = FabricantesDAO.filtrarFabricanteNombre(fabricante);
this.lblLogoFabricante.setIcon(obj.getLogo());
} catch (Exception ex) {
Logger.getLogger(Maquinas.class.getName()).log(Level.SEVERE, null, ex);
}
}
Alguien me puede ayudar?
Gracias.
Valora esta pregunta


0