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 payroll; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import java.awt.Color; import java.awt.Container; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.table.DefaultTableModel; /** * * @author ayu */ public class FrmAdmin extends javax.swing.JFrame { /** * Creates new form Admin1 */ static String Id; public FrmAdmin() { initComponents(); this.getContentPane().setBackground(Color.YELLOW); setDefaultCloseOperation(this.HIDE_ON_CLOSE); } /** * 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() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jPanel1 = new javax.swing.JPanel(); btnEdit = new javax.swing.JButton(); btnSalary = new javax.swing.JButton(); btnDelete = new javax.swing.JButton(); jScrollPane3 = new javax.swing.JScrollPane(); tblRecords = new javax.swing.JTable(); btnSearch = new javax.swing.JButton(); txtid = new javax.swing.JTextField(); btnDisplay = new javax.swing.JButton(); btnInsert = new javax.swing.JButton(); btnRates = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); lbid = new javax.swing.JLabel(); jTable1.setModel( new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); jScrollPane1.setViewportView(jTable1); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("ADMIN"); setBackground(new java.awt.Color(204, 255, 255)); setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); jPanel1.setBackground(new java.awt.Color(255, 255, 0)); jPanel1.setBorder(javax.swing.BorderFactory .createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED))); btnEdit.setBackground(new java.awt.Color(102, 102, 255)); btnEdit.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N btnEdit.setText("Edit"); btnEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditActionPerformed(evt); } }); btnSalary.setBackground(new java.awt.Color(102, 102, 255)); btnSalary.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N btnSalary.setText("Salary Calculation"); btnSalary.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSalaryActionPerformed(evt); } }); btnDelete.setBackground(new java.awt.Color(102, 102, 255)); btnDelete.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N btnDelete.setText("Delete"); btnDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDeleteActionPerformed(evt); } }); tblRecords.setFont(new java.awt.Font("Times New Roman", 0, 11)); // NOI18N tblRecords.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { })); tblRecords.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { tblRecordsMouseClicked(evt); } }); jScrollPane3.setViewportView(tblRecords); btnSearch.setBackground(new java.awt.Color(102, 102, 255)); btnSearch.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N btnSearch.setText("Search"); btnSearch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSearchActionPerformed(evt); } }); txtid.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N txtid.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtidActionPerformed(evt); } }); btnDisplay.setBackground(new java.awt.Color(102, 102, 255)); btnDisplay.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N btnDisplay.setText("Display"); btnDisplay.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDisplayActionPerformed(evt); } }); btnInsert.setBackground(new java.awt.Color(102, 102, 255)); btnInsert.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N btnInsert.setText("Insert"); btnInsert.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnInsertActionPerformed(evt); } }); btnRates.setBackground(new java.awt.Color(102, 102, 255)); btnRates.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N btnRates.setText("Hourly Rates"); btnRates.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnRatesActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Leelawadee", 1, 14)); // NOI18N jLabel1.setForeground(new java.awt.Color(102, 51, 0)); jLabel1.setText("ADMIN PAGE"); lbid.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 2, 14)); // NOI18N lbid.setText("WORKER ID :"); 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(jScrollPane3) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(261, 261, 261).addComponent( jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup().addGap(42, 42, 42) .addComponent(lbid) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btnRates) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(btnInsert).addGap(20, 20, 20) .addComponent(btnSearch).addGap(18, 18, 18) .addComponent(btnDisplay).addGap(18, 18, 18) .addComponent(btnEdit).addGap(18, 18, 18) .addComponent(btnDelete) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnSalary))))) .addGap(0, 32, Short.MAX_VALUE))) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel1) .addGap(34, 34, 34) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lbid) .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnSearch).addComponent(btnDisplay).addComponent(btnEdit) .addComponent(btnDelete).addComponent(btnInsert).addComponent(btnSalary)) .addGap(27, 27, 27).addComponent(btnRates).addGap(40, 40, 40).addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(127, Short.MAX_VALUE))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); pack(); }// </editor-fold>//GEN-END:initComponents private void txtidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtidActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txtidActionPerformed private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed // TODO add your handling code here: Connect1 j = new Connect1(); DBCollection dbc = j.connect("Emp_Records"); BasicDBObject bdb = new BasicDBObject(); bdb.put("ID", txtid.getText()); String[] columnNames = { "Employee ID", " First Name", "Last Name", "Date Of Birth", "Email ID", "Address", "Gender", "Mobile", "Date of joining", "Designation" }; DefaultTableModel dtm = new DefaultTableModel(columnNames, 0); DBCursor cursor = dbc.find(bdb); if (cursor.hasNext()) { DBObject obj = cursor.next(); String ID = (String) obj.get("ID"); String fname = (String) obj.get("First Name"); String lname = (String) obj.get("Last Name"); Date dob = (Date) obj.get("DOB"); String gender = (String) obj.get("Gender"); String phone = (String) obj.get("Mobile Number"); Date doj = (Date) obj.get("DOJ"); String desig = (String) obj.get("Designation"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dob1 = sdf.format(dob); String doj1 = sdf.format(doj); String email = (String) obj.get("Email"); String address = (String) obj.get("Address"); dtm.addRow(new Object[] { ID, fname, lname, dob1, email, address, gender, phone, doj1, desig }); tblRecords.setModel(dtm); } else { final JPanel p2 = new JPanel(); // final JOptionPane jo=new JOptionPane(); dtm.addRow(new Object[] { "", "", "", "", "", "", "", "" }); tblRecords.setModel(dtm); JOptionPane.showMessageDialog(p2, "Employee Not Found", "Failure", JOptionPane.ERROR_MESSAGE); txtid.setText(""); } }//GEN-LAST:event_btnSearchActionPerformed private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisplayActionPerformed Connect1 j = new Connect1(); DBCollection dbc = j.connect("Emp_Records"); BasicDBObject bdb = new BasicDBObject(); String[] columnNames = { "Employee ID", " First Name", "Last Name", "Date Of Birth", "Email", "Address", "Gender", "Mobile", "Date of joining", "Designation" }; DefaultTableModel dtm = new DefaultTableModel(columnNames, 0); DBCursor cursor = dbc.find(); while (cursor.hasNext()) { DBObject obj = cursor.next(); String ID = (String) obj.get("ID"); String fname = (String) obj.get("First Name"); String lname = (String) obj.get("Last Name"); Date dob = (Date) obj.get("DOB"); String gender = (String) obj.get("Gender"); String phone = (String) obj.get("Mobile Number"); Date doj = (Date) obj.get("DOJ"); String desig = (String) obj.get("Designation"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dob1 = sdf.format(dob); String doj1 = sdf.format(doj); String email = (String) obj.get("Email"); String address = (String) obj.get("Address"); dtm.addRow(new Object[] { ID, fname, lname, dob1, email, address, gender, phone, doj1, desig }); tblRecords.setModel(dtm); } }//GEN-LAST:event_btnDisplayActionPerformed private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed Connect1 j = new Connect1(); DBCollection dbc = j.connect("Emp_Records"); BasicDBObject bdb = new BasicDBObject(); BasicDBObject bdb1 = new BasicDBObject(); bdb.put("ID", txtid.getText()); DBCursor cursor = dbc.find(bdb); if (cursor.hasNext()) { dbc.remove(bdb); final JPanel p2 = new JPanel(); txtid.setText(""); } else { final JPanel p2 = new JPanel(); JOptionPane.showMessageDialog(p2, "Employee Not Found", "Failure", JOptionPane.ERROR_MESSAGE); txtid.setText(""); return; } DBCollection dbc1 = j.connect("Passwords"); bdb1.put("Employee Id", txtid.getText()); cursor = dbc1.find(bdb1); if (cursor.hasNext()) { dbc.remove(bdb); final JPanel p2 = new JPanel(); JOptionPane.showMessageDialog(p2, "Employee Record Deleted", "Deleted", JOptionPane.INFORMATION_MESSAGE); txtid.setText(""); } else { final JPanel p2 = new JPanel(); JOptionPane.showMessageDialog(p2, "Employee Not Found", "Failure", JOptionPane.ERROR_MESSAGE); txtid.setText(""); } }//GEN-LAST:event_btnDeleteActionPerformed private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed Connect1 j = new Connect1(); DBCollection dbc = j.connect("Emp_Records"); BasicDBObject bdb = new BasicDBObject(); bdb.put("ID", txtid.getText()); DBCursor cursor = dbc.find(bdb); if (cursor.hasNext()) { Id = txtid.getText(); new FrmEdit().setVisible(true); } else { final JPanel p2 = new JPanel(); // final JOptionPane jo=new JOptionPane(); JOptionPane.showMessageDialog(p2, "Employee Not Found", "Failure", JOptionPane.ERROR_MESSAGE); txtid.setText(""); } }//GEN-LAST:event_btnEditActionPerformed private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed // TODO add your handling code here: new FrmInsert().setVisible(true); }//GEN-LAST:event_btnInsertActionPerformed private void btnSalaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalaryActionPerformed // TODO add your handling code here: new FrmSalaryComp().setVisible(true); }//GEN-LAST:event_btnSalaryActionPerformed private void btnRatesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRatesActionPerformed // TODO add your handling code here: new FrmRate().setVisible(true); }//GEN-LAST:event_btnRatesActionPerformed private void tblRecordsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblRecordsMouseClicked // TODO add your handling code here: DefaultTableModel dtm = (DefaultTableModel) tblRecords.getModel(); int sel = tblRecords.getSelectedRow(); txtid.setText(dtm.getValueAt(sel, 0).toString()); }//GEN-LAST:event_tblRecordsMouseClicked /** * @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(FrmAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FrmAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FrmAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FrmAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new FrmAdmin().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnDelete; private javax.swing.JButton btnDisplay; private javax.swing.JButton btnEdit; private javax.swing.JButton btnInsert; private javax.swing.JButton btnRates; private javax.swing.JButton btnSalary; private javax.swing.JButton btnSearch; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTable jTable1; private javax.swing.JLabel lbid; private javax.swing.JTable tblRecords; private javax.swing.JTextField txtid; // End of variables declaration//GEN-END:variables }