diff --git a/src/tutorialJava/examenes/examen20250509/vista/PanelSociosPorEquipo.java b/src/tutorialJava/examenes/examen20250509/vista/PanelSociosPorEquipo.java index ef3ad13..0f551b4 100644 --- a/src/tutorialJava/examenes/examen20250509/vista/PanelSociosPorEquipo.java +++ b/src/tutorialJava/examenes/examen20250509/vista/PanelSociosPorEquipo.java @@ -3,18 +3,42 @@ package tutorialJava.examenes.examen20250509.vista; import javax.swing.JPanel; import java.awt.GridBagLayout; import javax.swing.JLabel; +import javax.swing.JOptionPane; + import java.awt.GridBagConstraints; import java.awt.Font; import java.awt.Insets; +import java.util.List; + +import javax.swing.ButtonGroup; import javax.swing.JComboBox; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; +import tutorialJava.examenes.examen20250616.controlador.ControladorEquipo; +import tutorialJava.examenes.examen20250616.controlador.ControladorSocio; +import tutorialJava.examenes.examen20250616.modelo.Equipo; +import tutorialJava.examenes.examen20250616.modelo.Socio; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.text.SimpleDateFormat; +import java.awt.event.ActionEvent; + public class PanelSociosPorEquipo extends JPanel { private static final long serialVersionUID = 1L; private JTable table; + private JRadioButton radioOrdenarNombre; + JRadioButton radioOrdenarApellido1; + JRadioButton radioOrdenarApellido2; + JRadioButton radioOrdenarFechaNacimiento; + JComboBox jcbEquipo; + JScrollPane scrollPane; + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + /** * Create the panel. @@ -23,7 +47,7 @@ public class PanelSociosPorEquipo extends JPanel { GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0, 0}; gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0}; - gridBagLayout.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE}; + gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE}; setLayout(gridBagLayout); @@ -38,19 +62,24 @@ public class PanelSociosPorEquipo extends JPanel { JLabel lblNewLabel_1 = new JLabel("Equipo:"); GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints(); - gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_1.insets = new Insets(0, 10, 5, 5); gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST; gbc_lblNewLabel_1.gridx = 0; gbc_lblNewLabel_1.gridy = 1; add(lblNewLabel_1, gbc_lblNewLabel_1); - JComboBox comboBox = new JComboBox(); - GridBagConstraints gbc_comboBox = new GridBagConstraints(); - gbc_comboBox.insets = new Insets(0, 0, 5, 0); - gbc_comboBox.fill = GridBagConstraints.HORIZONTAL; - gbc_comboBox.gridx = 1; - gbc_comboBox.gridy = 1; - add(comboBox, gbc_comboBox); + jcbEquipo = new JComboBox(); + jcbEquipo.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + cargarSociosEnTabla(); + } + }); + GridBagConstraints gbc_jcbEquipo = new GridBagConstraints(); + gbc_jcbEquipo.insets = new Insets(0, 0, 5, 0); + gbc_jcbEquipo.fill = GridBagConstraints.HORIZONTAL; + gbc_jcbEquipo.gridx = 1; + gbc_jcbEquipo.gridy = 1; + add(jcbEquipo, gbc_jcbEquipo); JPanel panel = new JPanel(); GridBagConstraints gbc_panel = new GridBagConstraints(); @@ -67,34 +96,55 @@ public class PanelSociosPorEquipo extends JPanel { // gbl_panel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE}; panel.setLayout(gbl_panel); - JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button"); - GridBagConstraints gbc_rdbtnNewRadioButton = new GridBagConstraints(); - gbc_rdbtnNewRadioButton.insets = new Insets(0, 0, 5, 5); - gbc_rdbtnNewRadioButton.gridx = 0; - gbc_rdbtnNewRadioButton.gridy = 0; - panel.add(rdbtnNewRadioButton, gbc_rdbtnNewRadioButton); + radioOrdenarNombre = new JRadioButton("Ordenar por Nombre"); + radioOrdenarNombre.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + cargarSociosEnTabla(); + } + }); + radioOrdenarNombre.setSelected(true); + GridBagConstraints gbc_radioOrdenarNombre = new GridBagConstraints(); + gbc_radioOrdenarNombre.insets = new Insets(0, 0, 5, 5); + gbc_radioOrdenarNombre.gridx = 0; + gbc_radioOrdenarNombre.gridy = 0; + panel.add(radioOrdenarNombre, gbc_radioOrdenarNombre); - JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button"); - GridBagConstraints gbc_rdbtnNewRadioButton_1 = new GridBagConstraints(); - gbc_rdbtnNewRadioButton_1.insets = new Insets(0, 0, 5, 0); - gbc_rdbtnNewRadioButton_1.gridx = 1; - gbc_rdbtnNewRadioButton_1.gridy = 0; - panel.add(rdbtnNewRadioButton_1, gbc_rdbtnNewRadioButton_1); + radioOrdenarApellido1 = new JRadioButton("Ordenar por primer apellido"); + radioOrdenarApellido1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + cargarSociosEnTabla(); + } + }); + GridBagConstraints gbc_radioOrdenarApellido1 = new GridBagConstraints(); + gbc_radioOrdenarApellido1.insets = new Insets(0, 0, 5, 0); + gbc_radioOrdenarApellido1.gridx = 1; + gbc_radioOrdenarApellido1.gridy = 0; + panel.add(radioOrdenarApellido1, gbc_radioOrdenarApellido1); - JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("New radio button"); - GridBagConstraints gbc_rdbtnNewRadioButton_2 = new GridBagConstraints(); - gbc_rdbtnNewRadioButton_2.insets = new Insets(0, 0, 0, 5); - gbc_rdbtnNewRadioButton_2.gridx = 0; - gbc_rdbtnNewRadioButton_2.gridy = 1; - panel.add(rdbtnNewRadioButton_2, gbc_rdbtnNewRadioButton_2); + radioOrdenarApellido2 = new JRadioButton("Ordenar por segundo apellido"); + radioOrdenarApellido2.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + cargarSociosEnTabla(); + } + }); + GridBagConstraints gbc_radioOrdenarApellido2 = new GridBagConstraints(); + gbc_radioOrdenarApellido2.insets = new Insets(0, 0, 0, 5); + gbc_radioOrdenarApellido2.gridx = 0; + gbc_radioOrdenarApellido2.gridy = 1; + panel.add(radioOrdenarApellido2, gbc_radioOrdenarApellido2); - JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("New radio button"); - GridBagConstraints gbc_rdbtnNewRadioButton_3 = new GridBagConstraints(); - gbc_rdbtnNewRadioButton_3.gridx = 1; - gbc_rdbtnNewRadioButton_3.gridy = 1; - panel.add(rdbtnNewRadioButton_3, gbc_rdbtnNewRadioButton_3); + radioOrdenarFechaNacimiento = new JRadioButton("Ordenar por fecha de nacimiento"); + radioOrdenarFechaNacimiento.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + cargarSociosEnTabla(); + } + }); + GridBagConstraints gbc_radioOrdenarFechaNacimiento = new GridBagConstraints(); + gbc_radioOrdenarFechaNacimiento.gridx = 1; + gbc_radioOrdenarFechaNacimiento.gridy = 1; + panel.add(radioOrdenarFechaNacimiento, gbc_radioOrdenarFechaNacimiento); - JScrollPane scrollPane = new JScrollPane(); + scrollPane = new JScrollPane(); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); gbc_scrollPane.gridwidth = 2; gbc_scrollPane.insets = new Insets(0, 0, 0, 5); @@ -105,7 +155,94 @@ public class PanelSociosPorEquipo extends JPanel { table = new JTable(); scrollPane.setViewportView(table); - + + ButtonGroup groupOrdenacion = new ButtonGroup(); + groupOrdenacion.add(radioOrdenarNombre); + groupOrdenacion.add(radioOrdenarApellido1); + groupOrdenacion.add(radioOrdenarApellido2); + groupOrdenacion.add(radioOrdenarFechaNacimiento); + + cargarEquipos(); + cargarSociosEnTabla(); + + } + + + /** + * + */ + private void cargarEquipos() { + List lista = ControladorEquipo.findAll(); + for (Equipo e : lista) { + jcbEquipo.addItem(e); + } } + /** + * + */ + private void cargarSociosEnTabla() { + // Localizo el id del equipo seleccionado en el JCombo + Equipo equipoSeleccionado = (Equipo) jcbEquipo.getSelectedItem(); + + // Localizo el campo de ordenación + String campoOrdenacion = ""; + if (radioOrdenarNombre.isSelected()) { + campoOrdenacion = "nombre"; + } + else if (radioOrdenarApellido1.isSelected()) { + campoOrdenacion = "apellido1"; + } + else if (radioOrdenarApellido2.isSelected()) { + campoOrdenacion = "apellido2"; + } + else { + campoOrdenacion = "fechaNacimiento"; + } + + // Obtengo los socios del equipo, ordenados correctamente + List listaSocios = ControladorSocio.findAllByEquipoAndOrder( + equipoSeleccionado.getId(), + campoOrdenacion); + + // Convierto los datos de la lista en una matriz de datos + Object matrizSocios[][] = new Object[listaSocios.size()][4]; + for (int i = 0; i < listaSocios.size(); i++) { + matrizSocios[i][0] = listaSocios.get(i).getNombre(); + matrizSocios[i][1] = listaSocios.get(i).getApellido1(); + matrizSocios[i][2] = listaSocios.get(i).getApellido2(); + matrizSocios[i][3] = + sdf.format(listaSocios.get(i).getFechaNacimiento()); + } + + // Creo un array con los títulos de las columnas + String titulos[] = new String[] {"Nombre", "Primer apellido", + "Segundo apellido", "Fecha nacimiento"}; + + // Creo un nuevo JTable con los datos + table = new JTable(matrizSocios, titulos); + table.addMouseListener(new MouseAdapter() { + + @Override + public void mouseClicked(MouseEvent e) { + JOptionPane.showMessageDialog(null, "Se ha hecho clic sobre " + + " el socio/a " + + listaSocios.get(table.getSelectedRow()).getNombre() + + " con id " + + listaSocios.get(table.getSelectedRow()).getId()); + } + }); + + // Muestro tabla en pantalla + scrollPane.setViewportView(table); + } } + + + + + + + + + diff --git a/src/tutorialJava/examenes/examen20250616/controlador/ControladorEquipo.java b/src/tutorialJava/examenes/examen20250616/controlador/ControladorEquipo.java index f4950b3..2a67bf0 100644 --- a/src/tutorialJava/examenes/examen20250616/controlador/ControladorEquipo.java +++ b/src/tutorialJava/examenes/examen20250616/controlador/ControladorEquipo.java @@ -31,6 +31,9 @@ public class ControladorEquipo { return list; } + + + } diff --git a/src/tutorialJava/examenes/examen20250616/controlador/ControladorSocio.java b/src/tutorialJava/examenes/examen20250616/controlador/ControladorSocio.java new file mode 100644 index 0000000..0053b45 --- /dev/null +++ b/src/tutorialJava/examenes/examen20250616/controlador/ControladorSocio.java @@ -0,0 +1,220 @@ +package tutorialJava.examenes.examen20250616.controlador; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; + +import tutorialJava.examenes.examen20250616.modelo.Equipo; +import tutorialJava.examenes.examen20250616.modelo.Socio; + +public class ControladorSocio { + + /** + * + * @return + */ + public static List findAllByEquipoAndOrder (int idEquipo, + String campoOrdenacion) { + + List list = new ArrayList(); + + try { + Connection conn = ConnectionManager.getConnection(); + PreparedStatement ps = conn.prepareStatement( + "select * from socio where idEquipo = ? " + + "order by " + campoOrdenacion); + ps.setInt(1, idEquipo); + + ResultSet rs = ps.executeQuery(); + while (rs.next()) { + list.add(getSocioFromResultSet(rs)); + } + } + catch (Exception ex) { + ex.printStackTrace(); + } + + return list; + } + + + public static Socio getPrimero() { + return getSocioFromSql("select * from socio order by id limit 1"); + } + + public static Socio getUltimo() { + return getSocioFromSql("select * from socio order by id desc limit 1"); + } + + public static Socio getSiguiente(int idActual) { + return getSocioFromSql("select * from socio " + + "where id > " + idActual + + " order by id limit 1"); + } + + public static Socio getAnterior(int idActual) { + return getSocioFromSql("select * from socio " + + "where id < " + idActual + + " order by id desc limit 1"); + } + + + /** + * + * @return + */ + public static Socio getSocioFromSql (String sql) { + Socio s = null; + + try { + Connection conn = ConnectionManager.getConnection(); + PreparedStatement ps = conn.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + s = getSocioFromResultSet(rs); + } + + } catch (Exception e) { + e.printStackTrace(); + } + return s; + } + + + /** + * + * @return + */ + private static Socio getSocioFromResultSet (ResultSet rs) throws Exception { + Socio s = new Socio(); + s.setId(rs.getInt("id")); + s.setNombre(rs.getString("nombre")); + s.setApellido1(rs.getString("apellido1")); + s.setApellido2(rs.getString("apellido2")); + s.setFechaNacimiento(rs.getDate("fechaNacimiento")); + s.setAntiguedadAnios(rs.getInt("antiguedadAnios")); + s.setActivo(rs.getBoolean("activo")); + s.setIdEquipo(rs.getInt("idEquipo")); + return s; + } + + /** + * + * @param s + * @return + */ + public static int guardar (Socio s) { + if (s.getId() == 0) { + return insertar(s); + } + else { + return modificar(s); + } + } + + + + public static int modificar (Socio s) { + try { + Connection conn = ConnectionManager.getConnection(); + PreparedStatement ps = conn.prepareStatement( + "update socio set nombre = ?, apellido1 = ?, " + + "apellido2 = ?, fechaNacimiento = ?, " + + "antiguedadAnios = ?, activo = ?, " + + "idEquipo = ? " + + "where id = ?"); + ps.setString(1, s.getNombre()); + ps.setString(2, s.getApellido1()); + ps.setString(3, s.getApellido2()); + ps.setDate(4, new java.sql.Date(s.getFechaNacimiento().getTime())); + ps.setInt(5, s.getAntiguedadAnios()); + ps.setBoolean(6, s.isActivo()); + ps.setInt(7, s.getIdEquipo()); + ps.setInt(8, s.getId()); + return ps.executeUpdate(); + } catch (Exception e) { + e.printStackTrace(); + } + + return 0; + } + + + public static int insertar (Socio s) { + try { + Connection conn = ConnectionManager.getConnection(); + PreparedStatement ps = conn.prepareStatement( + "insert into socio (id, nombre, apellido1, apellido2," + + "fechaNacimiento, antiguedadAnios, activo, idEquipo)" + + " values (?, ?, ?, ?, ?, ?, ?, ?)"); + ps.setInt(1, nuevoId()); + ps.setString(2, s.getNombre()); + ps.setString(3, s.getApellido1()); + ps.setString(4, s.getApellido2()); + ps.setDate(5, new java.sql.Date(s.getFechaNacimiento().getTime())); + ps.setInt(6, s.getAntiguedadAnios()); + ps.setBoolean(7, s.isActivo()); + ps.setInt(8, s.getIdEquipo()); + return ps.executeUpdate(); + } catch (Exception e) { + e.printStackTrace(); + } + return 0; + } + + + /** + * + * @return + */ + private static int nuevoId () { + try { + Connection conn = ConnectionManager.getConnection(); + PreparedStatement ps = conn.prepareStatement( + "select max(id) from socio"); + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + return rs.getInt(1) + 1; + } + } catch (Exception e) { + e.printStackTrace(); + } + return -1; + } + + /** + * + * @return + */ + public static int eliminar (int id) { + try { + Connection conn = ConnectionManager.getConnection(); + PreparedStatement ps = conn.prepareStatement( + "delete from socio where id = ?"); + ps.setInt(1, id); + return ps.executeUpdate(); + } catch (Exception e) { + e.printStackTrace(); + } + return 0; + } + +} + + + + + + + + + + + + + + + + diff --git a/src/tutorialJava/examenes/examen20250616/vista/PanelCRUDSocio.java b/src/tutorialJava/examenes/examen20250616/vista/PanelCRUDSocio.java new file mode 100644 index 0000000..2c24b63 --- /dev/null +++ b/src/tutorialJava/examenes/examen20250616/vista/PanelCRUDSocio.java @@ -0,0 +1,438 @@ +package tutorialJava.examenes.examen20250616.vista; + +import javax.swing.JPanel; +import java.awt.BorderLayout; +import javax.swing.JToolBar; + +import tutorialJava.examenes.examen20250509.controlador.ControladorConcierto; +import tutorialJava.examenes.examen20250509.modelo.Concierto; +import tutorialJava.examenes.examen20250616.controlador.ControladorEquipo; +import tutorialJava.examenes.examen20250616.controlador.ControladorSocio; +import tutorialJava.examenes.examen20250616.modelo.Equipo; +import tutorialJava.examenes.examen20250616.modelo.Socio; + +import javax.swing.JButton; +import java.awt.GridBagLayout; +import javax.swing.JLabel; +import javax.swing.JOptionPane; + +import java.awt.GridBagConstraints; +import java.awt.Font; +import java.awt.Insets; +import javax.swing.JTextField; +import javax.swing.JSlider; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.ImageIcon; +import java.awt.event.ActionListener; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.awt.event.ActionEvent; +import javax.swing.event.ChangeListener; +import javax.swing.event.ChangeEvent; + +public class PanelCRUDSocio extends JPanel { + + private static final long serialVersionUID = 1L; + private JTextField jtfNombre; + private JTextField jtfApellido1; + private JTextField jtfApellido2; + private JTextField jtfFechaNacimiento; + private JTextField jtfId; + private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + JSlider sliderAntiguedad; + JLabel lblAntiguedad; + JComboBox jcbEquipo; + JCheckBox chckActivo; + + /** + * Create the panel. + */ + public PanelCRUDSocio() { + setLayout(new BorderLayout(0, 0)); + + JToolBar toolBar = new JToolBar(); + add(toolBar, BorderLayout.NORTH); + + JButton btnPrimero = new JButton(""); + btnPrimero.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + mostrarSocioEnPantalla(ControladorSocio.getPrimero()); + } + }); + btnPrimero.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/gotostart.png"))); + toolBar.add(btnPrimero); + + JButton btnAnterior = new JButton(""); + btnAnterior.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int idSocioActual = Integer.parseInt(jtfId.getText()); + mostrarSocioEnPantalla( + ControladorSocio.getAnterior(idSocioActual)); + } + }); + btnAnterior.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/previous.png"))); + toolBar.add(btnAnterior); + + JButton btnSiguiente = new JButton(""); + btnSiguiente.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int idSocioActual = Integer.parseInt(jtfId.getText()); + mostrarSocioEnPantalla( + ControladorSocio.getSiguiente(idSocioActual)); + } + }); + btnSiguiente.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/next.png"))); + toolBar.add(btnSiguiente); + + JButton btnUltimo = new JButton(""); + btnUltimo.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + mostrarSocioEnPantalla(ControladorSocio.getUltimo()); + } + }); + btnUltimo.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/gotoend.png"))); + toolBar.add(btnUltimo); + + JButton btnNuevo = new JButton(""); + btnNuevo.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + nuevo(); + } + }); + btnNuevo.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/nuevo.png"))); + toolBar.add(btnNuevo); + + JButton btnGuardar = new JButton(""); + btnGuardar.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + guardar(); + } + }); + btnGuardar.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/guardar.png"))); + toolBar.add(btnGuardar); + + JButton btnEliminar = new JButton(""); + btnEliminar.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + eliminar(); + } + }); + btnEliminar.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/eliminar.png"))); + toolBar.add(btnEliminar); + + JPanel panel = new JPanel(); + add(panel, BorderLayout.CENTER); + GridBagLayout gbl_panel = new GridBagLayout(); + gbl_panel.columnWidths = new int[]{0, 0, 0, 0}; + gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + gbl_panel.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE}; + gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; + panel.setLayout(gbl_panel); + + JLabel lblNewLabel = new JLabel("Gestión de socios"); + lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14)); + GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); + gbc_lblNewLabel.gridwidth = 3; + gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0); + gbc_lblNewLabel.gridx = 0; + gbc_lblNewLabel.gridy = 0; + panel.add(lblNewLabel, gbc_lblNewLabel); + + JLabel lblNewLabel_8 = new JLabel("Id:"); + GridBagConstraints gbc_lblNewLabel_8 = new GridBagConstraints(); + gbc_lblNewLabel_8.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_8.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_8.gridx = 0; + gbc_lblNewLabel_8.gridy = 1; + panel.add(lblNewLabel_8, gbc_lblNewLabel_8); + + jtfId = new JTextField(); + GridBagConstraints gbc_jtfId = new GridBagConstraints(); + gbc_jtfId.insets = new Insets(0, 0, 5, 5); + gbc_jtfId.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfId.gridx = 1; + gbc_jtfId.gridy = 1; + panel.add(jtfId, gbc_jtfId); + jtfId.setColumns(10); + + JLabel lblNewLabel_1 = new JLabel("Nombre"); + GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints(); + gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_1.gridx = 0; + gbc_lblNewLabel_1.gridy = 2; + panel.add(lblNewLabel_1, gbc_lblNewLabel_1); + + jtfNombre = new JTextField(); + GridBagConstraints gbc_jtfNombre = new GridBagConstraints(); + gbc_jtfNombre.gridwidth = 2; + gbc_jtfNombre.insets = new Insets(0, 0, 5, 0); + gbc_jtfNombre.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfNombre.gridx = 1; + gbc_jtfNombre.gridy = 2; + panel.add(jtfNombre, gbc_jtfNombre); + jtfNombre.setColumns(10); + + JLabel lblNewLabel_2 = new JLabel("Primer apellido:"); + GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints(); + gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_2.gridx = 0; + gbc_lblNewLabel_2.gridy = 3; + panel.add(lblNewLabel_2, gbc_lblNewLabel_2); + + jtfApellido1 = new JTextField(); + GridBagConstraints gbc_jtfApellido1 = new GridBagConstraints(); + gbc_jtfApellido1.gridwidth = 2; + gbc_jtfApellido1.insets = new Insets(0, 0, 5, 0); + gbc_jtfApellido1.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfApellido1.gridx = 1; + gbc_jtfApellido1.gridy = 3; + panel.add(jtfApellido1, gbc_jtfApellido1); + jtfApellido1.setColumns(10); + + JLabel lblNewLabel_3 = new JLabel("Segundo apellido:"); + GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints(); + gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_3.gridx = 0; + gbc_lblNewLabel_3.gridy = 4; + panel.add(lblNewLabel_3, gbc_lblNewLabel_3); + + jtfApellido2 = new JTextField(); + GridBagConstraints gbc_jtfApellido2 = new GridBagConstraints(); + gbc_jtfApellido2.gridwidth = 2; + gbc_jtfApellido2.insets = new Insets(0, 0, 5, 0); + gbc_jtfApellido2.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfApellido2.gridx = 1; + gbc_jtfApellido2.gridy = 4; + panel.add(jtfApellido2, gbc_jtfApellido2); + jtfApellido2.setColumns(10); + + JLabel lblNewLabel_4 = new JLabel("Fecha de nacimiento:"); + GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints(); + gbc_lblNewLabel_4.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_4.gridx = 0; + gbc_lblNewLabel_4.gridy = 5; + panel.add(lblNewLabel_4, gbc_lblNewLabel_4); + + jtfFechaNacimiento = new JTextField(); + GridBagConstraints gbc_jtfFechaNacimiento = new GridBagConstraints(); + gbc_jtfFechaNacimiento.gridwidth = 2; + gbc_jtfFechaNacimiento.insets = new Insets(0, 0, 5, 0); + gbc_jtfFechaNacimiento.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfFechaNacimiento.gridx = 1; + gbc_jtfFechaNacimiento.gridy = 5; + panel.add(jtfFechaNacimiento, gbc_jtfFechaNacimiento); + jtfFechaNacimiento.setColumns(10); + + JLabel lblNewLabel_5 = new JLabel("Antigüedad (años):"); + GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints(); + gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_5.gridx = 0; + gbc_lblNewLabel_5.gridy = 6; + panel.add(lblNewLabel_5, gbc_lblNewLabel_5); + + sliderAntiguedad = new JSlider(); + sliderAntiguedad.addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent e) { + if (lblAntiguedad != null) { + lblAntiguedad.setText( + sliderAntiguedad.getValue() + " años"); + } + } + }); + sliderAntiguedad.setMaximum(150); + GridBagConstraints gbc_sliderAntiguedad = new GridBagConstraints(); + gbc_sliderAntiguedad.fill = GridBagConstraints.HORIZONTAL; + gbc_sliderAntiguedad.insets = new Insets(0, 0, 5, 5); + gbc_sliderAntiguedad.gridx = 1; + gbc_sliderAntiguedad.gridy = 6; + panel.add(sliderAntiguedad, gbc_sliderAntiguedad); + + lblAntiguedad = new JLabel("New label"); + GridBagConstraints gbc_lblAntiguedad = new GridBagConstraints(); + gbc_lblAntiguedad.insets = new Insets(0, 0, 5, 0); + gbc_lblAntiguedad.gridx = 2; + gbc_lblAntiguedad.gridy = 6; + panel.add(lblAntiguedad, gbc_lblAntiguedad); + + chckActivo = new JCheckBox("Socio en activo"); + GridBagConstraints gbc_chckActivo = new GridBagConstraints(); + gbc_chckActivo.anchor = GridBagConstraints.WEST; + gbc_chckActivo.insets = new Insets(0, 0, 5, 5); + gbc_chckActivo.gridx = 1; + gbc_chckActivo.gridy = 7; + panel.add(chckActivo, gbc_chckActivo); + + JLabel lblNewLabel_7 = new JLabel("Equipo:"); + GridBagConstraints gbc_lblNewLabel_7 = new GridBagConstraints(); + gbc_lblNewLabel_7.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_7.insets = new Insets(0, 0, 0, 5); + gbc_lblNewLabel_7.gridx = 0; + gbc_lblNewLabel_7.gridy = 8; + panel.add(lblNewLabel_7, gbc_lblNewLabel_7); + + jcbEquipo = new JComboBox(); + GridBagConstraints gbc_jcbEquipo = new GridBagConstraints(); + gbc_jcbEquipo.gridwidth = 2; + gbc_jcbEquipo.fill = GridBagConstraints.HORIZONTAL; + gbc_jcbEquipo.gridx = 1; + gbc_jcbEquipo.gridy = 8; + panel.add(jcbEquipo, gbc_jcbEquipo); + + cargarEquipos(); + mostrarSocioEnPantalla(ControladorSocio.getPrimero()); + } + + /** + * + */ + private void cargarEquipos(){ + List lista = ControladorEquipo.findAll(); + for (Equipo e : lista) { + this.jcbEquipo.addItem(e); + } + } + + + + + /** + * + */ + private void mostrarSocioEnPantalla(Socio s) { + if (s != null) { + this.jtfId.setText("" + s.getId()); + this.jtfNombre.setText(s.getNombre()); + this.jtfApellido1.setText(s.getApellido1()); + this.jtfApellido2.setText(s.getApellido2()); + this.jtfFechaNacimiento.setText( + sdf.format(s.getFechaNacimiento())); + this.sliderAntiguedad.setValue(s.getAntiguedadAnios()); + + this.chckActivo.setSelected(s.isActivo()); + + for (int i = 0; i < this.jcbEquipo.getItemCount(); i++) { + if (this.jcbEquipo.getItemAt(i).getId() == s.getIdEquipo()) { + this.jcbEquipo.setSelectedIndex(i); + } + } + } + } + + /** + * + * @return + */ + private Socio getSocioFromDatosDePantalla() { + Socio s = new Socio(); + s.setId(Integer.parseInt(jtfId.getText())); + s.setNombre(jtfNombre.getText()); + s.setApellido1(jtfApellido1.getText()); + s.setApellido2(jtfApellido2.getText()); + + try { + s.setFechaNacimiento(sdf.parse(jtfFechaNacimiento.getText())); + } catch (Exception e) { + JOptionPane.showMessageDialog(null, "El formato de la fecha debe ser dd/MM/yyyy"); + } + + s.setAntiguedadAnios(this.sliderAntiguedad.getValue()); + s.setActivo(this.chckActivo.isSelected()); + + Equipo equipoSeleccionado = (Equipo) this.jcbEquipo.getSelectedItem(); + s.setIdEquipo(equipoSeleccionado.getId()); + return s; + } + + /** + * + */ + private void guardar() { + Socio s = getSocioFromDatosDePantalla(); + int registrosAfectados = ControladorSocio.guardar(s); + if (registrosAfectados == 1) { + if (s.getId() == 0) { // Hemos insertado + mostrarSocioEnPantalla(ControladorSocio.getUltimo()); + } + JOptionPane.showMessageDialog(null, "Guardado correctamente"); + } + else { + JOptionPane.showMessageDialog(null, "Algo ha fallado"); + } + } + + /** + * + */ + private void nuevo() { + this.jtfId.setText("0"); + this.jtfNombre.setText(""); + this.jtfApellido1.setText(""); + this.jtfApellido2.setText(""); + this.jtfFechaNacimiento.setText(sdf.format(new Date())); + this.sliderAntiguedad.setValue(0); + this.chckActivo.setSelected(false); + this.jcbEquipo.setSelectedIndex(0); + } + + /** + * + */ + private void eliminar() { + String [] opciones ={"Sí","No"}; + int eleccion = JOptionPane.showOptionDialog(null, + "¿Desea eliminar el socio?","Eliminar socio", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, null, opciones, "Sí"); + if (eleccion != JOptionPane.YES_OPTION) { + return; + } + + + int idSocio = Integer.parseInt(jtfId.getText()); + int registrosAfectados = ControladorSocio.eliminar(idSocio); + if (registrosAfectados == 0) { + JOptionPane.showMessageDialog(null, "Error al eliminar"); + } + else if (registrosAfectados == 1) { + JOptionPane.showMessageDialog(null, "Eliminado correctamente"); + + Socio anterior = + ControladorSocio.getAnterior(idSocio); + // Si elimino correctamente, intento mostrar el anterior + if (anterior != null) { + mostrarSocioEnPantalla(anterior); + } + else { // No existe un anterior, intento mostrar el siguiente + Socio siguiente = + ControladorSocio.getSiguiente(idSocio); + if (siguiente != null) { + mostrarSocioEnPantalla(siguiente); + } + else { // No tiene concierto anterior ni siguiente + nuevo(); + } + } + } + } +} + + + + + + + + + + + + + + + diff --git a/src/tutorialJava/examenes/examen20250616/vista/VentanaPrincipal.java b/src/tutorialJava/examenes/examen20250616/vista/VentanaPrincipal.java index c369269..d9911a0 100644 --- a/src/tutorialJava/examenes/examen20250616/vista/VentanaPrincipal.java +++ b/src/tutorialJava/examenes/examen20250616/vista/VentanaPrincipal.java @@ -5,6 +5,9 @@ import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; + +import tutorialJava.examenes.examen20250509.vista.PanelSociosPorEquipo; + import java.awt.BorderLayout; import javax.swing.JLabel; import javax.swing.JTabbedPane; @@ -35,7 +38,7 @@ public class VentanaPrincipal extends JFrame { */ public VentanaPrincipal() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setBounds(100, 100, 450, 300); + setBounds(100, 100, 450, 500); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); @@ -48,14 +51,15 @@ public class VentanaPrincipal extends JFrame { JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); contentPane.add(tabbedPane, BorderLayout.CENTER); + PanelCRUDSocio panel_2 = new PanelCRUDSocio(); + tabbedPane.addTab("Gestión de socios", null, panel_2, null); + PanelListaEquipos panel = new PanelListaEquipos(); tabbedPane.addTab("Gestión de equipos", null, panel, null); - JPanel panel_1 = new JPanel(); - tabbedPane.addTab("New tab", null, panel_1, null); + PanelSociosPorEquipo panel_1 = new PanelSociosPorEquipo(); + tabbedPane.addTab("Socios por equipo", null, panel_1, null); - JPanel panel_2 = new JPanel(); - tabbedPane.addTab("New tab", null, panel_2, null); } }