From f0c090da0dfe49c347879b1d2d861c1e97332575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=20Mu=C3=B1oz?= Date: Mon, 31 Mar 2025 12:27:04 +0200 Subject: [PATCH] feat(ch 9 ex 2): inserted runnable objecto into JPanelDatosPersonales --- .../JPanelDatosPersonales.java | 74 ++++++++++++++++--- .../JPanelEstudiante.java | 59 ++++++++++++++- 2 files changed, 120 insertions(+), 13 deletions(-) diff --git a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo02_CentroEducativo/JPanelDatosPersonales.java b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo02_CentroEducativo/JPanelDatosPersonales.java index 75b49d1..6dad837 100644 --- a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo02_CentroEducativo/JPanelDatosPersonales.java +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo02_CentroEducativo/JPanelDatosPersonales.java @@ -6,6 +6,11 @@ import javax.swing.JLabel; import java.awt.GridBagConstraints; import javax.swing.JTextField; import java.awt.Insets; +import javax.swing.JToolBar; +import javax.swing.JButton; +import javax.swing.ImageIcon; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; public class JPanelDatosPersonales extends JPanel { @@ -13,6 +18,11 @@ public class JPanelDatosPersonales extends JPanel { private JTextField jtfId; private JTextField jtfNombre; private JTextField jtfAppelido1; + private JLabel lblNewLabel_3; + private JTextField textField; + private Runnable runNavegarPrimerRegistro; + private JToolBar toolBar; + private JButton btnNewButton; /** * Create the panel. @@ -20,17 +30,34 @@ public class JPanelDatosPersonales extends JPanel { public JPanelDatosPersonales() { GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0, 0}; - gridBagLayout.rowHeights = new int[]{0, 0, 0, 0}; + gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0}; gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; - gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE}; + gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; setLayout(gridBagLayout); + toolBar = new JToolBar(); + GridBagConstraints gbc_toolBar = new GridBagConstraints(); + gbc_toolBar.gridwidth = 2; + gbc_toolBar.insets = new Insets(0, 0, 5, 5); + gbc_toolBar.gridx = 0; + gbc_toolBar.gridy = 0; + add(toolBar, gbc_toolBar); + + btnNewButton = new JButton(""); + btnNewButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + runNavegarPrimerRegistro.run(); + } + }); + btnNewButton.setIcon(new ImageIcon(JPanelDatosPersonales.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/gotostart.png"))); + toolBar.add(btnNewButton); + JLabel lblNewLabel = new JLabel("Id:"); GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5); gbc_lblNewLabel.anchor = GridBagConstraints.EAST; gbc_lblNewLabel.gridx = 0; - gbc_lblNewLabel.gridy = 0; + gbc_lblNewLabel.gridy = 1; add(lblNewLabel, gbc_lblNewLabel); jtfId = new JTextField(); @@ -38,7 +65,7 @@ public class JPanelDatosPersonales extends JPanel { gbc_jtfId.insets = new Insets(0, 0, 5, 0); gbc_jtfId.fill = GridBagConstraints.HORIZONTAL; gbc_jtfId.gridx = 1; - gbc_jtfId.gridy = 0; + gbc_jtfId.gridy = 1; add(jtfId, gbc_jtfId); jtfId.setColumns(10); @@ -47,7 +74,7 @@ public class JPanelDatosPersonales extends JPanel { 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; + gbc_lblNewLabel_1.gridy = 2; add(lblNewLabel_1, gbc_lblNewLabel_1); jtfNombre = new JTextField(); @@ -55,25 +82,42 @@ public class JPanelDatosPersonales extends JPanel { gbc_jtfNombre.insets = new Insets(0, 0, 5, 0); gbc_jtfNombre.fill = GridBagConstraints.HORIZONTAL; gbc_jtfNombre.gridx = 1; - gbc_jtfNombre.gridy = 1; + gbc_jtfNombre.gridy = 2; add(jtfNombre, gbc_jtfNombre); jtfNombre.setColumns(10); JLabel lblNewLabel_2 = new JLabel("Apellido 1:"); GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints(); gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST; - gbc_lblNewLabel_2.insets = new Insets(0, 0, 0, 5); + gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5); gbc_lblNewLabel_2.gridx = 0; - gbc_lblNewLabel_2.gridy = 2; + gbc_lblNewLabel_2.gridy = 3; add(lblNewLabel_2, gbc_lblNewLabel_2); jtfAppelido1 = new JTextField(); GridBagConstraints gbc_jtfAppelido1 = new GridBagConstraints(); + gbc_jtfAppelido1.insets = new Insets(0, 0, 5, 0); gbc_jtfAppelido1.fill = GridBagConstraints.HORIZONTAL; gbc_jtfAppelido1.gridx = 1; - gbc_jtfAppelido1.gridy = 2; + gbc_jtfAppelido1.gridy = 3; add(jtfAppelido1, gbc_jtfAppelido1); jtfAppelido1.setColumns(10); + + lblNewLabel_3 = new JLabel("Apellido 2:"); + GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints(); + gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_3.insets = new Insets(0, 0, 0, 5); + gbc_lblNewLabel_3.gridx = 0; + gbc_lblNewLabel_3.gridy = 4; + add(lblNewLabel_3, gbc_lblNewLabel_3); + + textField = new JTextField(); + GridBagConstraints gbc_textField = new GridBagConstraints(); + gbc_textField.fill = GridBagConstraints.HORIZONTAL; + gbc_textField.gridx = 1; + gbc_textField.gridy = 4; + add(textField, gbc_textField); + textField.setColumns(10); } @@ -89,6 +133,18 @@ public class JPanelDatosPersonales extends JPanel { public int getId() { return Integer.parseInt(this.jtfId.getText()); } + + public void setNombre(String nombre) { + this.jtfNombre.setText(nombre); + } + + public String getNombre() { + return this.jtfNombre.getText(); + } + + public void setRunnableNavegarPrimerRegistro(Runnable runnable) { + this.runNavegarPrimerRegistro = runnable; + } } diff --git a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo02_CentroEducativo/JPanelEstudiante.java b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo02_CentroEducativo/JPanelEstudiante.java index cbe725c..948b167 100644 --- a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo02_CentroEducativo/JPanelEstudiante.java +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo02_CentroEducativo/JPanelEstudiante.java @@ -3,6 +3,18 @@ package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo02_CentroEducativo; import javax.swing.JPanel; import java.awt.BorderLayout; import javax.swing.JLabel; +import java.awt.GridBagLayout; +import javax.swing.JToolBar; + +import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Estudiante; + +import java.awt.GridBagConstraints; +import java.awt.Insets; +import java.awt.Font; +import javax.swing.JButton; +import javax.swing.ImageIcon; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; public class JPanelEstudiante extends JPanel { @@ -13,12 +25,51 @@ public class JPanelEstudiante extends JPanel { * Create the panel. */ public JPanelEstudiante() { - setLayout(new BorderLayout(0, 0)); + GridBagLayout gridBagLayout = new GridBagLayout(); + gridBagLayout.columnWidths = new int[]{0, 0}; + gridBagLayout.rowHeights = new int[]{0, 0, 0}; + gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE}; + gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE}; + setLayout(gridBagLayout); - JLabel lblNewLabel = new JLabel("Gestión de Estudiantes"); - add(lblNewLabel, BorderLayout.NORTH); - add(panelDatos, BorderLayout.CENTER); + JToolBar toolBar = new JToolBar(); + GridBagConstraints gbc_toolBar = new GridBagConstraints(); + gbc_toolBar.insets = new Insets(0, 0, 5, 0); + gbc_toolBar.gridx = 0; + gbc_toolBar.gridy = 0; + add(toolBar, gbc_toolBar); + + JButton btnNewButton = new JButton(""); + btnNewButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { +// Estudiante e = ControladorEstudiante.findFirst(); +// panelDatos.setId(e.getId()); +// panelDatos.setNombre(e.getNombre()); + } + }); + btnNewButton.setIcon(new ImageIcon(JPanelEstudiante.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/gotostart.png"))); + toolBar.add(btnNewButton); + + JLabel lblNewLabel = new JLabel("Gestión de estudiantes"); + lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14)); + GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); + gbc_lblNewLabel.gridx = 0; + gbc_lblNewLabel.gridy = 1; + add(lblNewLabel, gbc_lblNewLabel); + + GridBagConstraints gbc_PanelDatosPersonales = new GridBagConstraints(); + gbc_PanelDatosPersonales.gridx = 0; + gbc_PanelDatosPersonales.gridy = 2; + add(panelDatos, gbc_PanelDatosPersonales); + + panelDatos.setRunnableNavegarPrimerRegistro(new Runnable() { + public void run() { +// Estudiante e = ControladorEstudiante.findFirst(); +// panelDatos.setId(e.getId()); +// panelDatos.setNombre(e.getNombre()); + } + }); }