> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud8-solucions/mvc-model-view-controller.md).

# MVC (Model View Controller)

```java
package Vista;

import Modelo.Profesor;

import javax.swing.*;
import java.awt.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class VistaCrearProfesor extends JFrame {

    private JLabel nombre, correo, fecha;
    private JTextField nomCampo, valCorreo, valFecha;
    private JButton btnGuardar;
    private JPanel panelSuperior, panelInferior;

    public VistaCrearProfesor(){
        setTitle("Crear Profesor");
        setSize(400, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Panel superior
        this.panelSuperior = new JPanel(new GridLayout(3,2));
        //Panel superior nombre
        this.nombre = new JLabel("Nombre: ");
        this.nomCampo = new JTextField();
        this.nomCampo.setSize(new Dimension(200,40));
        this.panelSuperior.add(this.nombre);
        this.panelSuperior.add(this.nomCampo);
        //Panel superior correo
        this.correo = new JLabel("Correo: ");
        this.valCorreo = new JTextField();
        this.valCorreo.setSize(new Dimension(200,40));
        this.panelSuperior.add(this.correo);
        this.panelSuperior.add(this.valCorreo);
        //Panel superior fecha
        this.fecha = new JLabel("Fecha: ");
        this.valFecha = new JTextField("dd/MM/yyyyy");
        this.valFecha.setSize(new Dimension(200,40));
        this.panelSuperior.add(this.fecha);
        this.panelSuperior.add(this.valFecha);
        //Panel inferior
        this.panelInferior = new JPanel(new GridBagLayout());
        //Boton
        this.btnGuardar = new JButton("Guardar");
        this.panelInferior.add(this.btnGuardar);
        //Ventana principal
        this.setLayout(new BorderLayout());
        this.add(this.panelSuperior);
        this.add(this.panelInferior, BorderLayout.SOUTH);

        setVisible(true);
    }

    public JLabel getNombre() {
        return nombre;
    }

    public void setNombre(JLabel nombre) {
        this.nombre = nombre;
    }

    public JLabel getCorreo() {
        return correo;
    }

    public void setCorreo(JLabel correo) {
        this.correo = correo;
    }

    public JLabel getFecha() {
        return fecha;
    }

    public void setFecha(JLabel fecha) {
        this.fecha = fecha;
    }

    public JTextField getValCorreo() {
        return valCorreo;
    }

    public void setValCorreo(JTextField valCorreo) {
        this.valCorreo = valCorreo;
    }

    public JTextField getNomCampo() {
        return nomCampo;
    }

    public void setNomCampo(JTextField nomCampo) {
        this.nomCampo = nomCampo;
    }

    public JTextField getValFecha() {
        return valFecha;
    }

    public void setValFecha(JTextField valFecha) {
        this.valFecha = valFecha;
    }

    public JButton getBtnGuardar() {
        return btnGuardar;
    }

    public void setBtnGuardar(JButton btnGuardar) {
        this.btnGuardar = btnGuardar;
    }

    public JPanel getPanelSuperior() {
        return panelSuperior;
    }

    public void setPanelSuperior(JPanel panelSuperior) {
        this.panelSuperior = panelSuperior;
    }

    public JPanel getPanelInferior() {
        return panelInferior;
    }

    public void setPanelInferior(JPanel panelInferior) {
        this.panelInferior = panelInferior;
    }

    public Profesor recuperarProfesor(){
        return new Profesor(0,
                this.nomCampo.getText(),
                this.valCorreo.getText(),
                LocalDate.parse(this.valFecha.getText(), DateTimeFormatter.ofPattern("dd/MM/yyyy")),
                1);
    }

    public void profesorCreadoCorrectamente(int id){
        JOptionPane.showMessageDialog(null,"Usuario insertado correctamente. ID: " + id);
    }

    public void errorCreandoProfesor(){
        JOptionPane.showMessageDialog(null,"Error al crear el profesor");
    }
}

```

```java
package Controlador;

import DAO.ProfesorDAOPrepStat;
import Modelo.Profesor;
import Vista.VistaCrearProfesor;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ControladorCrearProfesor {

    private VistaCrearProfesor vista;
    private ProfesorDAOPrepStat modelo;

    public ControladorCrearProfesor(VistaCrearProfesor vista, ProfesorDAOPrepStat modelo) {
        this.vista = vista;
        this.modelo = modelo;

        this.vista.getBtnGuardar().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Profesor nuevo = vista.recuperarProfesor();
                int res = modelo.insert(nuevo);
                if (res>0)
                    vista.profesorCreadoCorrectamente(res);
                else
                    vista.errorCreandoProfesor();
            }
        });
    }


}

```

```java
public class App {

    public static void main(String[] args) {
        VistaCrearProfesor vista = new VistaCrearProfesor();
        ProfesorDAOPrepStat modelo = new ProfesorDAOPrepStat();
        ControladorCrearProfesor controladorCrearProfesor = new ControladorCrearProfesor(vista,modelo);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud8-solucions/mvc-model-view-controller.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
