I have two windows that you can see in an image below.
Code VistaArtistasBuscar
:
public class VistaArtistasBuscar extends JFrame implements ActionListener{
/**
* Componentes de la ventana
*/
private Coordinador miCoordinador;
private JTextField campoTextoArtista, campoTextoArtista2;
private JLabel etiquetaNombreArtista, etiquetaNombreArtista2, etiquetaNombreAlbumes;
private JButton botonBuscar, botonEliminar, botonModificar, botonSeleccionar;
private JList listadoAlbumes;
private DefaultListModel modeloLista;
private JSeparator separador;
private JScrollPane scrollListadoAlbumes;
private Generador miGenerador;
/**
* Iniciamos la ventana.
*/
public VistaArtistasBuscar() {
iniciarVentana();
}
/**
* Contenido de la ventana
*/
private void iniciarVentana() {
/*Propiedades Frame*/
setTitle("Artistas: Buscar");
setBounds(200, 100, 450, 337);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
setLocationRelativeTo(null);
setResizable(false);
/*Etiqueta nombre Artista I*/
etiquetaNombreArtista = new JLabel("Nombre:");
etiquetaNombreArtista.setFont(new Font("Tahoma", Font.PLAIN, 14));
etiquetaNombreArtista.setBounds(10, 6, 68, 22);
/*Campo texto Artista I*/
campoTextoArtista = new JTextField();
campoTextoArtista.setBounds(78, 6, 259, 22);
campoTextoArtista.setColumns(10);
/*Boton Buscar*/
botonBuscar = new JButton("Buscar");
botonBuscar.setBounds(347, 6, 77, 23);
botonBuscar.addActionListener(this);
getContentPane().setLayout(null);
getContentPane().add(etiquetaNombreArtista);
getContentPane().add(campoTextoArtista);
getContentPane().add(botonBuscar);
/*Etiqueta nombre Artista II*/
etiquetaNombreArtista2 = new JLabel("Nombre:");
etiquetaNombreArtista2.setFont(new Font("Tahoma", Font.PLAIN, 14));
etiquetaNombreArtista2.setBounds(10, 56, 68, 14);
getContentPane().add(etiquetaNombreArtista2);
/*Campo texto Artista II*/
campoTextoArtista2 = new JTextField();
campoTextoArtista2.setBounds(82, 55, 241, 20);
getContentPane().add(campoTextoArtista2);
campoTextoArtista2.setColumns(10);
/*Boton Eliminar*/
botonEliminar = new JButton("Eliminar");
botonEliminar.setBounds(234, 92, 89, 23);
botonEliminar.addActionListener(this);
getContentPane().add(botonEliminar);
/*Boton Modificar*/
botonModificar = new JButton("Modificar");
botonModificar.setBounds(135, 92, 89, 23);
botonModificar.addActionListener(this);
getContentPane().add(botonModificar);
/*Etiqueta Albumes*/
etiquetaNombreAlbumes = new JLabel("Albumes:");
etiquetaNombreAlbumes.setFont(new Font("Tahoma", Font.PLAIN, 14));
etiquetaNombreAlbumes.setBounds(10, 134, 89, 14);
getContentPane().add(etiquetaNombreAlbumes);
/*Boton Seleccionar*/
botonSeleccionar = new JButton("Seleccionar");
botonSeleccionar.setBounds(335, 265, 89, 23);
botonSeleccionar.addActionListener(this);
getContentPane().add(botonSeleccionar);
/*Separador*/
separador = new JSeparator();
separador.setBounds(0, 39, 434, 2);
getContentPane().add(separador);
/*Scroll para el listado de los Artistas*/
scrollListadoAlbumes = new JScrollPane();
listadoAlbumes = new JList();
modeloLista=new DefaultListModel();
scrollListadoAlbumes.setBounds(10, 154, 306, 135);
getContentPane().add(scrollListadoAlbumes);
scrollListadoAlbumes.setViewportView(listadoAlbumes);
}
/**
* Relacionamos esta clase con el Coordinador
*/
public void setCoordinador(Coordinador miCoordinador){
this.miCoordinador=miCoordinador;
}
/**
* Clase que maneja todos los eventos que ocurren en la ventana
*/
@Override
public void actionPerformed(ActionEvent evento) {
if(evento.getSource()==botonBuscar){
try{
ArtistaDAO artistaDAO=new ArtistaDAO();
ArtistaVO artistaVO=new ArtistaVO();
miGenerador=new Generador();
artistaVO.setNombreArtista(campoTextoArtista.getText());
artistaDAO.buscarArtista(artistaVO);
if(artistaDAO.buscarArtista(artistaVO)!=null){
mostrarArtista(artistaVO);
miGenerador.llenarListaAlbumes(artistaVO, modeloLista, listadoAlbumes, artistaDAO);
}else{
JOptionPane.showMessageDialog(null, "El artista no existe", "Error", JOptionPane.ERROR_MESSAGE);
}
}catch(Exception excepcion){
excepcion.printStackTrace();
}
}
if(evento.getSource()==botonModificar){
try{
ArtistaVO artistaVO=new ArtistaVO();
ArtistaDAO artistaDAO=new ArtistaDAO();
String nuevoNombre=campoTextoArtista2.getText();
artistaVO.setNombreArtista(campoTextoArtista.getText());
artistaDAO.modificarArtista(artistaVO, nuevoNombre);
}catch(Exception excepcion){
excepcion.printStackTrace();
}
}
if(evento.getSource()==botonEliminar){
if(!campoTextoArtista2.getText().equals("")){
int respuesta=JOptionPane.showConfirmDialog(this, "¿Estás seguro que deseas eliminar el Artista?", "Confirmacion", JOptionPane.YES_NO_OPTION);
if(respuesta==JOptionPane.YES_OPTION){
ArtistaDAO artistaDAO=new ArtistaDAO();
ArtistaVO artistaVO=new ArtistaVO();
AlbumVO albumVO=new AlbumVO();
albumVO.setNombreAlbum(listadoAlbumes.getSelectedValue().toString());
artistaVO.setNombreArtista(campoTextoArtista2.getText());
artistaDAO.eliminarArtista(artistaVO, artistaDAO, albumVO);
limpiar();
modeloLista.removeAllElements();
}
}
}
if(listadoAlbumes.getSelectedIndex()>=0&&evento.getSource()==botonSeleccionar){
VistaAlbumesBuscar ventanaAlbumBuscar=new VistaAlbumesBuscar();
ventanaAlbumBuscar.setVisible(true);
ventanaAlbumBuscar.abrirVentana(campoTextoArtista2, modeloLista, listadoAlbumes);
}else if(evento.getSource()==botonSeleccionar){
JOptionPane.showMessageDialog(null, "Debes elegir un Album de la lista","Información",JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* Establecemos el contenido del Campo texto tras la búsqueda
*/
private void mostrarArtista(ArtistaVO artistaVO){
campoTextoArtista2.setText(artistaVO.getNombreArtista());
}
private void limpiar(){
campoTextoArtista.setText("");
campoTextoArtista2.setText("");
}
}
Here I have the other window, which is called VistaAlbumesBuscar
, and this is the code:
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("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*/
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("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.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||elegirAlbum.getSelectedIndex()>=0){
AlbumVO albumVO=new AlbumVO();
AlbumDAO albumDAO=new AlbumDAO();
albumVO.setNombreAlbum(campoTextoAlbum.getText());
campoTextoAlbum.setText((String)elegirAlbum.getSelectedItem());
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();
}
}
}
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());
}
}
Well my problem arises is that in the window 'VistaArtistasSearch' I look for a Artista
from there, and this is shown to me with all its Album
is,
I want that when I have selected a Album
and click on the select button, I will open the window 'VistaAlbumsSearch' showing the data
of Artista
, are already the name of this, the name of the Album
and the listing of Cancion
are associated with this Album
, while the JComboBox
of Artista
and Album
contain the rest of the name, but when you open the window those names are indicated, as I show in this image, and you can see what the two windows look like:
Here I leave the methods that I used to fill the JComboBox
and the JList
of the window VistaAlbumesBuscar
:
public void llenarComboArtistas(DefaultComboBoxModel modeloCombo, JComboBox elegirArtista) {
Conexion conexion=new Conexion();
try {
modeloCombo = new DefaultComboBoxModel();
Statement sqlNombreArtista=conexion.getConexion().createStatement();
ResultSet resultado=sqlNombreArtista.executeQuery("SELECT name from artist");
modeloCombo.addElement("Seleccione un campo");
elegirArtista.setModel(modeloCombo);
while (resultado.next()) {
modeloCombo.addElement(resultado.getString("name"));
elegirArtista.setModel(modeloCombo);
}
sqlNombreArtista.close();
conexion.cerrarConexion();
} catch (SQLException excepcion) {
excepcion.printStackTrace();
}
}
public void llenarComboAlbumes(DefaultComboBoxModel modeloCombo, JComboBox elegirAlbum, JComboBox elegirArtista) {
Conexion conexion=new Conexion();
try {
ArtistaDAO artistaDAO=new ArtistaDAO();
ArtistaVO artistaVO=new ArtistaVO();
elegirAlbum.removeAllItems();
artistaVO.setNombreArtista((String)elegirArtista.getSelectedItem());
String sqlConsulta="SELECT title from album where artistid=?";
PreparedStatement sqlNombreAlbum=conexion.getConexion().prepareStatement(sqlConsulta);
sqlNombreAlbum.setInt(1, artistaDAO.getIdArtista(artistaVO));
ResultSet resultado=sqlNombreAlbum.executeQuery();
elegirAlbum.setModel(modeloCombo);
while (resultado.next()) {
modeloCombo.addElement(resultado.getString("title"));
elegirAlbum.setModel(modeloCombo);
}
sqlNombreAlbum.close();
} catch (SQLException excepcion) {
excepcion.printStackTrace();
}finally{
conexion.cerrarConexion();
}
}
And as you can see inside the window VistaAlbumesBuscar
I created a method called abrirVentana
which I call from VistaArtistasBuscar
which you can see here:
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 the call from VistaArtistasBuscar
is such that (Obviously it goes inside an ActionListener as you can check in VistaArtistasBuscar
):
if(listadoAlbumes.getSelectedIndex()>=0&&evento.getSource()==botonSeleccionar){
VistaAlbumesBuscar ventanaAlbumBuscar=new VistaAlbumesBuscar();
ventanaAlbumBuscar.setVisible(true);
ventanaAlbumBuscar.abrirVentana(campoTextoArtista2, modeloLista, listadoAlbumes);
}
The problem is that I open the window but it is also blank, with the JComboBox
filled but without showing the values of the previous window; so I'd like you to help me see what's wrong with that method or how to do what I want even if it's using another form.