Código HTML:
//ComboBox con lista de coches
public JComboBox getCampoMatriculaCoche() {
if (campoMatriculaCoche.getItemCount() == 0){
GestionarCoche gC = new GestionarCoche();
Hashtable hashCoches = gC.reCargar();
campoMatriculaCoche.addItem("Seleccione una matricula.. ");
for(Enumeration e = hashCoches.keys(); e.hasMoreElements();){
Object obj = e.nextElement();
campoMatriculaCoche.addItem(obj);
}
campoMatriculaCoche.addActionListener(listenerMatricula);
}
return campoMatriculaCoche;
}
public JComboBox getTipoMotorCoche() {
if (tipoMotorCoche.getItemCount() == 0){
tipoMotorCoche.addItem("Gasolina");
tipoMotorCoche.addItem("Diesel");
tipoMotorCoche.addItem("Bio-Diesel");
tipoMotorCoche.addItem("Electricidad");
}
return tipoMotorCoche;
}
//Panel del Boton agregar
public JPanel getPanelAddCocheBoton(){
JPanel panelAddCocheBoton = new JPanel();
JButton botonAnadir = new JButton("Modificar");
panelAddCocheBoton.setLayout(new GridLayout(1,1));
panelAddCocheBoton.add(botonAnadir);
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
//Recuperar datos Coche
if (concesionarioCoche.getSelectedIndex() > 0){
if(campoMatriculaCoche.getSelectedIndex() > 0){
concesionario = concesionarioCoche.getSelectedItem().toString();
matricula = campoMatriculaCoche.getSelectedItem().toString();
marca = marcaCoche.getText().toString();
modelo = modeloCoche.getText().toString();
clase = claseCoche.getSelectedItem().toString();
tipoMotor = tipoMotorCoche.getSelectedItem().toString();
try {
coche = (Coche) Class.forName("ucam.aplicacion."+clase).newInstance();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
coche.AtributosCoche(concesionario, matricula, marca, modelo, clase, tipoMotor);
//Creamos el ArrayList que contendra las especificaciones de las distintas clases de coches
listaEspecificaciones.removeAll(listaEspecificaciones);
listaEspecificaciones.add(Integer.parseInt(panelEspecificaciones.especificacion1Coche.getText().toString()));
coche.agregarAtributos(listaEspecificaciones); // Polimorfismo. Cada tipo de vehiculo implementa el metodo agregarAtributos, declarado en la clase Coche
coche.guardarCoche(coche); // declarado como abstract.
JOptionPane.showMessageDialog(null, marca+" "+modelo+" ha sido modificado", "Modificar Coche", JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null,"Matricula no valida", "Modificar Coche", JOptionPane.INFORMATION_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null,"Concesionario no valido", "Modificar Coche", JOptionPane.INFORMATION_MESSAGE);
}
}
};
botonAnadir.addActionListener(listener);
return panelAddCocheBoton;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
clase = claseCoche.getSelectedItem().toString();
panelPrincipal.removeAll();
panelPrincipal.setLayout(new BoxLayout(panelPrincipal,BoxLayout.PAGE_AXIS));
panelPrincipal.add(getPanelLabel());
panelPrincipal.add(getPanelAddCoche());
panelPrincipal.add(getJpanelEspecificaciones());
panelPrincipal.add(getPanelAddCocheBoton());
panelPrincipal.updateUI();
}
//Este es el ActionListener del JComboBox de la matricula que nos saca al coche del Fichero y nos rellena los campos
ActionListener listenerMatricula = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (campoMatriculaCoche.getSelectedIndex() > 0){
String matricula = campoMatriculaCoche.getSelectedItem().toString();
coche = gC.sacarDeFichero(matricula);
concesionario = coche.getConcesionario();
modelo = coche.getModelo();
marca = coche.getMarca();
clase = coche.getClase();
tipoMotor = coche.getTipoMotor();
listaEspecificaciones = coche.sacarAtributos();
concesionarioCoche.setSelectedItem(concesionario);
marcaCoche.setText(marca);
modeloCoche.setText(modelo);
tipoMotorCoche.setSelectedItem(tipoMotor);
claseCoche.setSelectedItem(clase);
panelEspecificaciones.especificacion1Coche.setText(listaEspecificaciones.get(0).toString());
}
}
};
ActionListener listenerConcesionario = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (concesionarioCoche.getSelectedIndex() > 0){
concesionario = concesionarioCoche.getSelectedItem().toString();
}
}
};
}
Lo pongo en dos partes porque no me dejaba ponerlo todo junto.
Yo deduzco que el fallo esta en esta clase en algun punto,porque cuando agrego un coche nuevo se pone todo en sus campos pero es cuando le doy a modificar y guardo es cuando se desencajan. Espero su ayuda y gracias en principio por todo.