diff --git a/src/tutorialJava/capitulo9_AWT_SWING/v06_EjemplosJTable/v02_TablaConDefaultTableModel.java b/src/tutorialJava/capitulo9_AWT_SWING/v06_EjemplosJTable/v02_TablaConDefaultTableModel.java index 41766ec..0eeb14d 100644 --- a/src/tutorialJava/capitulo9_AWT_SWING/v06_EjemplosJTable/v02_TablaConDefaultTableModel.java +++ b/src/tutorialJava/capitulo9_AWT_SWING/v06_EjemplosJTable/v02_TablaConDefaultTableModel.java @@ -62,17 +62,27 @@ public class v02_TablaConDefaultTableModel extends JPanel { */ private DefaultTableModel getDefaultTableModelNoEditable () { DefaultTableModel dtm = new DefaultTableModel(datosEnTabla, titulosEnTabla) { - - /** - * La sobreescritura de este m�todo nos permite controlar qu� celdas queremos que sean editables - */ + @Override public boolean isCellEditable(int row, int column) { - if (column != 1) { - return false; - } - return true; + if (column == 1) + return true; + return false; } + + + + +// /** +// * La sobreescritura de este m�todo nos permite controlar qu� celdas queremos que sean editables +// */ +// @Override +// public boolean isCellEditable(int row, int column) { +// if (column != 1) { +// return false; +// } +// return true; +// } }; return dtm; } diff --git a/src/tutorialJava/examenes/examen20250509/controlador/ConnectionManager.java b/src/tutorialJava/examenes/examen20250509/controlador/ConnectionManager.java new file mode 100644 index 0000000..941dfcd --- /dev/null +++ b/src/tutorialJava/examenes/examen20250509/controlador/ConnectionManager.java @@ -0,0 +1,22 @@ +package tutorialJava.examenes.examen20250509.controlador; + +import java.sql.Connection; +import java.sql.DriverManager; + +public class ConnectionManager { + + private static Connection conn = null; + + + public static Connection getConnection() throws Exception { + if (conn == null) { + Class.forName("com.mysql.cj.jdbc.Driver"); + + conn = + (Connection) DriverManager.getConnection ( + "jdbc:mysql://localhost:3306/conciertos_grupos_salas?serverTimezone=Europe/Madrid", + "root", "1234"); + } + return conn; + } +} diff --git a/src/tutorialJava/examenes/examen20250509/controlador/ControladorConcierto.java b/src/tutorialJava/examenes/examen20250509/controlador/ControladorConcierto.java new file mode 100644 index 0000000..139db7e --- /dev/null +++ b/src/tutorialJava/examenes/examen20250509/controlador/ControladorConcierto.java @@ -0,0 +1,163 @@ +package tutorialJava.examenes.examen20250509.controlador; + + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; +import java.text.SimpleDateFormat; + +import tutorialJava.examenes.examen20250509.modelo.Concierto; + +public class ControladorConcierto { + + public static Concierto getPrimero () { + Concierto c = getConciertoDesdeSql("select * from concierto " + + "order by id asc limit 1"); + return c; + } + + public static Concierto getSiguiente (int idActual) { + Concierto c = getConciertoDesdeSql("select * from concierto " + + "where id > " + idActual + " order by id asc limit 1"); + return c; + } + + public static Concierto getUltimo () { + Concierto c = getConciertoDesdeSql("select * from concierto " + + "order by id desc limit 1"); + return c; + } + + public static Concierto getAnterior (int idActual) { + Concierto c = getConciertoDesdeSql("select * from concierto " + + "where id < " + idActual + " order by id desc limit 1"); + return c; + } + + + public static Concierto getConciertoDesdeSql (String sql) { + Concierto c = null; + try { + Connection conn = ConnectionManager.getConnection(); + Statement s = conn.createStatement(); + ResultSet rs = s.executeQuery(sql); + if (rs.next()) { + c = new Concierto(); + c.setId(rs.getInt("id")); + c.setFecha(rs.getDate("fecha")); + c.setPrecio(rs.getFloat("precio")); + c.setFestival(rs.getBoolean("es_festival")); + c.setIdGrupo(rs.getInt("grupo_id")); + c.setIdSala(rs.getInt("sala_id")); + return c; + } + } catch (Exception e) { + e.printStackTrace(); + } + return c; + } + + /** + * + * @param c + * @return + */ + public static int guardar (Concierto c) { + if (c.getId() > 0) { + return modificar(c); + } + else if (c.getId() == 0){ // Inserción del dato + return insertar(c); + } + return 0; + } + + /** + * + * @param c + */ + public static int modificar(Concierto c) { + int registroAfectados = 0; + try { + Connection conn = ConnectionManager.getConnection(); + PreparedStatement ps = conn.prepareStatement( + "update concierto set fecha= ? " + + ", precio= ?, es_festival = ?, " + + "sala_id = ?, grupo_id = ? " + + "where id = ?"); + long millisFecha = c.getFecha().getTime(); + ps.setDate(1, new java.sql.Date(millisFecha)); + ps.setFloat(2, c.getPrecio()); + ps.setBoolean(3, c.isFestival()); + ps.setInt(4, c.getIdSala()); + ps.setInt(5, c.getIdGrupo()); + ps.setInt(6, c.getId()); + registroAfectados = ps.executeUpdate(); + + } catch (Exception e) { + e.printStackTrace(); + } + return registroAfectados; + } + + /** + * + * @param c + * @return + */ + public static int insertar(Concierto c) { + int registroAfectados = 0; + try { + Connection conn = ConnectionManager.getConnection(); + PreparedStatement ps = conn.prepareStatement( + "insert into concierto " + + "(id, fecha, precio, es_festival, sala_id," + + "grupo_id) values (?, ?, ?, ?, ?, ?)"); + long millisFecha = c.getFecha().getTime(); + ps.setInt(1, c.getId()); + ps.setDate(2, new java.sql.Date(millisFecha)); + ps.setFloat(3, c.getPrecio()); + ps.setBoolean(4, c.isFestival()); + ps.setInt(5, c.getIdSala()); + ps.setInt(6, c.getIdGrupo()); + registroAfectados = ps.executeUpdate(); + + } catch (Exception e) { + e.printStackTrace(); + } + return registroAfectados; + } + + + /* + * @param c + * @return + */ + public static int eliminar(int idConcierto) { + int registroAfectados = 0; + try { + Connection conn = ConnectionManager.getConnection(); + PreparedStatement ps = conn.prepareStatement( + "delete from concierto where id = ?"); + ps.setInt(1, idConcierto); + registroAfectados = ps.executeUpdate(); + + } catch (Exception e) { + e.printStackTrace(); + } + return registroAfectados; + } + +} + + + + + + + + + + + diff --git a/src/tutorialJava/examenes/examen20250509/controlador/ControladorGrupo.java b/src/tutorialJava/examenes/examen20250509/controlador/ControladorGrupo.java new file mode 100644 index 0000000..3ab0cc9 --- /dev/null +++ b/src/tutorialJava/examenes/examen20250509/controlador/ControladorGrupo.java @@ -0,0 +1,34 @@ +package tutorialJava.examenes.examen20250509.controlador; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +import tutorialJava.examenes.examen20250509.modelo.Grupo; + +public class ControladorGrupo { + + public static List findAll() { + List lista = new ArrayList(); + + try { + Connection conn = ConnectionManager.getConnection(); + Statement s = conn.createStatement(); + ResultSet rs = s.executeQuery("select * from grupo"); + + while (rs.next()) { + Grupo g = new Grupo(); + g.setId(rs.getInt("id")); + g.setNombre(rs.getString("nombre")); + g.setEstilo(rs.getString("estilo")); + lista.add(g); + } + } catch (Exception e) { + e.printStackTrace(); + } + + return lista; + } +} diff --git a/src/tutorialJava/examenes/examen20250509/controlador/ControladorSala.java b/src/tutorialJava/examenes/examen20250509/controlador/ControladorSala.java new file mode 100644 index 0000000..aac115c --- /dev/null +++ b/src/tutorialJava/examenes/examen20250509/controlador/ControladorSala.java @@ -0,0 +1,37 @@ +package tutorialJava.examenes.examen20250509.controlador; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +import tutorialJava.examenes.examen20250509.modelo.Sala; + + + +public class ControladorSala { + + public static List findAll() { + List lista = new ArrayList(); + + try { + Connection conn = ConnectionManager.getConnection(); + Statement s = conn.createStatement(); + ResultSet rs = s.executeQuery("select * from sala"); + + while (rs.next()) { + Sala sala = new Sala(); + sala.setId(rs.getInt("id")); + sala.setNombre(rs.getString("nombre")); + sala.setCiudad(rs.getString("ciudad")); + sala.setAforo(rs.getInt("aforo")); + lista.add(sala); + } + } catch (Exception e) { + e.printStackTrace(); + } + + return lista; + } +} diff --git a/src/tutorialJava/examenes/examen20250509/modelo/Concierto.java b/src/tutorialJava/examenes/examen20250509/modelo/Concierto.java new file mode 100644 index 0000000..4007830 --- /dev/null +++ b/src/tutorialJava/examenes/examen20250509/modelo/Concierto.java @@ -0,0 +1,72 @@ +package tutorialJava.examenes.examen20250509.modelo; + +import java.util.Date; + +public class Concierto { + private int id; + private Date fecha; + private float precio; + private boolean festival; + private int idSala; + private int idGrupo; + + public Concierto() { + super(); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Date getFecha() { + return fecha; + } + + public void setFecha(Date fecha) { + this.fecha = fecha; + } + + public float getPrecio() { + return precio; + } + + public void setPrecio(float precio) { + this.precio = precio; + } + + public boolean isFestival() { + return festival; + } + + public void setFestival(boolean festival) { + this.festival = festival; + } + + public int getIdSala() { + return idSala; + } + + public void setIdSala(int idSala) { + this.idSala = idSala; + } + + public int getIdGrupo() { + return idGrupo; + } + + public void setIdGrupo(int idGrupo) { + this.idGrupo = idGrupo; + } + + @Override + public String toString() { + return "Concierto [id=" + id + ", fecha=" + fecha + ", precio=" + precio + ", esFestival=" + festival + + ", idSala=" + idSala + ", idGrupo=" + idGrupo + "]"; + } + + +} diff --git a/src/tutorialJava/examenes/examen20250509/modelo/Grupo.java b/src/tutorialJava/examenes/examen20250509/modelo/Grupo.java new file mode 100644 index 0000000..b194397 --- /dev/null +++ b/src/tutorialJava/examenes/examen20250509/modelo/Grupo.java @@ -0,0 +1,42 @@ +package tutorialJava.examenes.examen20250509.modelo; + +public class Grupo { + private int id; + private String nombre; + private String estilo; + + public Grupo() { + super(); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + public String getEstilo() { + return estilo; + } + + public void setEstilo(String estilo) { + this.estilo = estilo; + } + + @Override + public String toString() { + return nombre; + } + + +} diff --git a/src/tutorialJava/examenes/examen20250509/modelo/Sala.java b/src/tutorialJava/examenes/examen20250509/modelo/Sala.java new file mode 100644 index 0000000..78506bd --- /dev/null +++ b/src/tutorialJava/examenes/examen20250509/modelo/Sala.java @@ -0,0 +1,51 @@ +package tutorialJava.examenes.examen20250509.modelo; + +public class Sala { + private int id; + private String nombre; + private String ciudad; + private int aforo; + + public Sala() { + super(); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + public String getCiudad() { + return ciudad; + } + + public void setCiudad(String ciudad) { + this.ciudad = ciudad; + } + + public int getAforo() { + return aforo; + } + + public void setAforo(int aforo) { + this.aforo = aforo; + } + + @Override + public String toString() { + return nombre; + } + + +} diff --git a/src/tutorialJava/examenes/examen20250509/vista/PanelGestionConciertos.java b/src/tutorialJava/examenes/examen20250509/vista/PanelGestionConciertos.java new file mode 100644 index 0000000..7605cf5 --- /dev/null +++ b/src/tutorialJava/examenes/examen20250509/vista/PanelGestionConciertos.java @@ -0,0 +1,396 @@ +package tutorialJava.examenes.examen20250509.vista; + +import javax.swing.JPanel; +import java.awt.BorderLayout; +import javax.swing.JToolBar; +import javax.swing.JButton; +import javax.swing.ImageIcon; +import javax.swing.JLabel; +import javax.swing.JOptionPane; + +import java.awt.GridBagLayout; +import java.awt.GridBagConstraints; +import java.awt.Insets; +import javax.swing.JCheckBox; +import javax.swing.JTextField; +import javax.swing.JComboBox; +import java.awt.event.ActionListener; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.awt.event.ActionEvent; +import tutorialJava.examenes.examen20250509.modelo.Concierto; +import tutorialJava.examenes.examen20250509.modelo.Grupo; +import tutorialJava.examenes.examen20250509.modelo.Sala; +import tutorialJava.examenes.examen20250509.controlador.ControladorConcierto; +import tutorialJava.examenes.examen20250509.controlador.ControladorGrupo; +import tutorialJava.examenes.examen20250509.controlador.ControladorSala; + +public class PanelGestionConciertos extends JPanel { + + private static final long serialVersionUID = 1L; + private JTextField jtfId; + private JTextField jtfFecha; + private JTextField jtfPrecio; + JComboBox jcbGrupo; + JComboBox jcbSala; + JCheckBox checkEsFestival; + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + JLabel lblErrorEnPrecio; + + + /** + * Create the panel. + */ + public PanelGestionConciertos() { + 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) { + Concierto c = ControladorConcierto.getPrimero(); + mostrarConciertoEnPantalla(c); + } + }); + btnPrimero.setIcon(new ImageIcon(PanelGestionConciertos.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 idActual = Integer.parseInt(jtfId.getText()); + mostrarConciertoEnPantalla( + ControladorConcierto.getAnterior(idActual)); + } + }); + btnAnterior.setIcon(new ImageIcon(PanelGestionConciertos.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 idActual = Integer.parseInt(jtfId.getText()); + mostrarConciertoEnPantalla( + ControladorConcierto.getSiguiente(idActual)); + } + }); + btnSiguiente.setIcon(new ImageIcon(PanelGestionConciertos.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) { + mostrarConciertoEnPantalla(ControladorConcierto.getUltimo()); + } + }); + btnUltimo.setIcon(new ImageIcon(PanelGestionConciertos.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(PanelGestionConciertos.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(PanelGestionConciertos.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(PanelGestionConciertos.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}; + gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0}; + gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; + gbl_panel.rowWeights = new double[]{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("Id:"); + GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); + gbc_lblNewLabel.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel.gridx = 0; + gbc_lblNewLabel.gridy = 0; + panel.add(lblNewLabel, gbc_lblNewLabel); + + jtfId = new JTextField(); + jtfId.setEnabled(false); + GridBagConstraints gbc_jtfId = new GridBagConstraints(); + gbc_jtfId.insets = new Insets(0, 0, 5, 0); + gbc_jtfId.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfId.gridx = 1; + gbc_jtfId.gridy = 0; + panel.add(jtfId, gbc_jtfId); + jtfId.setColumns(10); + + JLabel lblNewLabel_1 = new JLabel("Fecha:"); + 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 = 1; + panel.add(lblNewLabel_1, gbc_lblNewLabel_1); + + jtfFecha = new JTextField(); + GridBagConstraints gbc_jtfFecha = new GridBagConstraints(); + gbc_jtfFecha.insets = new Insets(0, 0, 5, 0); + gbc_jtfFecha.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfFecha.gridx = 1; + gbc_jtfFecha.gridy = 1; + panel.add(jtfFecha, gbc_jtfFecha); + jtfFecha.setColumns(10); + + JLabel lblNewLabel_2 = new JLabel("Precio"); + 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 = 2; + panel.add(lblNewLabel_2, gbc_lblNewLabel_2); + + jtfPrecio = new JTextField(); + GridBagConstraints gbc_jtfPrecio = new GridBagConstraints(); + gbc_jtfPrecio.insets = new Insets(0, 0, 5, 0); + gbc_jtfPrecio.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfPrecio.gridx = 1; + gbc_jtfPrecio.gridy = 2; + panel.add(jtfPrecio, gbc_jtfPrecio); + jtfPrecio.setColumns(10); + + lblErrorEnPrecio = new JLabel(""); + GridBagConstraints gbc_lblErrorEnPrecio = new GridBagConstraints(); + gbc_lblErrorEnPrecio.anchor = GridBagConstraints.WEST; + gbc_lblErrorEnPrecio.insets = new Insets(0, 0, 5, 0); + gbc_lblErrorEnPrecio.gridx = 1; + gbc_lblErrorEnPrecio.gridy = 3; + panel.add(lblErrorEnPrecio, gbc_lblErrorEnPrecio); + + checkEsFestival = new JCheckBox("New check box"); + GridBagConstraints gbc_checkEsFestival = new GridBagConstraints(); + gbc_checkEsFestival.anchor = GridBagConstraints.WEST; + gbc_checkEsFestival.insets = new Insets(0, 0, 5, 0); + gbc_checkEsFestival.gridx = 1; + gbc_checkEsFestival.gridy = 4; + panel.add(checkEsFestival, gbc_checkEsFestival); + checkEsFestival.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + compruebaFestival(); + } + }); + + JLabel lblNewLabel_3 = new JLabel("Grupo:"); + 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 = 5; + panel.add(lblNewLabel_3, gbc_lblNewLabel_3); + + jcbGrupo = new JComboBox(); + GridBagConstraints gbc_jcbGrupo = new GridBagConstraints(); + gbc_jcbGrupo.insets = new Insets(0, 0, 5, 0); + gbc_jcbGrupo.fill = GridBagConstraints.HORIZONTAL; + gbc_jcbGrupo.gridx = 1; + gbc_jcbGrupo.gridy = 5; + panel.add(jcbGrupo, gbc_jcbGrupo); + + JLabel lblNewLabel_4 = new JLabel("Sala:"); + GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints(); + gbc_lblNewLabel_4.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_4.insets = new Insets(0, 0, 0, 5); + gbc_lblNewLabel_4.gridx = 0; + gbc_lblNewLabel_4.gridy = 6; + panel.add(lblNewLabel_4, gbc_lblNewLabel_4); + + jcbSala = new JComboBox(); + GridBagConstraints gbc_jcbSala = new GridBagConstraints(); + gbc_jcbSala.fill = GridBagConstraints.HORIZONTAL; + gbc_jcbSala.gridx = 1; + gbc_jcbSala.gridy = 6; + panel.add(jcbSala, gbc_jcbSala); + + cargaTodosLosGrupos(); + cargaTodasLasSalas(); + + mostrarConciertoEnPantalla(ControladorConcierto.getPrimero()); + } + + /** + * + */ + private void compruebaFestival () { + if (checkEsFestival.isSelected()) { + checkEsFestival.setText("Es festival"); + } + else { + checkEsFestival.setText("No es festival"); + } + } + + /** + * + */ + private void cargaTodosLosGrupos() { + List grupos = ControladorGrupo.findAll(); + for (Grupo g : grupos) { + jcbGrupo.addItem(g); + } + } + + /** + * + */ + private void cargaTodasLasSalas() { + List salas = ControladorSala.findAll(); + for (Sala s : salas) { + jcbSala.addItem(s); + } + } + + /** + * + * @param c + */ + private void mostrarConciertoEnPantalla(Concierto c) { + if (c != null) { + jtfId.setText("" + c.getId()); + jtfFecha.setText(sdf.format(c.getFecha())); + jtfPrecio.setText("" + c.getPrecio()); + + checkEsFestival.setSelected(c.isFestival()); +// if (c.isFestival()) { +// checkEsFestival.setSelected(true); +// } +// else { +// checkEsFestival.setSelected(false); +// } + compruebaFestival(); + + for (int i = 0; i < jcbGrupo.getItemCount(); i++) { + Grupo grupoEnJCombo = (Grupo) jcbGrupo.getItemAt(i); + if (grupoEnJCombo.getId() == c.getIdGrupo()) { + jcbGrupo.setSelectedIndex(i); + } + } + + for (int i = 0; i < jcbSala.getItemCount(); i++) { + Sala salaEnJCombo = (Sala) jcbSala.getItemAt(i); + if (salaEnJCombo.getId() == c.getIdSala()) { + jcbSala.setSelectedIndex(i); + } + } + } + + } + + /** + * + */ + private void guardar() { + Concierto c = new Concierto(); + c.setId(Integer.parseInt(jtfId.getText())); + try { + c.setFecha(sdf.parse(jtfFecha.getText())); + } catch (ParseException e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(null, "El formato de la" + + " fecha debe ser dd/MM/yyyy"); + return; + } + + float precioConcierto = Float.parseFloat(jtfPrecio.getText()); + if (precioConcierto < 0 || precioConcierto >= 1000) { + lblErrorEnPrecio.setText("Precio debe estar entre 0 y 999.99"); + return; + } + else { + lblErrorEnPrecio.setText(""); + } + c.setPrecio(precioConcierto); + + c.setFestival(checkEsFestival.isSelected()); + + Grupo g = (Grupo) jcbGrupo.getSelectedItem(); + c.setIdGrupo(g.getId()); + + Sala s = (Sala) jcbSala.getSelectedItem(); + c.setIdSala(s.getId()); + + int registrosAfectados = ControladorConcierto.guardar(c); + if (registrosAfectados == 0) { + JOptionPane.showMessageDialog(null, "Error al guardar"); + } + else if (registrosAfectados == 1) { + if (c.getId() == 0) { // Se trata de una inserción + mostrarConciertoEnPantalla(ControladorConcierto.getUltimo()); + } + JOptionPane.showMessageDialog(null, "Guardado correctamente"); + } + } + + /** + * + */ + private void nuevo() { + jtfId.setText("0"); + jtfFecha.setText(sdf.format(new Date())); + jtfPrecio.setText("0"); + checkEsFestival.setSelected(false); + jcbGrupo.setSelectedIndex(0); + jcbSala.setSelectedIndex(0); + } + + /** + * + */ + private void eliminar() { + String [] opciones ={"Sí","No"}; + int eleccion = JOptionPane.showOptionDialog(null, + "¿Desea eliminar el concierto?","Eliminar concierto", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, null, opciones, "Sí"); + if (eleccion != JOptionPane.YES_OPTION) { + return; + } + + + int idConcierto = Integer.parseInt(jtfId.getText()); + int registrosAfectados = ControladorConcierto.eliminar(idConcierto); + if (registrosAfectados == 0) { + JOptionPane.showMessageDialog(null, "Error al eliminar"); + } + else if (registrosAfectados == 1) { + JOptionPane.showMessageDialog(null, "Eliminado correctamente"); + } + } +} + + + + + + + + diff --git a/src/tutorialJava/examenes/examen20250509/vista/VentanaPrincipal.java b/src/tutorialJava/examenes/examen20250509/vista/VentanaPrincipal.java new file mode 100644 index 0000000..c519fd2 --- /dev/null +++ b/src/tutorialJava/examenes/examen20250509/vista/VentanaPrincipal.java @@ -0,0 +1,65 @@ +package tutorialJava.examenes.examen20250509.vista; + +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import java.awt.BorderLayout; +import javax.swing.JLabel; +import java.awt.Font; +import javax.swing.SwingConstants; +import javax.swing.JTabbedPane; + +public class VentanaPrincipal extends JFrame { + + private static final long serialVersionUID = 1L; + private JPanel contentPane; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + VentanaPrincipal frame = new VentanaPrincipal(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the frame. + */ + public VentanaPrincipal() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 450, 300); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + + setContentPane(contentPane); + contentPane.setLayout(new BorderLayout(0, 0)); + + JLabel lblNewLabel = new JLabel("Gestión de conciertos"); + lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); + lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16)); + contentPane.add(lblNewLabel, BorderLayout.NORTH); + + JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); + contentPane.add(tabbedPane, BorderLayout.CENTER); + + PanelGestionConciertos panel1 = new PanelGestionConciertos(); + tabbedPane.addTab("New tab", null, panel1, null); + + JPanel panel2 = new JPanel(); + tabbedPane.addTab("New tab", null, panel2, null); + + JPanel panel3 = new JPanel(); + tabbedPane.addTab("New tab", null, panel3, null); + } + +}