> 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/dialogonuevapersona.md).

# DialogoNuevaPersona

## PanelFormularioPersona

O PanelFormularioPersona conten todos os compoñentes necesarios para dar de alta a unha persoa nova na nosa interface:

```java
package vista;

import modelo.Persona;

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

public class PanelFormularioPersona extends JPanel {

    JTextField txtNombre;
    JTextField txtApellidos;
    JSpinner spFechaNacimiento;
    JComboBox<String> cbCiudad;
    JRadioButton rbHombre, rbMujer;
    ButtonGroup grupoSexo;

    private boolean editable;

    public PanelFormularioPersona(){
        this(true);
    }

    public PanelFormularioPersona(boolean editable) {
        this.editable = editable;
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints(); //Espacios al rededor de los componentes
        gbc.insets = new Insets(5,5,5,5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // Nombre
        gbc.gridx = 0; gbc.gridy = 0;
        add(new JLabel("Nombre:"), gbc);
        gbc.gridx = 1;
        txtNombre = new JTextField(15);
        txtNombre.setEditable(editable);
        add(txtNombre, gbc);

        // Apellidos
        gbc.gridx = 0; gbc.gridy = 1;
        add(new JLabel("Apellidos:"), gbc);
        gbc.gridx = 1;
        txtApellidos = new JTextField(15);
        txtApellidos.setEditable(editable);
        add(txtApellidos, gbc);

        // Fecha de nacimiento
        gbc.gridx = 0; gbc.gridy = 2;
        add(new JLabel("Fecha de nacimiento:"), gbc);
        gbc.gridx = 1;
        spFechaNacimiento = new JSpinner(new SpinnerDateModel());
        spFechaNacimiento.setEnabled(editable);
        spFechaNacimiento.setEditor(new JSpinner.DateEditor(spFechaNacimiento, "dd/MM/yyyy"));
        add(spFechaNacimiento, gbc);

        // Ciudad
        gbc.gridx = 0; gbc.gridy = 3;
        add(new JLabel("Ciudad:"), gbc);
        gbc.gridx = 1;
        cbCiudad = new JComboBox<>(new String[]{"Lugo","Coruña","Ourense","Pontevedra","Vigo","Santiago","Ferrol"});
        cbCiudad.setEditable(editable);
        add(cbCiudad, gbc);

        // Sexo
        gbc.gridx = 0; gbc.gridy = 4;
        add(new JLabel("Sexo:"), gbc);
        gbc.gridx = 1;
        JPanel panelSexo = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
        rbHombre = new JRadioButton("Hombre");
        rbHombre.setEnabled(editable);
        rbMujer = new JRadioButton("Mujer");
        rbMujer.setEnabled(editable);
        grupoSexo = new ButtonGroup();
        grupoSexo.add(rbHombre);
        grupoSexo.add(rbMujer);
        panelSexo.add(rbHombre);
        panelSexo.add(rbMujer);
        add(panelSexo, gbc);

    }

    public Persona crearPersona() {
        String nombre = txtNombre.getText();
        String apellidos = txtApellidos.getText();
        String fecha = new java.text.SimpleDateFormat("dd/MM/yyyy").format(spFechaNacimiento.getValue());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate fechaLD = LocalDate.parse(fecha, formatter);
        String ciudad = (String) cbCiudad.getSelectedItem();
        String sexo = rbHombre.isSelected() ? "Hombre" : "Mujer";

        return new Persona(nombre, apellidos, fechaLD, ciudad, sexo);
    }
}

```

```java

import modelo.Persona;

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

public class DialogoNuevaPersona extends JDialog {

    private PanelFormularioPersona panelFormulario; // Panel con los campos
    private JButton btnGuardar;

    public DialogoNuevaPersona(JFrame padre, PanelCentral panelCentral){
        super(padre, "Nueva Persona", true);

        // Panel con el formulario
        panelFormulario = new PanelFormularioPersona();
        add(panelFormulario, BorderLayout.CENTER);

        // Panel para el botón
        btnGuardar = new JButton("Guardar");

        // Listener clásico
        btnGuardar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Crear Persona desde los datos del formulario
                Persona persona = panelFormulario.crearPersona();
                // Añadir la persona al PanelCentral (tabla)
                panelCentral.añadirPersona(persona);
                // Cerrar el diálogo
                dispose();
            }
        });
        JPanel panelBoton = new JPanel();
        panelBoton.add(btnGuardar);
        add(panelBoton, BorderLayout.SOUTH);
        pack(); // Ajusta tamaño al contenido
        setLocationRelativeTo(padre); // Centrar sobre la ventana principal
    }
}

```

## VentanaPrincipal

Posteriormente imos a engadir na nosa VentanaPrincipal un ActionListener para capturar os clicks que se realizan no boton de "Nueva Persona"

```java
package vista;

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


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);

        panelInferior.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(VentanaPrincipal.this, panelCentro);
                dialogo.setVisible(true);
            }
        });

        setVisible(true);
    }

}

```


---

# 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/dialogonuevapersona.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.
