Obtención eventos en ComboBox, pase eventos a menuController,

cierre de la conexión BD al cerrar ventana.
This commit is contained in:
victor.monge 2025-03-06 20:22:02 -06:00
parent d684c59adc
commit 7f3c76ea18
6 changed files with 86 additions and 38 deletions
ProyectoVentaBoletos/src/main/java

View File

@ -17,7 +17,5 @@ public class ProyectoVentaBoletos {
// Iniciar interfaz principal // Iniciar interfaz principal
Menu menu = new Menu(); Menu menu = new Menu();
// Iniciar controlador menu
// ...
} }
} }

View File

@ -20,12 +20,12 @@ public class ConexionSingleton {
Class.forName("com.mysql.cj.jdbc.Driver"); Class.forName("com.mysql.cj.jdbc.Driver");
CONEXION = DriverManager.getConnection(URL, USERNAME, PASSWORD); CONEXION = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch(ClassNotFoundException e) { } catch(ClassNotFoundException e) {
System.out.println("ConexionSingleton: error Driver no encontrado. " + e.getMessage()); System.out.println("ConexionSingleton.getInstance(): error Driver no encontrado. " + e.getMessage());
} catch(SQLException e) { } catch(SQLException e) {
System.out.println("ConexionSingleton: error no se pudo establecer la conexión. " + e.getMessage()); System.out.println("ConexionSingleton.getInstance(): error no se pudo establecer la conexión. " + e.getMessage());
} }
} }
System.out.println("ConexionSingleton: conexión exitosa."); System.out.println("ConexionSingleton.getInstance(): conexión exitosa.");
return CONEXION; return CONEXION;
} }
@ -34,9 +34,9 @@ public class ConexionSingleton {
try { try {
CONEXION.close(); CONEXION.close();
CONEXION = null; CONEXION = null;
System.out.println("ConexionSingleton: conexión cerrada."); System.out.println("ConexionSingleton.closeConnection(): conexión cerrada.");
} catch (SQLException e) { } catch (SQLException e) {
System.out.println("ConexionSingleton: error al cerrar la conexión: " + e.getMessage()); System.out.println("ConexionSingleton.closeConnection(): error al cerrar la conexión: " + e.getMessage());
} }
} }
} }

View File

@ -1,11 +1,60 @@
package controller; package controller;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Evento;
import model.ModeloSQL;
import view.Menu; import view.Menu;
import view.MenuSala;
public class MenuController { public class MenuController extends WindowAdapter implements ActionListener {
private Menu menu; private Menu menu;
private ModeloSQL modeloSQL;
public MenuController(Menu menu){ public MenuController(Menu menu){
this.menu = menu; this.menu = menu;
this.modeloSQL = new ModeloSQL();
obtenerEventosDefaultComboBoxMoldel();
}
public void actionPerformed(ActionEvent e){
Object o = e.getSource();
// Si se presiona el boton de visualizar
if(o.equals(menu.jButtonVisualizar)){
System.out.println("controller.MenuController.actionPerformed(): jButtonVisualizar");
MenuSala menuSala = new MenuSala();
menu.jPanelMenuSala.removeAll(); // Limpiar panel
menu.jPanelMenuSala.add(menuSala, BorderLayout.CENTER); // Agrega el nuevo JPanel
menu.jPanelMenuSala.revalidate(); // Reorganiza los componentes
menu. jPanelMenuSala.repaint(); // Redibuja la interfaz
}
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("controller.MenuController.windowClosing(): cerrar.");
ConexionSingleton.closeConnection();
}
// Obtener ResultSet de ModeloSQL y añádir eventos al ComboBoxModel del ComboBox de eventos
public void obtenerEventosDefaultComboBoxMoldel(){
ResultSet rs = modeloSQL.obtenerEventos();
try {
while ( rs.next() ){
menu.defaultComboBoxModel.addElement(rs.getString("nombre"));
System.out.println("MenuController.obtenerEventosDefaultComboBoxMoldel(): + " + rs.getString("nombre"));
}
} catch (SQLException ex) {
System.out.println("MenuController.obtenerEventosDefaultComboBoxMoldel(): error " + ex);;
}
} }
} }

View File

@ -3,8 +3,6 @@ package model;
import controller.ConexionSingleton; import controller.ConexionSingleton;
import java.sql.*; import java.sql.*;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public class ModeloSQL { public class ModeloSQL {
Connection con; Connection con;
@ -16,6 +14,14 @@ public class ModeloSQL {
// Eventos // Eventos
public ResultSet obtenerEventos(){ public ResultSet obtenerEventos(){
ResultSet rs = null; ResultSet rs = null;
try {
Statement st = con.createStatement();
String consulta = "SELECT * FROM eventos";
rs = st.executeQuery(consulta);
System.out.println("ModeloSQL.obtenerEventos(): eventos obtenidos.");
} catch (SQLException e) {
System.out.println("[X]ModeloSQL.obtenerEventos(): " + e.getMessage());
}
return rs; return rs;
} }

View File

@ -79,26 +79,22 @@
</Component> </Component>
<Component class="javax.swing.JComboBox" name="jComboBoxEventos"> <Component class="javax.swing.JComboBox" name="jComboBoxEventos">
<Properties> <Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor"> <Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<StringArray count="4"> <Connection code="defaultComboBoxModel" type="code"/>
<StringItem index="0" value="Item 1"/>
<StringItem index="1" value="Item 2"/>
<StringItem index="2" value="Item 3"/>
<StringItem index="3" value="Item 4"/>
</StringArray>
</Property> </Property>
</Properties> </Properties>
<AuxValues> <AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/> <AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</AuxValues> </AuxValues>
</Component> </Component>
<Component class="javax.swing.JButton" name="jButtonVisualizar"> <Component class="javax.swing.JButton" name="jButtonVisualizar">
<Properties> <Properties>
<Property name="text" type="java.lang.String" value="Visualizar"/> <Property name="text" type="java.lang.String" value="Visualizar"/>
</Properties> </Properties>
<Events> <AuxValues>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonVisualizarActionPerformed"/> <AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</Events> </AuxValues>
</Component> </Component>
<Container class="javax.swing.JPanel" name="jPanelMenuSala"> <Container class="javax.swing.JPanel" name="jPanelMenuSala">
<Properties> <Properties>
@ -106,6 +102,9 @@
<Color blue="fd" green="fa" red="f8" type="rgb"/> <Color blue="fd" green="fa" red="f8" type="rgb"/>
</Property> </Property>
</Properties> </Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</AuxValues>
<Layout> <Layout>
<DimensionLayout dim="0"> <DimensionLayout dim="0">

View File

@ -1,14 +1,23 @@
package view; package view;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import controller.MenuController;
import javax.swing.DefaultComboBoxModel;
public class Menu extends javax.swing.JFrame { public class Menu extends javax.swing.JFrame {
public DefaultComboBoxModel<String> defaultComboBoxModel;
public Menu() { public Menu() {
defaultComboBoxModel = new DefaultComboBoxModel();
initComponents(); initComponents();
this.setLocationRelativeTo(null); this.setLocationRelativeTo(null);
this.setVisible(true); this.setVisible(true);
jPanelMenuSala.setLayout(new BorderLayout()); jPanelMenuSala.setLayout(new BorderLayout());
MenuController menuController = new MenuController(this);
this.jButtonVisualizar.addActionListener(menuController);
addWindowListener(menuController);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -29,14 +38,9 @@ public class Menu extends javax.swing.JFrame {
jLabel1.setFont(new java.awt.Font("Gill Sans MT", 0, 14)); // NOI18N jLabel1.setFont(new java.awt.Font("Gill Sans MT", 0, 14)); // NOI18N
jLabel1.setText("Seleccione un evento:"); jLabel1.setText("Seleccione un evento:");
jComboBoxEventos.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jComboBoxEventos.setModel(defaultComboBoxModel);
jButtonVisualizar.setText("Visualizar"); jButtonVisualizar.setText("Visualizar");
jButtonVisualizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonVisualizarActionPerformed(evt);
}
});
jPanelMenuSala.setBackground(new java.awt.Color(248, 250, 253)); jPanelMenuSala.setBackground(new java.awt.Color(248, 250, 253));
@ -85,18 +89,10 @@ public class Menu extends javax.swing.JFrame {
pack(); pack();
}// </editor-fold>//GEN-END:initComponents }// </editor-fold>//GEN-END:initComponents
private void jButtonVisualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVisualizarActionPerformed
MenuSala menuSala = new MenuSala();
jPanelMenuSala.removeAll(); // Limpiar panel
jPanelMenuSala.add(menuSala, BorderLayout.CENTER); // Agrega el nuevo JPanel
jPanelMenuSala.revalidate(); // Reorganiza los componentes
jPanelMenuSala.repaint(); // Redibuja la interfaz
}//GEN-LAST:event_jButtonVisualizarActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables // Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButtonVisualizar; public javax.swing.JButton jButtonVisualizar;
private javax.swing.JComboBox<String> jComboBoxEventos; public javax.swing.JComboBox<String> jComboBoxEventos;
private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanelMenuSala; public javax.swing.JPanel jPanelMenuSala;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
} }