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 employee.payroll.system; import com.itextpdf.text.Document; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.awt.Dimension; import java.awt.Font; import java.awt.Toolkit; import java.io.File; import java.io.FileOutputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Date; import javax.swing.JFileChooser; import javax.swing.JOptionPane; /** * * @author Usman */ public class searchEmpSalarySlip extends javax.swing.JFrame { Connection conn = null; ResultSet rs = null; PreparedStatement pst = null; /** * Creates new form searchEmpSalarySlip */ public searchEmpSalarySlip() { initComponents(); conn = db.java_db(); Toolkit toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation(size.width / 2 - getWidth() / 2, size.height / 2 - getHeight() / 2); } /** * 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() { jLabel3 = new javax.swing.JLabel(); jPanel4 = new javax.swing.JPanel(); jLabel11 = new javax.swing.JLabel(); txt_search = new javax.swing.JTextField(); txt_dep = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); txt_id = new javax.swing.JTextField(); txt_firstname = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); txt_surname = new javax.swing.JTextField(); txt_dob = new javax.swing.JTextField(); jLabel8 = new javax.swing.JLabel(); txt_desig = new javax.swing.JTextField(); txt_status = new javax.swing.JTextField(); txt_doj = new javax.swing.JTextField(); txt_job = new javax.swing.JTextField(); txt_salary = new javax.swing.JTextField(); jLabel13 = new javax.swing.JLabel(); jLabel20 = new javax.swing.JLabel(); jLabel19 = new javax.swing.JLabel(); jLabel18 = new javax.swing.JLabel(); jLabel17 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jLabel3.setText("Date of Birth :"); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Search", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Adobe Arabic", 1, 14))); // NOI18N jLabel11.setText("Employee ID :"); txt_search.addContainerListener(new java.awt.event.ContainerAdapter() { public void componentRemoved(java.awt.event.ContainerEvent evt) { txt_searchComponentRemoved(evt); } }); txt_search.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txt_searchActionPerformed(evt); } }); txt_search.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { txt_searchKeyReleased(evt); } }); 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().addComponent(jLabel11) .addGap(18, 18, 18).addComponent(txt_search).addContainerGap())); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txt_search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel11)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); txt_dep.setEditable(false); jLabel6.setText("Employee id :"); txt_id.setEditable(false); txt_firstname.setEditable(false); txt_firstname.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txt_firstnameActionPerformed(evt); } }); jLabel4.setText("First name :"); jLabel7.setText("Surname :"); txt_surname.setEditable(false); txt_dob.setEditable(false); jLabel8.setText("Date of Birth :"); txt_desig.setEditable(false); txt_status.setEditable(false); txt_doj.setEditable(false); txt_job.setEditable(false); txt_job.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txt_jobActionPerformed(evt); } }); txt_salary.setEditable(false); txt_salary.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txt_salaryActionPerformed(evt); } }); jLabel13.setText("Basic Salary :"); jLabel20.setText("Job Title :"); jLabel19.setText("Date Hired :"); jLabel18.setText("Status :"); jLabel17.setText("Designation :"); jLabel9.setText("Department :"); jButton1.setIcon( new javax.swing.ImageIcon(getClass().getResource("/employee/payroll/system/Images/slip.png"))); // NOI18N jButton1.setText("Generate Slip"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel8).addComponent(jLabel7).addComponent(jLabel4) .addComponent(jLabel6).addComponent(jLabel9)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(txt_surname, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txt_firstname) .addComponent(txt_dob, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txt_id, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE) .addComponent(txt_dep)) .addGap(25, 25, 25) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel18) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel19).addComponent(jLabel13) .addComponent(jLabel17).addComponent(jLabel20)).addGap(14, 14, 14) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txt_doj).addComponent(txt_salary) .addComponent(txt_status).addComponent(txt_desig) .addComponent(txt_job, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addGroup(layout.createSequentialGroup().addComponent(jButton1).addGap(191, 191, 191))) .addContainerGap(18, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jPanel4, 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.TRAILING) .addComponent(jLabel6).addComponent( txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(7, 7, 7) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(txt_firstname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txt_desig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel17)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel18).addComponent(txt_status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(6, 6, 6) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel19) .addComponent( txt_doj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent( txt_surname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7)) .addGap(13, 13, 13) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel20) .addComponent(txt_job, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent( txt_dob, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel13) .addComponent(txt_salary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9).addComponent(txt_dep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(18, 18, 18) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void txt_searchComponentRemoved(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_txt_searchComponentRemoved // TODO add your handling code here: }//GEN-LAST:event_txt_searchComponentRemoved private void txt_searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_searchActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txt_searchActionPerformed private void txt_searchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_searchKeyReleased // TODO add your handling code here: try { String sql = "select * from Staff_information where id=? "; pst = conn.prepareStatement(sql); pst.setString(1, txt_search.getText()); rs = pst.executeQuery(); String add1 = rs.getString("id"); txt_id.setText(add1); String add2 = rs.getString("first_name"); txt_firstname.setText(add2); String add3 = rs.getString("surname"); txt_surname.setText(add3); String add4 = rs.getString("Dob"); txt_dob.setText(add4); String add5 = rs.getString("Department"); txt_dep.setText(add5); String add7 = rs.getString("Salary"); txt_salary.setText(add7); String add8 = rs.getString("Status"); txt_status.setText(add8); String add9 = rs.getString("Date_hired"); txt_doj.setText(add9); String add10 = rs.getString("job_title"); txt_job.setText(add10); String add17 = rs.getString("Designation"); txt_desig.setText(add17); } catch (Exception e) { JOptionPane.showMessageDialog(null, "No Data"); } finally { try { rs.close(); pst.close(); } catch (Exception e) { } } }//GEN-LAST:event_txt_searchKeyReleased private void txt_firstnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_firstnameActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txt_firstnameActionPerformed private void txt_jobActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_jobActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txt_jobActionPerformed private void txt_salaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_salaryActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txt_salaryActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: String value = txt_firstname.getText(); String value0 = txt_surname.getText(); String value1 = txt_id.getText(); String value2 = txt_desig.getText(); String value3 = txt_desig.getText(); JFileChooser dialog = new JFileChooser(); dialog.setSelectedFile(new File(value + " " + value0 + "-Salary Slip" + ".pdf")); int dialogResult = dialog.showSaveDialog(null); if (dialogResult == JFileChooser.APPROVE_OPTION) { String filePath = dialog.getSelectedFile().getPath(); try { // TODO add your handling code here: String sql = "select * from Deductions where emp_id = '" + value1 + "'"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); String val = rs.getString(5); String reason = rs.getString(6); rs.close(); pst.close(); String sq = "select * from Allowance where emp_id = '" + value1 + "'"; pst = conn.prepareStatement(sq); rs = pst.executeQuery(); int calcTotal = Integer.parseInt(txt_salary.getText()); float x = Float.valueOf(rs.getString(9)); int v = Integer.parseInt(val); float total = calcTotal + x - v; Document myDocument = new Document(); PdfWriter myWriter = PdfWriter.getInstance(myDocument, new FileOutputStream(filePath)); myDocument.open(); myDocument .add(new Paragraph("PAY SLIP", FontFactory.getFont(FontFactory.TIMES_BOLD, 20, Font.BOLD))); myDocument.add(new Paragraph(new Date().toString())); myDocument.add(new Paragraph( "-------------------------------------------------------------------------------------------")); myDocument.add((new Paragraph("EMPLOYEE DETAILS", FontFactory.getFont(FontFactory.TIMES_ROMAN, 15, Font.BOLD)))); myDocument.add((new Paragraph("Name of Employee: " + value + " " + value0, FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)))); myDocument.add((new Paragraph("Designation: " + value2, FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)))); myDocument.add((new Paragraph("Department: " + value3, FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN)))); myDocument.add(new Paragraph( "-------------------------------------------------------------------------------------------")); myDocument .add(new Paragraph("SALARY", FontFactory.getFont(FontFactory.TIMES_ROMAN, 15, Font.BOLD))); myDocument.add(new Paragraph("Basic Salary: $" + calcTotal, FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))); myDocument.add(new Paragraph("Overtime: " + rs.getString(2) + " Hours", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))); myDocument.add(new Paragraph("Medical: $" + rs.getString(3), FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))); myDocument.add(new Paragraph("Bonus: $" + rs.getString(4), FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))); myDocument.add(new Paragraph("Other: $" + rs.getString(5), FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))); myDocument.add(new Paragraph( "-------------------------------------------------------------------------------------------")); myDocument.add( new Paragraph("DEDUCTION", FontFactory.getFont(FontFactory.TIMES_ROMAN, 15, Font.BOLD))); myDocument.add(new Paragraph("Deduction Details: " + reason, FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))); myDocument.add(new Paragraph("Total Deductions : $" + val, FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))); myDocument.add(new Paragraph( "-------------------------------------------------------------------------------------------")); myDocument.add(new Paragraph("TOTAL PAYMENT", FontFactory.getFont(FontFactory.TIMES_ROMAN, 15, Font.BOLD))); myDocument.add(new Paragraph("Total Earnings: " + rs.getString(9), FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))); myDocument.add(new Paragraph("Net Pay : " + total, FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.PLAIN))); myDocument.add(new Paragraph( "-------------------------------------------------------------------------------------------")); myDocument.newPage(); myDocument.close(); JOptionPane.showMessageDialog(null, "Report was successfully generated"); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } finally { try { rs.close(); pst.close(); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } } }//GEN-LAST:event_jButton1ActionPerformed /** * @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(searchEmpSalarySlip.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(searchEmpSalarySlip.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(searchEmpSalarySlip.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(searchEmpSalarySlip.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 searchEmpSalarySlip().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel17; private javax.swing.JLabel jLabel18; private javax.swing.JLabel jLabel19; private javax.swing.JLabel jLabel20; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel4; private javax.swing.JTextField txt_dep; private javax.swing.JTextField txt_desig; private javax.swing.JTextField txt_dob; private javax.swing.JTextField txt_doj; private javax.swing.JTextField txt_firstname; private javax.swing.JTextField txt_id; private javax.swing.JTextField txt_job; private javax.swing.JTextField txt_salary; private javax.swing.JTextField txt_search; private javax.swing.JTextField txt_status; private javax.swing.JTextField txt_surname; // End of variables declaration//GEN-END:variables }