22 Sept 2014

Sync with Excel using your Java Applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;

/*
<applet code="BizzApp" height=200 width=400>
</applet>
*/

public class BizzApp extends Applet implements ActionListener,ItemListener,WindowListener
{
Button b1,b2,b3,b4;
TextField tf1,tf2,tf3,tf4,tf5;
Label l1,l2,l3,l4,l5,l6,l7,l8;
Checkbox c1,c2,c3;
CheckboxGroup cbg;
String str1,str2,str3,str4,str5,str6;
Double rate,d1,fsalary;
Frame frame;

    public void init()
    {
    //setLayout(new FlowLayout(FlowLayout.LEFT));
    Color c=new Color(32,203,212);
    Color col1=new Color(239,179,203);
    setBackground(c);
    frame=new Frame();
    frame.setSize(270,100);
    frame.setBackground(col1);
    l1=new Label("ID:");
    l2=new Label("Name:");
    l3=new Label("Designation:");
    l4=new Label("Salary:");
    l5=new Label("Press Calculate button to compute salary with bonus.");
    l6=new Label("Calculated Salary(auto):");
    l7=new Label("Bonus Rate(auto):");
    l8=new Label("Bizz App v1.0");
    Font f1=new Font("Courier",Font.BOLD+Font.ITALIC,32);
    l8.setFont(f1);
    Font f=new Font("Times New Roman",Font.BOLD,14);
    l5.setFont(f);
    tf1=new TextField(5);
    tf2=new TextField(25);
    tf3=new TextField(10);
    tf4=new TextField(10);
    tf5=new TextField(2);
    b1=new Button("Calculate");
    b2=new Button("Save");
    b3=new Button("Reset");
    b4=new Button("About");
   
    cbg=new CheckboxGroup();
    c1=new Checkbox("Project Manager",cbg,false);
    c2=new Checkbox("Team Member",cbg,false);
    c3=new Checkbox("Work Man",cbg,false);
    add(l1);
    add(tf1);
    add(l2);
    add(tf2);
    add(l3);
    add(c1);add(c2);add(c3);
    add(l4);
    add(tf3);
    add(l7);
    add(tf5);
    add(l6);
    add(tf4);
    add(l5);
    add(b1);
    add(b2);
    add(b3);
    add(b4);
    frame.add(l8);
   
        try
        {
        File file=new File("BizzApp.xls");
            if(!file.exists())
            {
            file.createNewFile();
           
            FileWriter fw=new FileWriter("BizzApp.xls",true);
            String header="ID\tName\tDesignation\tSalary\tBonus Rate(%)\tFinal Salary\n";
            fw.write(header);
            fw.close();
            }
        }
        catch(Exception e)
        {
        System.out.println(e);
        }
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    c1.addItemListener(this);
    c2.addItemListener(this);
    c3.addItemListener(this);
    /*frame.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent we)
        {
        frame.dispose();
        }
    });*/
    frame.addWindowListener(this);
    }
   
    public void windowClosing(WindowEvent we)
    {
    frame.dispose();
    }
    public void windowOpened(WindowEvent we){}
    public void windowClosed(WindowEvent we){}
    public void windowIconified(WindowEvent we){}
    public void windowDeiconified(WindowEvent we){}
    public void windowActivated(WindowEvent we){}
    public void windowDeactivated(WindowEvent we){}
   
    public void itemStateChanged(ItemEvent ie)
    {
    }
   
    public void transfer()
    {
    str1=tf1.getText();
    str2=tf2.getText();
    str3=cbg.getSelectedCheckbox().getLabel();
    str4=tf3.getText();
    d1=Double.parseDouble(str4);
    }
   
    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getSource()==b1)
    {
    this.transfer();
        if(str3=="Project Manager")
        {
        rate=0.15;
        }
        else if(str3=="Team Member")
        {
        rate=0.1;
        }
        else if(str3=="Work Man")
        {
        rate=0.05;
        }
    fsalary=d1+(d1*rate);
    str6=String.valueOf(rate*100);
    tf5.setText(str6);
    str5=String.valueOf(fsalary);
    tf4.setText(str5);
    }
        else if(ae.getSource()==b2)
        {
        this.transfer();
            try
            {
            FileWriter fw1=new FileWriter("BizzApp.xls",true);
            String row=str1+"\t"+str2+"\t"+str3+"\t"+d1+"\t"+str6+"\t"+fsalary+"\n";
            fw1.write(row);
            fw1.close();
            }
            catch(Exception e)
            {
            System.out.println(e);
            }
        }
   
    else if(ae.getSource()==b3)
    {
    tf1.setText("");
    tf2.setText("");
    tf3.setText("");
    tf4.setText("");
    tf5.setText("");
    }
        else if(ae.getSource()==b4)
        {
        frame.setVisible(true);
        }
    }
}
***********************************************************************************

Snapshots: