Select does not catch the correct value of the TextField when modifying it

-2

Good morning, I have a window that serves to choose Artist and Album through two JComboBox, and at the same time they fill two JTextField, the JTextField that I use for the name of the Album I use it to edit it and give it the new name, but the problem is that when I click on Modify even if I have edited the JTextField, I still get the previous value.

This is the code of the window:

public class VistaAlbumesBuscar extends JFrame implements ItemListener, ActionListener{

/**
 * Componentes de la ventana
 */
private Coordinador miCoordinador;
private JTextField campoTextoArtista, campoTextoAlbum;
private JLabel etiquetaArtista, etiquetaAlbum, etiquetaArtista2, etiquetaAlbum2, etiquetaCanciones;
private JSeparator separador;
private JButton botonModificar, botonEliminar, botonEliminar2, botonAdd;
private List listaCanciones;
private JComboBox elegirAlbum, elegirArtista;
private DefaultComboBoxModel modeloCombo;
private Generador miGenerador;

/**
 * Iniciamos la ventana
 */
public VistaAlbumesBuscar() {
    iniciarVentana();
}

/**
 * Contenido de la ventana.
 */
private void iniciarVentana() {

    /*Propiedades Frame*/
    setTitle("Albumes: Buscar");
    setBounds(100, 100, 353, 437);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);
    setLocationRelativeTo(null);
    setResizable(false);

    /*Etiqueta Artista*/
    etiquetaArtista = new JLabel("Artista:");
    etiquetaArtista.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaArtista.setBounds(10, 11, 46, 14);
    getContentPane().add(etiquetaArtista);

    /*Etiqueta de Album*/
    etiquetaAlbum = new JLabel("Album:");
    etiquetaAlbum.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaAlbum.setBounds(10, 36, 46, 14);
    getContentPane().add(etiquetaAlbum);

    /*Separador*/
    separador = new JSeparator();
    separador.setBounds(0, 76, 337, 2);
    getContentPane().add(separador);

    /*Etiqueta Artista II*/
    etiquetaArtista2 = new JLabel("Artista:");
    etiquetaArtista2.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaArtista2.setBounds(10, 89, 46, 14);
    getContentPane().add(etiquetaArtista2);

    /*Campo texto Artista*/
    campoTextoArtista = new JTextField();
    campoTextoArtista.setBounds(62, 89, 231, 20);
    getContentPane().add(campoTextoArtista);
    campoTextoArtista.setColumns(10);
    campoTextoArtista.setEditable(false);

    /*Etiqueta Album II*/
    etiquetaAlbum2 = new JLabel("Album:");
    etiquetaAlbum2.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaAlbum2.setBounds(10, 121, 46, 14);
    getContentPane().add(etiquetaAlbum2);

    /*Campo texto Album*/
    campoTextoAlbum = new JTextField();
    campoTextoAlbum.setBounds(62, 120, 231, 20);
    getContentPane().add(campoTextoAlbum);
    campoTextoAlbum.setColumns(10);

    /*Boton Modificar*/
    botonModificar = new JButton("Modificar");
    botonModificar.setBounds(62, 151, 89, 23);
    botonModificar.addActionListener(this);
    getContentPane().add(botonModificar);

    /*Boton Eliminar*/
    botonEliminar = new JButton("Eliminar");
    botonEliminar.setBounds(161, 151, 89, 23);
    getContentPane().add(botonEliminar);

    /*Etiqueta Canciones*/
    etiquetaCanciones = new JLabel("Canciones:");
    etiquetaCanciones.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaCanciones.setBounds(10, 206, 76, 14);
    getContentPane().add(etiquetaCanciones);

    /*Lista de Canciones*/
    listaCanciones = new List();
    listaCanciones.setBounds(10, 226, 210, 163);
    getContentPane().add(listaCanciones);

    /*Boton Eliminar II*/
    botonEliminar2 = new JButton("Eliminar");
    botonEliminar2.setBounds(226, 366, 101, 23);
    getContentPane().add(botonEliminar2);

    /*Boton Añadir*/
    botonAdd = new JButton("A\u00F1adir");
    botonAdd.setBounds(226, 332, 101, 23);
    getContentPane().add(botonAdd);

    /*Combo Artista*/
    elegirArtista = new JComboBox();
    modeloCombo = new DefaultComboBoxModel();
    miGenerador=new Generador();
    elegirArtista.addItemListener(this);
    elegirArtista.setBounds(66, 10, 227, 20);
    getContentPane().add(elegirArtista);
    miGenerador.llenarComboArtistas(modeloCombo, elegirArtista);    

    /*Combo Albumes*/
    elegirAlbum = new JComboBox();
    elegirAlbum.setBounds(66, 35, 227, 20);
    elegirAlbum.addActionListener(this);
    getContentPane().add(elegirAlbum);  
}

public void setCoordinador(Coordinador miCoordinador){
    this.miCoordinador=miCoordinador;
}

public void itemStateChanged(ItemEvent evento) {
    if(evento.getStateChange()==ItemEvent.SELECTED){
        if(elegirArtista.getSelectedIndex()>0){
            miGenerador.llenarComboAlbumes(modeloCombo, elegirAlbum, elegirArtista);
            campoTextoArtista.setText((String)elegirArtista.getSelectedItem());

        }
    }
}

@Override
public void actionPerformed(ActionEvent evento) {
    campoTextoAlbum.setText((String)elegirAlbum.getSelectedItem());

    if(evento.getSource()==botonModificar){
        try{
            AlbumVO albumVO=new AlbumVO();
            AlbumDAO albumDAO=new AlbumDAO();

            String nuevoNombreAlbum=campoTextoAlbum.getText();
            albumVO.setNombreAlbum(elegirAlbum.getSelectedItem().toString());
            albumDAO.modificarAlbum(albumVO, nuevoNombreAlbum); 
        }catch(Exception excepcion){
            excepcion.printStackTrace();
        }
    }
}

}

And this is the code of the method I use to Modify the Album:

  public void modificarAlbum(AlbumVO albumVO, String nuevoNombreAlbum){

    Conexion conexion=new Conexion();
    ArtistaDAO artistaDAO=new ArtistaDAO();

    try{
        String consulta="update album set title=? where title=?";

        PreparedStatement sqlModificarAlbum=conexion.getConexion().prepareStatement(consulta);
        sqlModificarAlbum.setString(1, nuevoNombreAlbum);
        sqlModificarAlbum.setString(2, albumVO.getNombreAlbum());

        int validar=sqlModificarAlbum.executeUpdate();
        if(validar>0){
            JOptionPane.showMessageDialog(null, "Se ha modificado correctamente","Información",JOptionPane.INFORMATION_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(null, "No se ha modificado el Album","Información",JOptionPane.INFORMATION_MESSAGE);
        }
        sqlModificarAlbum.close();
        conexion.cerrarConexion();
    }catch(SQLException excepcion){
        excepcion.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error modificar el Album","Error",JOptionPane.ERROR_MESSAGE);
    }
}

I hope you understand the problem. Thanks.

SOLUTION

In the end I found the solution, it had nothing to do with the Update, but with the ComboBox event to show its information in the JTexField; simply in the class VistaAlbumesBuscar within the method actionPerformed should put this like this:

 if(evento.getSource()==elegirAlbum){
        campoTextoAlbum.setText((String)elegirAlbum.getSelectedItem());
      }

This way the JTexfield of the Album will not be changed if we do not click on the ComboBox, and in this way the Update takes the correct value to modify the name.

    
asked by NeoChiri 10.05.2016 в 11:59
source

2 answers

0

What happens is that when you make the modification you never pass the reference of the record to modify, you pass two parameters:

sqlModificarAlbum.setString(1, nuevoNombreAlbum);
sqlModificarAlbum.setString(2, albumVO.getNombreAlbum());

But you never tell him what record you are going to modify, I recommend that you handle an identifier, or store the original data and also pass it as a parameter.

    
answered by 10.05.2016 в 23:31
-1

The problem is when you add the name of the album to albumVO:

    String nuevoNombreAlbum=campoTextoAlbum.getText();

    //no estas tomando el valor del JTextField.
 albumVO.setNombreAlbum(elegirAlbum.getSelectedItem().toString());

You must take the value of the TextAlbum field, not the JComboBox:

   String nuevoNombreAlbum=campoTextoAlbum.getText(); 
 albumVO.setNombreAlbum(nuevoNombreAlbum);
    
answered by 10.05.2016 в 14:38