Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package vistas; import controlador.BuscarDomicilio; import controlador.ControladorGrupoFamiliar; import controlador.ControladorUsuario; import modelo.Usuario; import java.awt.event.KeyEvent; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.JTable; import modelo.GestionConexion; import org.apache.commons.codec.digest.DigestUtils; /** * * @author martinrh83 */ public class GestionPersonal extends javax.swing.JFrame { private GestionConexion connection; private Usuario user; private ControladorUsuario control; private ModPersonal window; /** * Creates new form AltaProducto */ public GestionPersonal(ControladorUsuario cont, GestionConexion conn, Usuario us) { control = cont; connection = conn; user = us; initComponents(); this.setLocationRelativeTo(null); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); txt_idUser = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); txt_Name = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); btnFin_APer = new javax.swing.JButton(); txt_apellido = new javax.swing.JTextField(); jLabel10 = new javax.swing.JLabel(); txt_Dni = new javax.swing.JTextField(); jLabel11 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); txt_user = new javax.swing.JTextField(); combo_tipo = new javax.swing.JComboBox(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); txt_hora = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); tablaUsuario = new javax.swing.JTable(); jDateChooser1 = new com.toedter.calendar.JDateChooser(); btnCancelar_APer = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); btn_delete = new javax.swing.JButton(); btnAgregar_APer = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); txt_pass = new javax.swing.JPasswordField(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Gestion Personal"); setResizable(false); jLabel1.setText("ID usuario:"); txt_idUser.setEnabled(false); jLabel2.setText("Nombre"); txt_Name.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); txt_Name.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txt_NameKeyTyped(evt); } }); jLabel3.setText("Tipo:"); btnFin_APer.setText("Finalizar"); btnFin_APer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnFin_APerActionPerformed(evt); } }); txt_apellido.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); txt_apellido.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txt_apellidoKeyTyped(evt); } }); jLabel10.setText("Apellido:"); txt_Dni.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txt_DniKeyTyped(evt); } }); jLabel11.setText("DNI"); jLabel5.setText("Usuario:"); jLabel6.setText("Contrasea:"); combo_tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Empleado", "Administrador" })); jLabel7.setText("Carga Horaria:"); jLabel8.setText("Fecha Ingreso:"); txt_hora.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txt_horaKeyTyped(evt); } }); tablaUsuario .setModel(new javax.swing.table.DefaultTableModel(new Object[][] { {}, {}, {}, {} }, new String[] { })); jScrollPane1.setViewportView(tablaUsuario); btnCancelar_APer.setText("Cancelar"); btnCancelar_APer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelar_APerActionPerformed(evt); } }); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Operaciones")); btn_delete.setText("Eliminar"); btn_delete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_deleteActionPerformed(evt); } }); btnAgregar_APer.setText("Agregar"); btnAgregar_APer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAgregar_APerActionPerformed(evt); } }); jButton1.setText("Modificar"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(btnAgregar_APer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btn_delete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(5, 5, 5).addComponent(btnAgregar_APer) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btn_delete).addContainerGap(13, Short.MAX_VALUE))); jButton2.setText("Administrar Grupo Familiar"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(50, 50, 50) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE) .addComponent(btnCancelar_APer).addGap(32, 32, 32).addComponent(btnFin_APer) .addGap(90, 90, 90)) .addGroup(layout.createSequentialGroup().addGap(16, 16, 16).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txt_user, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE) .addComponent(txt_idUser, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE) .addComponent(txt_Name, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE) .addComponent(txt_Dni))) .addComponent(combo_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6).addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txt_apellido, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE) .addComponent(txt_pass))) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel8) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup().addComponent(jLabel7) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txt_hora, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, Short.MAX_VALUE))) .addGap(18, 18, 18) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap().addComponent(jScrollPane1).addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup().addGap(24, 24, 24).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1) .addComponent(txt_idUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(txt_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6).addComponent(txt_pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txt_Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txt_apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel11) .addComponent(txt_Dni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8)) .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(19, 19, 19) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(combo_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7).addComponent(txt_hora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30)) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(7, 7, 7) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnCancelar_APer) .addComponent(btnFin_APer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents public JTable getTablaUsuario() { return tablaUsuario; } public void setTablaUsuario(JTable tablaUsuario) { this.tablaUsuario = tablaUsuario; } public void limpiar() { txt_Dni.setText(null); txt_Name.setText(null); txt_apellido.setText(null); txt_hora.setText(null); txt_pass.setText(null); txt_user.setText(null); } private void btnAgregar_APerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregar_APerActionPerformed user.setIdUsuario(Integer.valueOf(txt_idUser.getText())); user.setUserUsuario(txt_user.getText()); String password = txt_pass.getText(); String PassCrypto = DigestUtils.md5Hex(password); user.setPassUsuario(PassCrypto); user.setNomUsuario(txt_Name.getText()); user.setApellidoUsuario(txt_apellido.getText()); user.setDniUsuario(Integer.valueOf(txt_Dni.getText())); user.setTipoUsuario(combo_tipo.getSelectedItem().toString()); user.setCargaHoraria(Integer.valueOf(txt_hora.getText())); Date fechaIngreso = jDateChooser1.getDate(); SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy"); user.setFechaIngreso(formato.format(fechaIngreso)); this.control.agregarUsuario(); this.limpiar(); this.control.Generarnumeracion(); this.control.mostrarusuarios(); //this.dispose(); }//GEN-LAST:event_btnAgregar_APerActionPerformed private void txt_DniKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_DniKeyTyped char car = evt.getKeyChar(); if (txt_Dni.getText().length() >= 9) { JOptionPane.showMessageDialog(this, "Permitido hasta 8 digitos"); evt.consume(); } if ((car < '0' || car > '9')) { JOptionPane.showMessageDialog(this, "Solo numeros"); evt.consume(); } }//GEN-LAST:event_txt_DniKeyTyped private void txt_NameKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_NameKeyTyped char car = evt.getKeyChar(); if ((car < 'a' || car > 'z') && (car < 'A' || car > 'Z') && (car != (char) KeyEvent.VK_SPACE) && (car != (char) KeyEvent.VK_BACK_SPACE)) { JOptionPane.showMessageDialog(this, "Ingrese solo letras"); evt.consume(); } }//GEN-LAST:event_txt_NameKeyTyped private void txt_apellidoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_apellidoKeyTyped char car = evt.getKeyChar(); if ((car < 'a' || car > 'z') && (car < 'A' || car > 'Z') && (car != (char) KeyEvent.VK_SPACE) && (car != (char) KeyEvent.VK_BACK_SPACE)) { JOptionPane.showMessageDialog(this, "Ingrese solo letras"); evt.consume(); } }//GEN-LAST:event_txt_apellidoKeyTyped private void txt_horaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_horaKeyTyped char car = evt.getKeyChar(); if (txt_hora.getText().length() >= 8) { JOptionPane.showMessageDialog(this, "Permitido hasta 8 digitos"); evt.consume(); } if ((car < '0' || car > '9') && (car != (char) KeyEvent.VK_BACK_SPACE)) { JOptionPane.showMessageDialog(this, "Solo numeros"); evt.consume(); } }//GEN-LAST:event_txt_horaKeyTyped private void btn_deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_deleteActionPerformed control.eliminarFila(this); control.Generarnumeracion(); control.mostrarusuarios(); }//GEN-LAST:event_btn_deleteActionPerformed private void btnFin_APerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFin_APerActionPerformed this.dispose(); }//GEN-LAST:event_btnFin_APerActionPerformed private void btnCancelar_APerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelar_APerActionPerformed this.limpiar(); }//GEN-LAST:event_btnCancelar_APerActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed int idUser = control.administrarGFamiliar(this); ControladorGrupoFamiliar grFam = new ControladorGrupoFamiliar(connection, idUser); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed try { window = new ModPersonal(control, connection, user); window.setVisible(true); } catch (ParseException ex) { Logger.getLogger(GestionPersonal.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnAgregar_APer; private javax.swing.JButton btnCancelar_APer; private javax.swing.JButton btnFin_APer; private javax.swing.JButton btn_delete; private javax.swing.JComboBox combo_tipo; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private com.toedter.calendar.JDateChooser jDateChooser1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JPanel jPanel1; public javax.swing.JScrollPane jScrollPane1; public javax.swing.JTable tablaUsuario; private javax.swing.JTextField txt_Dni; private javax.swing.JTextField txt_Name; private javax.swing.JTextField txt_apellido; private javax.swing.JTextField txt_hora; public javax.swing.JTextField txt_idUser; private javax.swing.JPasswordField txt_pass; private javax.swing.JTextField txt_user; // End of variables declaration//GEN-END:variables }