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 interfaces; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.MongoException; import java.awt.event.KeyEvent; import java.awt.Image; import java.awt.event.KeyEvent; import java.net.UnknownHostException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JOptionPane; /** * * @author MarioJavier */ public class Rsuarios extends javax.swing.JFrame { DB db; DBCollection tabla; public Rsuarios() { try { //realizamos la conexion de la base de datos MOngoDB Mongo mongo = new Mongo("localhost", 27017); db = mongo.getDB("basedadatos"); tabla = db.getCollection("usuario"); } catch (UnknownHostException ex) { Logger.getLogger(Rsuarios.class.getName()).log(Level.SEVERE, null, ex); } catch (MongoException ex) { Logger.getLogger(Rsuarios.class.getName()).log(Level.SEVERE, null, ex); } //codigo pra centrar, quitar margen y transarencia del formulario this.setUndecorated(true); initComponents(); //this.setLocationRelativeTo(null); //this.setOpacity(0.9f); } /** * 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() { jPanel2 = new javax.swing.JPanel(); jSeparator2 = new javax.swing.JSeparator(); jPanel1 = new javax.swing.JPanel(); jLabel14 = new javax.swing.JLabel(); jSeparator3 = new javax.swing.JSeparator(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jPanel3 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); txtnom = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); txtape = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); txtcd = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); txtciu = new javax.swing.JTextField(); jLabel9 = new javax.swing.JLabel(); txtedad = new javax.swing.JTextField(); jLabel10 = new javax.swing.JLabel(); cbnsexo = new javax.swing.JComboBox(); jSeparator1 = new javax.swing.JSeparator(); jPanel4 = new javax.swing.JPanel(); jLabel8 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel2.setBackground(new java.awt.Color(255, 255, 255)); jPanel2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true)); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true)); jLabel14.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel14.setText("ACCIONES"); jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jButton1.setText("Guardar"); jButton1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jButton2.setText("Reportes"); jButton2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true)); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jButton3.setText("Mostrar"); jButton3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true)); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jButton4.setText("Eliminar"); jButton4.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true)); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(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.LEADING) .addComponent(jLabel14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSeparator3) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel14) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(jButton4)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3).addComponent(jButton2)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel3.setBackground(new java.awt.Color(255, 255, 255)); jPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true)); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N jLabel2.setText("Registrar Usuarios"); jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel3.setText("Nombres:"); txtnom.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true)); txtnom.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtnomActionPerformed(evt); } }); txtnom.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txtnomKeyTyped(evt); } }); jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel4.setText("Apellidos:"); txtape.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true)); txtape.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txtapeKeyTyped(evt); } }); jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel5.setText("Cdula:"); txtcd.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true)); txtcd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtcdActionPerformed(evt); } }); txtcd.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txtcdKeyTyped(evt); } }); jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel6.setText("Ciudad:"); txtciu.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true)); txtciu.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtciuActionPerformed(evt); } }); txtciu.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txtciuKeyTyped(evt); } }); jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel9.setText("Edad:"); txtedad.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true)); txtedad.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtedadActionPerformed(evt); } }); txtedad.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txtedadKeyTyped(evt); } }); jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel10.setText("Sexo:"); cbnsexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "M", "F" })); cbnsexo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cbnsexoActionPerformed(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(20, 20, 20) .addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6) .addComponent(txtciu, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2))) .addGroup(jPanel3Layout.createSequentialGroup().addGap(244, 244, 244) .addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtedad, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9).addComponent(jLabel10).addComponent(cbnsexo, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap(22, Short.MAX_VALUE)) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(19, 19, 19).addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3).addComponent(jLabel4).addComponent(jLabel5) .addComponent(txtcd, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(txtnom, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE) .addComponent(txtape, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))) .addContainerGap(247, Short.MAX_VALUE)))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(13, 13, 13).addComponent(jLabel9) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtedad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel10) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cbnsexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(47, 47, 47).addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtciu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(70, 70, 70).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtnom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtape, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtcd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(50, Short.MAX_VALUE)))); jPanel4.setBackground(new java.awt.Color(255, 255, 255)); jPanel4.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true)); jLabel8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel8.setText("Registros:"); jTextArea1.setColumns(20); jTextArea1.setRows(5); jTextArea1.setBorder(null); jScrollPane1.setViewportView(jTextArea1); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout .setHorizontalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addComponent(jLabel8) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(jScrollPane1)) .addContainerGap())); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap().addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE) .addContainerGap())); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jSeparator2) .addGroup(jPanel2Layout.createSequentialGroup().addGap(4, 4, 4) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 16, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void txtnomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnomActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txtnomActionPerformed private void txtnomKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnomKeyTyped // en este envento realizaremos la validacion de los campos para solo letras. char teclas; teclas = evt.getKeyChar(); if (!Character.isLetter(teclas) && teclas != KeyEvent.VK_SPACE && teclas != KeyEvent.VK_BACK_SPACE) { evt.consume(); getToolkit().beep();//emite sonido } }//GEN-LAST:event_txtnomKeyTyped private void txtapeKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtapeKeyTyped // TODO add your handling code here: // en este envento realizaremos la validacion de los campos para solo letras. char teclas; teclas = evt.getKeyChar(); if (!Character.isLetter(teclas) && teclas != KeyEvent.VK_SPACE && teclas != KeyEvent.VK_BACK_SPACE) { evt.consume(); getToolkit().beep();//emite sonido } }//GEN-LAST:event_txtapeKeyTyped private void txtcdKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtcdKeyTyped // en este envento realizaremos la validacion de los campos para solo numeros. char teclas; teclas = evt.getKeyChar(); if (!Character.isDigit(teclas) && teclas != KeyEvent.VK_BACK_SPACE) { evt.consume(); getToolkit().beep();//emite sonido } ///validamos limite de caracteres para este campo int numeroingreso = 10; if (txtcd.getText().length() >= numeroingreso) { evt.consume(); JOptionPane.showMessageDialog(null, "Limite Superado ", "Mensaje", JOptionPane.INFORMATION_MESSAGE); } }//GEN-LAST:event_txtcdKeyTyped private void txtciuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtciuActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txtciuActionPerformed private void txtciuKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtciuKeyTyped // en este envento realizaremos la validacion de los campos para solo letras. char teclas; teclas = evt.getKeyChar(); if (!Character.isLetter(teclas) && teclas != KeyEvent.VK_SPACE && teclas != KeyEvent.VK_BACK_SPACE) { evt.consume(); getToolkit().beep();//emite sonido } }//GEN-LAST:event_txtciuKeyTyped private void txtedadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtedadActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txtedadActionPerformed private void txtedadKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtedadKeyTyped // en este envento realizaremos la validacion de los campos para solo numeros. char teclas; teclas = evt.getKeyChar(); if (!Character.isDigit(teclas) && teclas != KeyEvent.VK_BACK_SPACE) { evt.consume(); getToolkit().beep();//emite sonido } ///validamos limite de caracteres para este campo int numeroingreso = 2; if (txtedad.getText().length() >= numeroingreso) { evt.consume(); JOptionPane.showMessageDialog(null, "Limite Superado ", "Mensaje", JOptionPane.INFORMATION_MESSAGE); } }//GEN-LAST:event_txtedadKeyTyped private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed //manera completa de validacion de los campos vacios if (txtnom.getText().isEmpty() || txtape.getText().isEmpty() || txtcd.getText().isEmpty() || txtciu.getText().isEmpty() || txtedad.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "Existen campos vacios", "Mensaje", JOptionPane.ERROR_MESSAGE); } else { //realizamos la insercion de datos a la base de datos mongodb BasicDBObject document = new BasicDBObject(); document.put("nombre", "'" + txtnom.getText() + "'"); document.put("apellido", "'" + txtape.getText() + "'"); document.put("cedula", "'" + txtcd.getText() + "'"); document.put("edad", "'" + txtedad.getText() + "'"); document.put("sexo", "'" + txtcd.getText() + "'"); document.put("ciudad", "'" + txtciu.getText() + "'"); tabla.insert(document); } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed }//GEN-LAST:event_jButton2ActionPerformed private void txtcdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcdActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txtcdActionPerformed private void cbnsexoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbnsexoActionPerformed // TODO add your handling code here: }//GEN-LAST:event_cbnsexoActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // mostrar los datos en la base de datos DBCursor curso = tabla.find(); while (curso.hasNext()) { jTextArea1.setText(jTextArea1.getText() + "\n" + curso.next()); } }//GEN-LAST:event_jButton3ActionPerformed private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jButton4ActionPerformed //metodo para limpiar las cajas de texto public void limpiar() { this.txtcd.setText(""); this.txtnom.setText(""); this.txtape.setText(""); this.txtedad.setText(""); this.txtciu.setText(""); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Rsuarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Rsuarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Rsuarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Rsuarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Rsuarios().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox cbnsexo; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField txtape; private javax.swing.JTextField txtcd; private javax.swing.JTextField txtciu; private javax.swing.JTextField txtedad; private javax.swing.JTextField txtnom; // End of variables declaration//GEN-END:variables }