> 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/ud5-entrada-e-saida-da-informacion/interfaces-graficas/exemplo-de-interface-grafica-para-xestion-de-persoas/modelo-vista-controlador.md).

# Modelo Vista Controlador

A idea de esta fase intermedia e engadir un controlador ao noso código anterior, de tal maneira que eliminemos toda a xestión de eventos da parte da vista. Para isto imos a crear un Controlador de Persoas e imos actualizar VentanaPrincipal e instanciar o noso controlador na App

## ControladorPersonas

```java
package Controller;

import modelo.Persona;
import vista.DialogoNuevaPersona;
import vista.DialogoVisualizarPersona;
import vista.VentanaPrincipal;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ControladorPersonas {

    private VentanaPrincipal vista;

    public ControladorPersonas(VentanaPrincipal vista){
        this.vista=vista;
        this.inicializar();
    }

    private void inicializar(){
        cargarTabla();

        //Gestionamos los eventos asociados al boton de crear personas
        vista.getPanelInferior().getBtnNuevaPersona().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Abrimos el diálogo pasando la ventana y el panel central
                DialogoNuevaPersona dialogo = new DialogoNuevaPersona(vista, vista.getPanelCentro());
                dialogo.setVisible(true);
                cargarTabla();
            }
        });

        vista.getPanelCentro().getTabla().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int fila = vista.getPanelCentro().getTabla().getSelectedRow();
                Persona persona = vista.getPanelCentro().getPersona(fila);
                DialogoVisualizarPersona dialogo = new DialogoVisualizarPersona(vista, persona);
                dialogo.setVisible(true);
            }
        });
    }

    private void cargarTabla(){

    }

}

```

## VentanaPrincipal

A idea principal neste punto é que **a vista NON xestiona os eventos, é unha responsabilidade do controlador.**

```java
package vista;

import modelo.Persona;

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


public class VentanaPrincipal extends JFrame {

    private PanelSuperior panelSuperior;
    private PanelCentral panelCentro;
    private PanelInferior panelInferior;

    public VentanaPrincipal(){
        setTitle("Gestión de Personas");
        setSize(600,400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        this.panelSuperior = new PanelSuperior();
        this.panelCentro = new PanelCentral();
        this.panelInferior = new PanelInferior();

        panelSuperior.setBackground(Color.GREEN);
        panelCentro.setBackground(Color.YELLOW);
        panelInferior.setBackground(Color.CYAN);
        //Dimensiones
        panelSuperior.setPreferredSize(new Dimension(600, 80));
        panelInferior.setPreferredSize(new Dimension(600, 60));
        //Añadimos los paneles al frame
        this.add(panelSuperior, BorderLayout.NORTH);
        this.add(panelCentro, BorderLayout.CENTER);
        this.add(panelInferior, BorderLayout.SOUTH);
        
        setVisible(true);
    }

    public PanelSuperior getPanelSuperior() {
        return panelSuperior;
    }

    public PanelCentral getPanelCentro() {
        return panelCentro;
    }

    public PanelInferior getPanelInferior() {
        return panelInferior;
    }
}

```

## App

Na clase principal imos ter que inicializar a nosa vista e o noso controlador.

```java
public class App {

    public static void main(String[] args) {
        VentanaPrincipal ventana = new VentanaPrincipal();
        ControladorPersonas controladorPersonas = new ControladorPersonas(ventana);
    }
}
```


---

# 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/ud5-entrada-e-saida-da-informacion/interfaces-graficas/exemplo-de-interface-grafica-para-xestion-de-persoas/modelo-vista-controlador.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.
