Skip to content

Bonus (Employee Payroll Management)

March 14, 2013
The given source code below is used in employee payroll to manage the bonus of employee and provide the bonus to employee.

package payroll;

import Validations.Validation;
import database.ConnectionSetting;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class BonusEmployee extends JInternalFrame implements ActionListener
{
    /*Declaring Object Name of all Components*/
    JLabel bonustypelbl, imglbl, headinglbl, oplbl, empIDlbl, namelbl, desiglbl, departlbl, dojlbl, remarklbl, salaryAmtlbl;
    JTextField empIDTF, nameTF, desigTF, departTF, dojTF, bonusAmtTF;
    JTextArea remarkTA;
    JComboBox bonustype;
    JButton saveBtn, CancelBtn, searchBtn;
    JPanel head, MainPanel, imagePanel, salaryPanel;
    String empid=null;
        JScrollPane scrollPane;
    String[] type = {“Select Any”, “Diwali”, “Annual”};
    JLabel imagelbl=null;
    InputStream stream=null;
    BufferedImage bimg = null;
        Connection con = null;
    public BonusEmployee()
    {
                /*Creating Connection between Back End and Front End*/
        try
        {
            ConnectionSetting conn = new ConnectionSetting();
            con = conn.getConnection();
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this,e,”DataSource Error”,1);
        }
             /*Defining Properties of Frame*/
        this.setTitle(“Bonus”);
        this.setResizable(true);
        this.setFrameIcon(new ImageIcon(“Images/s-prices.jpg”));
        this.setLocation(0,25);
        this.setSize(1010,685);
        this.setClosable(true);
        this.setVisible(true);
        this.setLayout(null);

            /*Initializing all Panels*/
        head = new JPanel();
        MainPanel = new JPanel();
        MainPanel.add(imagePanel = new JPanel());
        MainPanel.add(salaryPanel = new JPanel());
            /*Intitializing all Buttons*/
        saveBtn = new JButton(“Save”);
        CancelBtn = new JButton(“Cancel”);
        searchBtn = new JButton(“Search”);
            /*Initializing all Labels adding on the MainPanel*/       
        headinglbl = new JLabel(“Pay System”);
        oplbl = new JLabel(“Bonus”);           
        MainPanel.add(empIDlbl = new JLabel(“Empid”));
        MainPanel.add(namelbl = new JLabel(“Name”));
        MainPanel.add(desiglbl = new JLabel(“Designation”));
        MainPanel.add(departlbl = new JLabel(“Department”));
        MainPanel.add(dojlbl = new JLabel(“Date Of Joining”));
        MainPanel.add(remarklbl = new JLabel(“Remarks”));

            /*Initializing all TextFields, TextArea and adding on the MainPanel*/
        MainPanel.add(empIDTF = new JTextField());
        MainPanel.add(nameTF = new JTextField());
        MainPanel.add(desigTF = new JTextField());
        MainPanel.add(departTF = new JTextField());
        MainPanel.add(dojTF = new JTextField());
                remarkTA = new JTextArea();
        MainPanel.add(scrollPane = new JScrollPane(remarkTA));
            /*Initializing all Salary Related Label, TextField,
            ComboBox and Adding on Salary Panel*/
        salaryPanel.add(bonustypelbl = new JLabel(“Bonus Type”));
        salaryPanel.add(salaryAmtlbl = new JLabel(“Bonus Amount”));
        salaryPanel.add(bonustype = new JComboBox(type));
        salaryPanel.add(bonusAmtTF = new JTextField());
            /*setting size and location of panels*/       
        head.setBounds(5,5,990,100);
        MainPanel.setBounds(5,110,990,500);
        imagePanel.setBounds(600,20,250,200);
        salaryPanel.setBounds(40,220,410,200);
            /*Setting Border of Salary Panel and Specifying Heading of Name of Panel*/
        salaryPanel.setBorder(BorderFactory.createTitledBorder(“Bonus Info.”));
            /*Setting size and location of all Labels*/
        headinglbl.setBounds(350,20,300,40);
        oplbl.setBounds(440,60,300,40);
        empIDlbl.setBounds(50,20,100,20);
        namelbl.setBounds(50,60,100,20);
        desiglbl.setBounds(50,100,100,20);
        departlbl.setBounds(50,140,100,20);
        dojlbl.setBounds(50,180,100,20);
        remarklbl.setBounds(500,280,100,20);
        bonustypelbl.setBounds(50,50,100,20);
        bonustype.setBounds(200,50,100,25);
        salaryAmtlbl.setBounds(50,90,100,20);
        bonusAmtTF.setBounds(200,90,100,25);
            /*setting Size and location of all TextFields*/
        empIDTF.setBounds(200,20,100,25);
        nameTF.setBounds(200,60,200,25);
        desigTF.setBounds(200,100,200,25);
        departTF.setBounds(200,140,200,25);
        dojTF.setBounds(200,180,100,25);
        scrollPane.setBounds(600,280,200,100);
            /*Setting un-editable all Fields on Form load*/
        empIDTF.setEditable(false);
        empIDTF.setEditable(false);
        nameTF.setEditable(false);
        desigTF.setEditable(false);
        departTF.setEditable(false);
        dojTF.setEditable(false);
        remarkTA.setEditable(false);
        bonusAmtTF.setEditable(false);
        bonustype.setEnabled(false);
        saveBtn.setEnabled(false);
            /*Setting size and location of all Buttons*/
        saveBtn.setBounds(350,615,80,25);
        CancelBtn.setBounds(600,615,80,25);
        searchBtn.setBounds(475,615,80,25);
            /*Setting Background Color of All Panel*/
        head.setBackground(Color.LIGHT_GRAY);
        MainPanel.setBackground(Color.CYAN);
        imagePanel.setBackground(Color.LIGHT_GRAY);
        salaryPanel.setBackground(Color.CYAN);
            /*Adding Heading Label on Head Panel*/
        head.add(headinglbl);
        head.add(oplbl);
            /*Setting null layout of all panels*/
        head.setLayout(null);
        MainPanel.setLayout(null);
        imagePanel.setLayout(null);
        salaryPanel.setLayout(null);
            /*Adding All panels on Frame*/
        this.add(head);
        this.add(MainPanel);       
        this.add(saveBtn);
        this.add(searchBtn);
        this.add(CancelBtn);
            /*Adding Image on Label, Label on ImagePanel and Specifying
            path of image and setting size of location of Image here..*/           
        try
        {
                    imagelbl = new JLabel(new ImageIcon(ImageIO.read(new File(“Images/user.png”))));
            imagelbl.setBounds(5,5,240,190);
            imagePanel.add(imagelbl);
            }
        catch (IOException e)
        {
                    JOptionPane.showMessageDialog(null,e,”Error Message”,1);
            }
            /*Specifying the Font and height of the heading Labels…*/
        headinglbl.setFont(new Font(“Algerian”,Font.BOLD,40));
        oplbl.setFont(new Font(“Castellar”,Font.PLAIN,20));
            /*Specifying or Performing actions on Buttons Spefified    */
        CancelBtn.addActionListener(this);
        saveBtn.addActionListener(this);       
        searchBtn.addActionListener(this);
    }
    /*Specifying or Performing actions on Buttons Spefified    */
    public void actionPerformed(ActionEvent ae)
    {
        /*Getting Source which Button is Pressed or Clicked*/
        Object ob = ae.getSource();
            /*Performing Close Operation on JInternalFrame when
            Cancel Button is Clicked or Pressed and Released*/       
        if(ob == CancelBtn)
        {
            this.dispose();
        }
        /*Performing or specifying action of Save Operation to save or update
        data into the database when Save button is clicked or Pressed and Released*/
        if(ob == saveBtn)
        {
            /*here a valid object is created to get the methods defined in
            Validation class to validate the Date, String and Number Fields*/
            Validation valid = new Validation();
            /*Validating all fields here*/
            if(bonustype.getSelectedItem().equals(“Select Any”))
                JOptionPane.showMessageDialog(this,”Please Select Salary Type!”,”Empty Field”,1);
            else if(bonusAmtTF.getText().length()<=0)
                JOptionPane.showMessageDialog(this,”Bonus Field Required!”,”Empty Field”,1);
            else if(valid.isValidNumber(bonusAmtTF.getText()))
                JOptionPane.showMessageDialog(this,”Bonus Field Must Be in Numeric Format Only!\nSpecial Characters (!,\\/. Etc) Are Not Allowed.”,”Empty Field”,1);
            /*Validation ended here*/
            else
            {
                String id = empIDTF.getText().substring(3,6);
                int emp = Integer.parseInt(id);
                try
                {
                    /*Setting Connection AutoComment False */
                                        PreparedStatement ps = con.prepareStatement(“update Bonus_Table set Bonus_Type=?,Bonus_Amt=?,Remarks=? where EmpID=?”);
                    ps.setString(1,bonustype.getSelectedItem().toString());
                    ps.setInt(2,Integer.parseInt(bonusAmtTF.getText()));
                    ps.setString(3,remarkTA.getText());
                    ps.setString(4,empIDTF.getText());
                    ps.executeUpdate();
                    int ch = JOptionPane.showConfirmDialog(this,”Record successfully Updated!!! Do you want to Continue?”,”Operation Successfull”,JOptionPane.YES_NO_OPTION);
                    /*If user wants to insert other record or want to continue then all fields will be cleared*/
                    if(ch==0)
                    {
                        empIDTF.setEditable(false);
                        empIDTF.setEditable(false);
                        nameTF.setEditable(false);
                        desigTF.setEditable(false);
                        departTF.setEditable(false);
                        dojTF.setEditable(false);
                        remarkTA.setEditable(false);
                        bonusAmtTF.setEditable(false);
                        bonustype.setEnabled(false);
                        saveBtn.setEnabled(false);

                        empIDTF.setText(“”);
                        nameTF.setText(“”);
                        bonustype.setSelectedItem(“Select Any”);
                        desigTF.setText(“”);
                        departTF.setText(“”);
                        dojTF.setText(“”);
                        remarkTA.setText(“”);
                        bonusAmtTF.setText(“”);
                        imagelbl.setIcon(new ImageIcon(“Images/user.png”));
                    }/*if user says no then frame will be closed*/                   
                    else
                    {
                        this.dispose();
                    }
                }
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog(this,e,”Data Retrieve Error”,1);
                }
            }
        }//Action Performed on save buton ended here.
        /*Performing Search Operation from Database when an employee id is sepcified
        and setting all imformation to there relative fields*/
        if(ob == searchBtn)
        {
            try
            {
                Boolean rsult=false;
                String getID = JOptionPane.showInputDialog(“Enter EmpID”);
                if(getID != null)
                {
                                        PreparedStatement ps = con.prepareStatement(“select * from Emp_Table where EmpId=?”);
                    PreparedStatement ps1 = con .prepareStatement(“select * from Bonus_Table where EmpId=?”);
                    ps.setString(1, getID);
                    ResultSet rs = ps.executeQuery();
                    while(rs.next())
                    {   
                        stream=rs.getBinaryStream(“Image”);
                        bimg =ImageIO.read(stream);
                        imagelbl.setIcon(new ImageIcon(bimg));
                        empIDTF.setText(rs.getString(1));
                        nameTF.setText(rs.getString(“Emp_Name”));
                        dojTF.setText(rs.getDate(“Doj”).toString());
                        departTF.setText(rs.getString(“Depart”));
                        desigTF.setText(rs.getString(“Design”));
                        bonustype.setSelectedItem(“Select Any”);
                        bonusAmtTF.setText(“”);
                        remarkTA.setText(“”);
                        saveBtn.setEnabled(true);
                        bonustype.setEnabled(true);
                        remarkTA.setEditable(true);
                        bonusAmtTF.setEditable(true);
                        rsult=true;
                    }
                    ps1.setString(1, getID);
                    ResultSet rs1 = ps1.executeQuery();

                    while(rs1.next())
                    {   
                        bonustype.setSelectedItem(rs1.getString(“Bonus_Type”));
                        bonusAmtTF.setText(String.valueOf(rs1.getInt(“Bonus_Amt”)));
                        remarkTA.setText(rs1.getString(“Remarks”));
                    }
                    if(rsult==false)
                        JOptionPane.showMessageDialog(null,”Sorry Record Not Found”,”Data Retrieve Error”,1);
                }
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null,e,”Data Retrieve Error”,1);
            }
        }//Search Operation ends here.
    }//addActionListener method ended here.
}

Leave a Comment

Leave a comment