Tengo dos ventanas que podréis ver en una imagen más adelante.
(http://i.stack.imgur.com/kmKm9.png)
Código de `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("");
}
}
(http://i.stack.imgur.com/vjLi7.png)
Aquí tengo la otra ventana, que se llama `VistaAlbumesBuscar`, y este es el código:
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());
}
}
Pues mi problema se plantea es que en la ventana 'VistaArtistasBuscar' yo desde ahí busco un `Artista`, y este se me muestra con todos sus `Album`es,
pues yo quiero que al tener seleccionado un `Album` y pincha en el botón seleccionar, se me abra la ventana 'VistaAlbumesBuscar' mostrando los datos
del `Artista`, ya son el nombre de este, el nombre del `Album` y el listado de `Cancion`es asociadas a este `Album`, a la vez que los `JComboBox` de `Artista` y `Album` contengan el resto de nombre, pero que al abrir la ventana estén señalados esos nombres, tal y como muestro en esta imagen, y podéis ver como son las dos ventanas:
(http://i.stack.imgur.com/Gh5DT.png)
Aquí dejo los metodos que he usado para llenar los `JComboBox` y el `JList` de la ventana `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();
}
}
Y como podéis ver dentro de la ventana `VistaAlbumesBuscar` he creado un método llamado `abrirVentana` al cual llamo desde `VistaArtistasBuscar` el cual podéis ver aquí:
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());
}
Y la llamada desde `VistaArtistasBuscar` es tal que así (Evidentemente va dentro de un ActionListener como podeis comprobar en `VistaArtistasBuscar`):
if(listadoAlbumes.getSelectedIndex()>=0&&evento.getSource()==botonSeleccionar){
VistaAlbumesBuscar ventanaAlbumBuscar=new VistaAlbumesBuscar();
ventanaAlbumBuscar.setVisible(true);
ventanaAlbumBuscar.abrirVentana(campoTextoArtista2, modeloLista, listadoAlbumes);
}
El problema es que me abre la ventana pero se muestra en blanco igualmente, con los `JComboBox` rellenos pero sin mostrar los valores de la ventana anterior; así que me gustaría que me ayudéis a ver qué está mal en ese método o cómo poder hacer lo que deseo aunque sea usando otra forma.
no lei todo lo que posteaste... me dio pereza ajajajaj disculpas
pero lo podes pasar mediante un arreglo
Lo primero es poner un método setText(String texto) a ventana2.
public class Ventana2 extends JDialog
{
private JLabel etiqueta = new JLabel ("su texto aquí");
...
public void setText (String elTexto)
{
etiqueta.setText(elTexto);
}
}
Si Ventana1 es la que hace el new de Ventana2, simplemente tendremos llamar al método setText()
public class Ventana1 extends JDialog
{
private Ventana2 v2 = new Ventana2();
private JButton boton = new JButton("Pulsame");
...
public Ventana1()
{
boton.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
v2.setText("El texto");
}
});
}
}