feat(ch 9 ex 2): inserted runnable objecto into JPanelDatosPersonales

This commit is contained in:
Rafa Muñoz
2025-03-31 12:27:04 +02:00
parent abf5adcfe9
commit f0c090da0d
2 changed files with 120 additions and 13 deletions

View File

@@ -6,6 +6,11 @@ import javax.swing.JLabel;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import javax.swing.JTextField; import javax.swing.JTextField;
import java.awt.Insets; 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 { public class JPanelDatosPersonales extends JPanel {
@@ -13,6 +18,11 @@ public class JPanelDatosPersonales extends JPanel {
private JTextField jtfId; private JTextField jtfId;
private JTextField jtfNombre; private JTextField jtfNombre;
private JTextField jtfAppelido1; private JTextField jtfAppelido1;
private JLabel lblNewLabel_3;
private JTextField textField;
private Runnable runNavegarPrimerRegistro;
private JToolBar toolBar;
private JButton btnNewButton;
/** /**
* Create the panel. * Create the panel.
@@ -20,17 +30,34 @@ public class JPanelDatosPersonales extends JPanel {
public JPanelDatosPersonales() { public JPanelDatosPersonales() {
GridBagLayout gridBagLayout = new GridBagLayout(); GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0}; 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.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); 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:"); JLabel lblNewLabel = new JLabel("Id:");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5); gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.anchor = GridBagConstraints.EAST; gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel.gridx = 0; gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0; gbc_lblNewLabel.gridy = 1;
add(lblNewLabel, gbc_lblNewLabel); add(lblNewLabel, gbc_lblNewLabel);
jtfId = new JTextField(); jtfId = new JTextField();
@@ -38,7 +65,7 @@ public class JPanelDatosPersonales extends JPanel {
gbc_jtfId.insets = new Insets(0, 0, 5, 0); gbc_jtfId.insets = new Insets(0, 0, 5, 0);
gbc_jtfId.fill = GridBagConstraints.HORIZONTAL; gbc_jtfId.fill = GridBagConstraints.HORIZONTAL;
gbc_jtfId.gridx = 1; gbc_jtfId.gridx = 1;
gbc_jtfId.gridy = 0; gbc_jtfId.gridy = 1;
add(jtfId, gbc_jtfId); add(jtfId, gbc_jtfId);
jtfId.setColumns(10); jtfId.setColumns(10);
@@ -47,7 +74,7 @@ public class JPanelDatosPersonales extends JPanel {
gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST; gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5); gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1.gridx = 0; gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 1; gbc_lblNewLabel_1.gridy = 2;
add(lblNewLabel_1, gbc_lblNewLabel_1); add(lblNewLabel_1, gbc_lblNewLabel_1);
jtfNombre = new JTextField(); jtfNombre = new JTextField();
@@ -55,25 +82,42 @@ public class JPanelDatosPersonales extends JPanel {
gbc_jtfNombre.insets = new Insets(0, 0, 5, 0); gbc_jtfNombre.insets = new Insets(0, 0, 5, 0);
gbc_jtfNombre.fill = GridBagConstraints.HORIZONTAL; gbc_jtfNombre.fill = GridBagConstraints.HORIZONTAL;
gbc_jtfNombre.gridx = 1; gbc_jtfNombre.gridx = 1;
gbc_jtfNombre.gridy = 1; gbc_jtfNombre.gridy = 2;
add(jtfNombre, gbc_jtfNombre); add(jtfNombre, gbc_jtfNombre);
jtfNombre.setColumns(10); jtfNombre.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("Apellido 1:"); JLabel lblNewLabel_2 = new JLabel("Apellido 1:");
GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints(); GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST; 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.gridx = 0;
gbc_lblNewLabel_2.gridy = 2; gbc_lblNewLabel_2.gridy = 3;
add(lblNewLabel_2, gbc_lblNewLabel_2); add(lblNewLabel_2, gbc_lblNewLabel_2);
jtfAppelido1 = new JTextField(); jtfAppelido1 = new JTextField();
GridBagConstraints gbc_jtfAppelido1 = new GridBagConstraints(); GridBagConstraints gbc_jtfAppelido1 = new GridBagConstraints();
gbc_jtfAppelido1.insets = new Insets(0, 0, 5, 0);
gbc_jtfAppelido1.fill = GridBagConstraints.HORIZONTAL; gbc_jtfAppelido1.fill = GridBagConstraints.HORIZONTAL;
gbc_jtfAppelido1.gridx = 1; gbc_jtfAppelido1.gridx = 1;
gbc_jtfAppelido1.gridy = 2; gbc_jtfAppelido1.gridy = 3;
add(jtfAppelido1, gbc_jtfAppelido1); add(jtfAppelido1, gbc_jtfAppelido1);
jtfAppelido1.setColumns(10); 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() { public int getId() {
return Integer.parseInt(this.jtfId.getText()); 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;
}
} }

View File

@@ -3,6 +3,18 @@ package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo02_CentroEducativo;
import javax.swing.JPanel; import javax.swing.JPanel;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import javax.swing.JLabel; 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 { public class JPanelEstudiante extends JPanel {
@@ -13,12 +25,51 @@ public class JPanelEstudiante extends JPanel {
* Create the panel. * Create the panel.
*/ */
public JPanelEstudiante() { 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"); JToolBar toolBar = new JToolBar();
add(lblNewLabel, BorderLayout.NORTH); GridBagConstraints gbc_toolBar = new GridBagConstraints();
add(panelDatos, BorderLayout.CENTER); 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());
}
});
} }