JList does not fill up when I open a window

2

I have two windows, one that I open from the other.

This is VistaAlbumesBuscar :

public class VistaAlbumesBuscar extends JFrame implements 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 JList listaCanciones;
private DefaultListModel modeloLista;
private JScrollPane scrollListadoCanciones;
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("Albums: Search");
    setBounds(100, 100, 353, 437);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);
    setLocationRelativeTo(null);
    setResizable(false);

    /*Etiqueta Artista*/
    etiquetaArtista = new JLabel("Artist:");
    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("Artist:");
    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("Modify");
    botonModificar.setBounds(62, 151, 89, 23);
    botonModificar.addActionListener(this);
    getContentPane().add(botonModificar);

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

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

    /*Lista de Canciones*/
    scrollListadoCanciones=new JScrollPane();
    listaCanciones = new JList();
    modeloLista=new DefaultListModel();
    scrollListadoCanciones.setBounds(10, 226, 210, 163);
    getContentPane().add(scrollListadoCanciones);
    scrollListadoCanciones.setViewportView(listaCanciones);

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

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

    /*Combo Artista*/
    elegirArtista = new JComboBox();
    modeloCombo = new DefaultComboBoxModel();
    miGenerador=new Generador();
    elegirArtista.addActionListener(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;
}

@Override
public void actionPerformed(ActionEvent evento) {
    if(evento.getSource()==elegirArtista){
        if(elegirArtista.getSelectedIndex()>0){
            miGenerador.llenarComboAlbumes(modeloCombo, elegirAlbum, elegirArtista);
            campoTextoArtista.setText((String)elegirArtista.getSelectedItem());

        }
    }

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

    if(elegirAlbum.getSelectedIndex()>=0){
        AlbumVO albumVO=new AlbumVO();
        AlbumDAO albumDAO=new AlbumDAO();

        albumVO.setNombreAlbum(campoTextoAlbum.getText());
        miGenerador.llenarListaCanciones(albumVO, modeloLista, listaCanciones, albumDAO);
    }

    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();
        }
    }

    if(evento.getSource()==botonEliminar){
        int respuesta=JOptionPane.showConfirmDialog(this, "¿Estás seguro de que deseas eliminar el Album?","Confirmación",JOptionPane.YES_NO_OPTION);
        if(respuesta==JOptionPane.YES_OPTION){
            AlbumDAO albumDAO=new AlbumDAO();
            AlbumVO albumVO=new AlbumVO();
            ArtistaDAO artistaDAO=new ArtistaDAO();
            ArtistaVO artistaVO=new ArtistaVO();

            artistaVO.setArtistName(campoTextoArtista.getText());
            albumVO.setNombreAlbum(campoTextoAlbum.getText());
            albumDAO.eliminarAlbum(artistaDAO, artistaVO, albumDAO, albumVO);
        }
    }

    if(evento.getSource()==botonAdd){
        VistaCancionesAdd ventanaCancionesAdd=new VistaCancionesAdd();

        ventanaCancionesAdd.setVisible(true);
        ventanaCancionesAdd.abrirVentanaCanciones(campoTextoArtista, campoTextoAlbum, modeloLista, listaCanciones);
    }
}

public void abrirVentana(JTextField campoTextoArtista2, DefaultListModel modeloLista, JList listadoAlbumes){

    miGenerador.llenarComboArtistas(modeloCombo, elegirArtista);
    miGenerador.llenarComboAlbumes(modeloCombo, elegirAlbum, elegirArtista);
    elegirArtista.setSelectedItem(campoTextoArtista2.getText());
    elegirAlbum.setSelectedItem(listadoAlbumes.getSelectedValue());
}

}

And this is VistaCancionesAdd :

public class VistaCancionesAdd extends JFrame{

private Coordinador miCoordinador;
private JTextField campoTextoArtistaCancion, campoTextoAlbumCancion, campoTextoCancion;
private JLabel etiquetaArtista, etiquetaAlbum, etiquetaCanciones, etiquetaCancion;
private JSeparator separador, separador2;
private JList listadoCanciones;
private DefaultListModel modeloListaCanciones;
private JScrollPane scrollListadoCanciones;
private JButton botonAdd, botonAdd2;
private Generador miGenerador;

/**
 * Iniciar la Ventana
 */
public VistaCancionesAdd() {
    iniciarVentana();
}

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

    /*Propiedades del Frame*/
    setTitle("Canciones: A\u00F1adir");
    setBounds(100, 100, 354, 374);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);

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

    /*Campo Texto Artista (No editable)*/
    campoTextoArtistaCancion = new JTextField();
    campoTextoArtistaCancion.setBounds(66, 10, 251, 20);
    getContentPane().add(campoTextoArtistaCancion);
    campoTextoArtistaCancion.setEditable(false);
    campoTextoArtistaCancion.setColumns(10);

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

    /*Campo texto Album (No editable)*/
    campoTextoAlbumCancion = new JTextField();
    campoTextoAlbumCancion.setBounds(66, 41, 251, 20);
    getContentPane().add(campoTextoAlbumCancion);
    campoTextoAlbumCancion.setEditable(false);
    campoTextoAlbumCancion.setColumns(10);

    /*Separador 1*/
    separador = new JSeparator();
    separador.setBounds(0, 70, 338, 2);
    getContentPane().add(separador);

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

    /*Listado Canciones*/
    scrollListadoCanciones=new JScrollPane();
    listadoCanciones = new JList();
    modeloListaCanciones=new DefaultListModel();
    scrollListadoCanciones.setBounds(10, 103, 216, 138);
    getContentPane().add(scrollListadoCanciones);
    scrollListadoCanciones.setViewportView(listadoCanciones);
    miGenerador=new Generador();

    /*Boton Añadir*/
    botonAdd = new JButton("Modificar");
    botonAdd.setBounds(239, 218, 89, 23);
    getContentPane().add(botonAdd);

    /*Separador 2*/
    separador2 = new JSeparator();
    separador2.setBounds(0, 262, 338, 2);
    getContentPane().add(separador2);

    /*Etiqueta Cancion*/
    etiquetaCancion = new JLabel("Canci\u00F3n:");
    etiquetaCancion.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaCancion.setBounds(10, 293, 62, 14);
    getContentPane().add(etiquetaCancion);

    /*Campo Texto Cancion*/
    campoTextoCancion = new JTextField();
    campoTextoCancion.setBounds(82, 292, 144, 20);
    getContentPane().add(campoTextoCancion);
    campoTextoCancion.setColumns(10);

    /*Boton Añadir 2*/
    botonAdd2 = new JButton("A\u00F1adir");
    botonAdd2.setBounds(239, 291, 89, 23);
    getContentPane().add(botonAdd2);

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

public void abrirVentanaCanciones(JTextField campoTextoArtista, JTextField campoTextoAlbum, DefaultListModel modeloLista, JList listaCanciones){
    AlbumVO  albumVO=new AlbumVO();
    AlbumDAO albumDAO=new AlbumDAO();

    albumVO.setNombreAlbum(campoTextoAlbumCancion.getText());
    campoTextoArtistaCancion.setText(campoTextoArtista.getText());
    campoTextoAlbumCancion.setText(campoTextoAlbum.getText());
    miGenerador.llenarListaCanciones2(albumVO, modeloListaCanciones, listadoCanciones, albumDAO);

}
}

Well, I open VistaCancionesAdd from VistaAlbumesBuscar and for this I use these methods that can be seen within the views.

The method I use from VistaCancionesAdd , I use it so that when the window opens the JTextField are filled with a certain value and the same for the JList

public void abrirVentanaCanciones(JTextField campoTextoArtista, JTextField campoTextoAlbum, DefaultListModel modeloLista, JList listaCanciones){
    AlbumVO  albumVO=new AlbumVO();
    AlbumDAO albumDAO=new AlbumDAO();

    albumVO.setNombreAlbum(campoTextoAlbumCancion.getText());
    campoTextoArtistaCancion.setText(campoTextoArtista.getText());
    campoTextoAlbumCancion.setText(campoTextoAlbum.getText());
    miGenerador.llenarListaCanciones2(albumVO, modeloListaCanciones, listadoCanciones, albumDAO);

}

And this is the call I make from VistaAlbumesBuscar :

  if(evento.getSource()==botonAdd){
        VistaCancionesAdd ventanaCancionesAdd=new VistaCancionesAdd();

        ventanaCancionesAdd.setVisible(true);
        ventanaCancionesAdd.abrirVentanaCanciones(campoTextoArtista, campoTextoAlbum, modeloLista, listaCanciones);
    }

This is the llenarListaCanciones2() method:

  public void llenarListaCanciones2(AlbumVO albumVO, DefaultListModel modeloListaCanciones,JList listadoCanciones, AlbumDAO albumDAO){

    Conexion conexion=new Conexion();
    //modeloLista.removeAllElements();
    try{
        String consultaSql="select name from track where albumid=?";
        PreparedStatement sqlNombreCancion=conexion.getConexion().prepareStatement(consultaSql);
        sqlNombreCancion.setInt(1, albumDAO.getIdAlbum(albumVO));
        ResultSet resultado=sqlNombreCancion.executeQuery();
        while(resultado.next()){
            modeloListaCanciones.addElement(resultado.getString("name"));
            listadoCanciones.setModel(modeloListaCanciones);
        }
        sqlNombreCancion.close();
        conexion.cerrarConexion();
    }catch(SQLException excepcion){
        excepcion.printStackTrace();
    }
   }

This is the getIdAlbum() method

    public int getIdAlbum(AlbumVO albumVO){

    Conexion conexion=new Conexion();
    int id=0;

    try{
        String consultaSql="SELECT albumId from album where title=?";
        PreparedStatement sqlArtistaId=conexion.getConexion().prepareStatement(consultaSql);
        sqlArtistaId.setString(1, albumVO.getNombreAlbum());
        ResultSet resultado=sqlArtistaId.executeQuery();
        while(resultado.next()){
            id=resultado.getInt("albumid");
        }
        sqlArtistaId.close();
        conexion.cerrarConexion();
        return id;
    }catch(SQLException excepcion){
        excepcion.printStackTrace();
        return 0;
    }
    }

My problem is that when I open VistaCancionesAdd does not fill the JList is more, the one that does is the JList of VistaAlbumesBuscar and I what I want is to fill the VistaCancionesAdd .

    
asked by NeoChiri 22.05.2016 в 10:48
source

1 answer

1

In your class VistaAlbumesBuscar you have the following attribute:

private JList listaCanciones;

Instanced like this:

listaCanciones = new JList();
scrollListadoCanciones.setViewportView(listaCanciones);

said JList must take values (load) when the following occurs:

if(elegirAlbum.getSelectedIndex() >= 0){
    AlbumVO albumVO=new AlbumVO();
    AlbumDAO albumDAO=new AlbumDAO();
    albumVO.setNombreAlbum(campoTextoAlbum.getText());
    miGenerador.llenarListaCanciones(albumVO, modeloLista, listaCanciones, albumDAO);
}

then I suppose that because of what you say, this is the JList that does manage to load with data.

Then, if you press your "Add" button, the following happens:

if(evento.getSource()==botonAdd){
    VistaCancionesAdd ventanaCancionesAdd=new VistaCancionesAdd();
    ventanaCancionesAdd.setVisible(true);
    ventanaCancionesAdd.abrirVentanaCanciones(campoTextoArtista, campoTextoAlbum, modeloLista, listaCanciones);
}

you pass your "loaded" list to the other window VistaCancionesAdd , same that also has an attribute of type JList called listadoCanciones that is initialized as well

listadoCanciones = new JList();
modeloListaCanciones=new DefaultListModel();
scrollListadoCanciones.setBounds(10, 103, 216, 138);
getContentPane().add(scrollListadoCanciones);
scrollListadoCanciones.setViewportView(listadoCanciones);

Then you have your abrirVentanaCanciones method that is defined as follows:

public void abrirVentanaCanciones(JTextField campoTextoArtista, JTextField campoTextoAlbum, DefaultListModel modeloLista, JList listaCanciones){
    AlbumVO  albumVO = new AlbumVO();
    AlbumDAO albumDAO = new AlbumDAO();

    albumVO.setNombreAlbum(campoTextoAlbumCancion.getText());
    campoTextoArtistaCancion.setText(campoTextoArtista.getText());
    campoTextoAlbumCancion.setText(campoTextoAlbum.getText());
    miGenerador.llenarListaCanciones2(albumVO, modeloListaCanciones, listadoCanciones, albumDAO);

}

But if you notice, even if you pass the parameters modeloLista and listaCanciones you do not do anything with them in the abrirVentanaCanciones method, they only cause confusion. If the method llenarListaCanciones of your class VistaAlbumesBuscar is working correctly, I assume that your method llenarListaCanciones2 is the result of a copy / paste of the first, Verify that you are loading the model to the correct% JList and eliminate the redundancies that I mention. After that, let's see what happens.

    
answered by 23.05.2016 / 18:40
source