sms.ReportForms.java Source code

Java tutorial

Introduction

Here is the source code for sms.ReportForms.java

Source

/*
 * 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 sms;

import com.itextpdf.kernel.geom.PageSize;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import static sms.ViewResults.yearid;

/**
 *
 * @author kimani kogi
 */
public class ReportForms extends javax.swing.JFrame {
    static int yearid = 2;
    int examid = 1;
    int termid = 1;
    String stream = "";
    String schooldetails = "ITHANGA SECONDARY SCHOOL \n P.O BOX 47 THIKA-0100\nTel:0714406984\nSTUDENT PROGRESS REPORT";

    /**
     * Creates new form ReportForms
     */
    public ReportForms() {
        initComponents();
        setExamsModel();
    }

    public String unig() {
        //int year= jYearChooser1.getYear();
        Calendar cal = new GregorianCalendar();
        String sidi = this.sid.getText();
        String yr = String.valueOf(yearid);
        String termi = String.valueOf(termid);
        String exami = String.valueOf(examid);
        // int year=cal.get(Calendar.YEAR);
        int year = jYearChooser1.getYear();
        String uniq = yr + termi + exami + sidi + String.valueOf(year);
        return uniq;
    }

    String[] Exams;

    private void setExamsModel() {
        methods m = new methods();
        jComboBoxExam.removeAllItems();
        jComboBoxExam.addItem("Choose Exam");

        ArrayList<String> list = m.ExamsTypes();
        Exams = new String[list.size()];
        list.toArray(Exams);
        for (int a = 0; a < Exams.length; a++) {

            jComboBoxExam.addItem(Exams[a]);
        }

    }

    public ArrayList<ExamDbDataHolder> selectExamResults(String uniq, String query) {
        ArrayList<ExamDbDataHolder> usersList = new ArrayList();
        try {

            // String searchQuery = "SELECT * FROM `exam` WHERE `uniq` ='" + uniq + "'";
            methods m = new methods();
            Connection con = m.getConnection();
            Statement st = con.createStatement();

            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                ExamDbDataHolder user = new ExamDbDataHolder(rs.getString("sid"), rs.getString("uniq"),
                        rs.getString("s1"), rs.getString("s2"), rs.getString("s3"), rs.getString("s4"),
                        rs.getString("s5"), rs.getString("s6"), rs.getString("s7"), rs.getString("s8"),
                        rs.getString("s9"), rs.getString("s10"), rs.getString("s11"), rs.getString("s12"),
                        rs.getString("s13"), rs.getString("s14"), rs.getString("s15"), rs.getString("s16"),
                        rs.getString("s17"), rs.getString("s18"), rs.getString("s19"), rs.getString("s20"),
                        rs.getString("s21"), rs.getString("s22"), rs.getString("s23"), rs.getInt("total"));

                usersList.add(user);

            }
            st.close();
            rs.close();

            con.close();

        } catch (SQLException ex) {
            Logger.getLogger(ReportForms.class.getName()).log(Level.SEVERE, null, ex);
        }

        return usersList;

    }

    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxExam = new javax.swing.JComboBox<>();
        jLabel3 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox<>();
        jLabel4 = new javax.swing.JLabel();
        sid = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox<>();
        jYearChooser1 = new com.toedter.calendar.JYearChooser();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox4 = new javax.swing.JComboBox<>();
        jLabel6 = new javax.swing.JLabel();
        jComboBox5 = new javax.swing.JComboBox<>();
        jLabel7 = new javax.swing.JLabel();
        jComboBox6 = new javax.swing.JComboBox<>();
        jLabel8 = new javax.swing.JLabel();
        jComboBox7 = new javax.swing.JComboBox<>();
        jButton2 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("PER STUDENT"));

        jLabel2.setText("EXAM");

        jLabel3.setText("TERM");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(
                new String[] { "Choose", "Term 1", "ITerm 2", "ITerm 3", "ITerm 4" }));

        jLabel4.setText("ADM NO");

        jButton1.setText("PRINT");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel5.setText("FORM");

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel<>(
                new String[] { "Choose Form", "Form 1", "Form 2", "Form 3", "Form 4" }));

        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)
                        .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel5)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jComboBox3, javax.swing.GroupLayout.Alignment.LEADING, 0,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jComboBoxExam, javax.swing.GroupLayout.Alignment.LEADING, 0,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jComboBox2, javax.swing.GroupLayout.Alignment.LEADING, 0,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94,
                                        Short.MAX_VALUE)
                                .addGroup(jPanel2Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel4).addComponent(sid).addComponent(jButton1,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))
                                .addGap(51, 51, 51))))
                .addGroup(jPanel2Layout.createSequentialGroup().addGap(117, 117, 117)
                        .addComponent(jYearChooser1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
                        .addComponent(jYearChooser1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jComboBoxExam, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(sid, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18).addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton1))
                        .addContainerGap(25, Short.MAX_VALUE)));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("PER CLASS"));

        jLabel1.setText("EXAM");

        jLabel6.setText("TERM");

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel<>(
                new String[] { "Choose Term", "Term 1", "Term 2", "Term 3" }));

        jLabel7.setText("FORM");

        jComboBox6.setModel(new javax.swing.DefaultComboBoxModel<>(
                new String[] { "Choose Form", "Form 1", "Form 2", "Form 3", "Form 4" }));

        jLabel8.setText("CLASS");

        jButton2.setText("PRINT");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1).addComponent(jLabel6).addComponent(jLabel7)
                                        .addComponent(jComboBox6, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE)
                                        .addComponent(jComboBox4, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE))
                                .addGap(107, 107, 107)
                                .addGroup(jPanel3Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 60,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jComboBox7, javax.swing.GroupLayout.PREFERRED_SIZE, 76,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jButton2))
                                .addGap(34, 34, 34))
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addGap(23, 23, 23)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1).addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jComboBox7, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel6)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18).addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jComboBox6, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jButton2))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("PER FORM"));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(jPanel4Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 241, Short.MAX_VALUE));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jPanel3,
                                javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));

        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(14, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(14, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap(17, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String u = unig();
        //checkPreviousResults(u);  
        getStudentsDetails(sid.getText(), "SELECT * FROM `exam` WHERE `uniq` ='" + u + "'");

    }//GEN-LAST:event_jButton1ActionPerformed

    public void getStudentsDetails(String id, String Query) {
        String name = null;
        String formclass = null;
        String dob = null;
        String house = null;
        String kcpe = null;
        String kcpegrade = null;
        String img = null;

        try {
            String query = "SELECT * FROM `students` WHERE `id` ='" + id + "'";
            methods m = new methods();
            Connection con = m.getConnection();
            Statement st = con.createStatement();

            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                name = rs.getString("fname") + " " + rs.getString("lastname");
                formclass = rs.getString("form") + "" + rs.getString("class");
                dob = rs.getString("dob");
                house = rs.getString("house");
                kcpe = rs.getString("kcpe");
                img = rs.getString("imgurl");
                kcpegrade = rs.getString("kcpegrade");

            }

            st.close();
            rs.close();

            con.close();

        } catch (SQLException ex) {
            Logger.getLogger(ReportForms.class.getName()).log(Level.SEVERE, null, ex);
        }
        utext(Query, name, dob, house, formclass, kcpe, img, kcpegrade, id);
    }

    public void utext(String query, String Name, String dob, String house, String formclass, String kcpe,
            String imgurl, String kcpegrade, String id) {
        methods nn = new methods();
        String u = unig();
        checkPreviousResults(u, kcpe, kcpegrade, id);
        ArrayList<ExamDbDataHolder> users = selectExamResults(u, query);
        if (users.size() < 2) {
            Form1Exams n = new Form1Exams();
            String[] Subjects = n.findSubjectid();
            String[] Subjectsnames = n.findSubjectname();

            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new java.io.File(","));
            chooser.setDialogTitle("Save at");
            chooser.setApproveButtonText("save");
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                try {

                    Document pdfp = new Document();
                    PdfWriter w = PdfWriter.getInstance(pdfp,
                            new FileOutputStream(new File(chooser.getSelectedFile(), "" + sid.getText() + ".pdf")));
                    pdfp.open();
                    PdfContentByte canvas = w.getDirectContentUnder();
                    Image imgb = Image.getInstance("C:\\Users\\kimani kogi\\Pictures\\icons\\logo.png");
                    imgb.setAbsolutePosition(2, 2);
                    imgb.scaleAbsoluteHeight(PageSize.A4.getHeight() / 4);
                    imgb.scaleAbsoluteWidth(PageSize.A4.getWidth() / 4);
                    canvas.saveState();
                    PdfGState state = new PdfGState();
                    state.setFillOpacity(0.6f);
                    canvas.setGState(state);
                    canvas.addImage(imgb);
                    canvas.restoreState();

                    PdfPTable tbl1 = new PdfPTable(2);
                    //  tbl.setWidthPercentage(100);

                    tbl1.setTotalWidth(575);
                    tbl1.setLockedWidth(true);

                    tbl1.setWidths(new int[] { 1, 4 });
                    if (imgurl.equals("image")) {
                        tbl1.addCell("no image ");

                        String img = imgurl;
                    } else {
                        tbl1.addCell(createImageCell(imgurl));

                    }

                    tbl1.addCell(createTextCell(schooldetails));

                    PdfPTable tbl = new PdfPTable(8);
                    //  tbl.setWidthPercentage(100);
                    tbl.setTotalWidth(575);
                    tbl.setLockedWidth(true);
                    tbl.setSpacingBefore(8);
                    tbl.setSpacingAfter(6);
                    tbl.getDefaultCell().setBorderWidthTop(2);
                    tbl.getDefaultCell().setBorderWidthLeft(0);
                    tbl.getDefaultCell().setBorderWidthRight(0);
                    tbl.setWidths(new int[] { 1, 1, 1, 2, 1, 1, 1, 1 });
                    tbl.addCell("Adm No:");
                    tbl.addCell(sid.getText());
                    tbl.addCell("Name:");
                    tbl.addCell(Name);
                    tbl.addCell("Form:");
                    tbl.addCell(formclass);
                    tbl.addCell("Kcpe:");
                    tbl.addCell(kcpe);

                    tbl.addCell("House:");
                    tbl.addCell(house);
                    tbl.addCell("Term:");
                    tbl.addCell("Second term");
                    tbl.addCell("Year:");
                    tbl.addCell("2017");
                    tbl.addCell("DOB:");
                    tbl.addCell(dob);

                    PdfPTable tbl2 = new PdfPTable(5);
                    //  tbl.setWidthPercentage(100);
                    tbl2.setTotalWidth(575);
                    tbl2.setLockedWidth(true);
                    //  tbl2.getDefaultCell().setFixedHeight(35f);
                    tbl2.setWidths(new int[] { 2, 1, 1, 2, 2 });

                    tbl2.addCell(creatTextCellHeader("Subjects"));
                    tbl2.addCell(creatTextCellHeader("Exams"));
                    tbl2.addCell(creatTextCellHeader("Grade"));
                    tbl2.addCell(creatTextCellHeader("Ratings"));
                    tbl2.addCell(creatTextCellHeader("Remarks"));
                    // String []  Subjectsnames=n.findSubjectname();
                    for (int i = 0; i < users.size(); i++) {
                        for (int a = 0; a < Subjectsnames.length; a++) {
                            tbl2.addCell(Subjectsnames[a]);
                            String re = null;
                            if (Subjects[a].equals("s1")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getMathematics());
                                re = ((ExamDbDataHolder) users.get(i)).getMathematics();
                                //  JOptionPane.showMessageDialog(null,((ExamDbDataHolder)users.get(i)).getMathematics());
                                // String maths = ((ExamDbDataHolder)users.get(i)).getMathematics();

                            } else if (Subjects[a].equals("s2")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getEnglish());
                                re = ((ExamDbDataHolder) users.get(i)).getEnglish();
                                //row[c] = ((ExamDbDataHolder)users.get(i)).getEnglish();
                                // c++;
                            } else if (Subjects[a].equals("s3")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getKiswahili());
                                re = ((ExamDbDataHolder) users.get(i)).getKiswahili();
                                // row[c] = ((ExamDbDataHolder)users.get(i)).getKiswahili();
                                // c++;
                            }

                            else if (Subjects[a].equals("s4")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getPhysics());
                                re = ((ExamDbDataHolder) users.get(i)).getPhysics();
                                //   row[c] = ((ExamDbDataHolder)users.get(i)).getPhysics();
                                //  c++;
                            } else if (Subjects[a].equals("s5")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getChemistry());
                                re = ((ExamDbDataHolder) users.get(i)).getChemistry();
                                // row[c] = ((ExamDbDataHolder)users.get(i)).getChemistry();
                                // c++;
                            } else if (Subjects[a].equals("s6")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getBiology());
                                re = ((ExamDbDataHolder) users.get(i)).getBiology();
                                //   row[c] = ((ExamDbDataHolder)users.get(i)).getBiology();
                                //  c++;
                            } else if (Subjects[a].equals("s7")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getHistory());
                                re = ((ExamDbDataHolder) users.get(i)).getHistory();
                                //  row[c] = ((ExamDbDataHolder)users.get(i)).getHistory();
                                //  c++;
                            } else if (Subjects[a].equals("s8")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getGeography());
                                re = ((ExamDbDataHolder) users.get(i)).getGeography();
                                // row[c] = ((ExamDbDataHolder)users.get(i)).getGeography();
                                // c++;
                            } else if (Subjects[a].equals("s9")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getCre());
                                re = ((ExamDbDataHolder) users.get(i)).getCre();
                                //  row[c] = ((ExamDbDataHolder)users.get(i)).getCre();
                                //  c++;
                            } else if (Subjects[a].equals("s10")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getIre());
                                re = ((ExamDbDataHolder) users.get(i)).getIre();
                                //   row[c] = ((ExamDbDataHolder)users.get(i)).getIre();
                                //  c++;
                            } else if (Subjects[a].equals("s11")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getHre());
                                re = ((ExamDbDataHolder) users.get(i)).getHre();
                                //   row[c] = ((ExamDbDataHolder)users.get(i)).getHre();
                                //  c++;
                            } else if (Subjects[a].equals("s12")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getAgriculture());
                                re = ((ExamDbDataHolder) users.get(i)).getAgriculture();
                                //   row[c] = ((ExamDbDataHolder)users.get(i)).getAgriculture();
                                //  c++;
                            } else if (Subjects[a].equals("s13")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getHomescience());
                                re = ((ExamDbDataHolder) users.get(i)).getHomescience();
                                //  row[c] = ((ExamDbDataHolder)users.get(i)).getHomescience();
                                //  c++;
                            } else if (Subjects[a].equals("s14")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getArtdesign());
                                re = ((ExamDbDataHolder) users.get(i)).getArtdesign();
                                //   row[c] = ((ExamDbDataHolder)users.get(i)).getArtdesign();
                                //  c++;
                            } else if (Subjects[a].equals("s15")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getComputer());
                                re = ((ExamDbDataHolder) users.get(i)).getComputer();
                                // row[c] = ((ExamDbDataHolder)users.get(i)).getComputer();
                                // c++;

                            } else if (Subjects[a].equals("s16")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getBuilding());
                                re = ((ExamDbDataHolder) users.get(i)).getBuilding();
                                // row[c] = ((ExamDbDataHolder)users.get(i)).getBuilding();
                                //  c++;
                            } else if (Subjects[a].equals("s17")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getWoodwork());
                                re = ((ExamDbDataHolder) users.get(i)).getWoodwork();
                                //  row[c] = ((ExamDbDataHolder)users.get(i)).getWoodwork();
                                // c++;
                            } else if (Subjects[a].equals("s18")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getMetalwork());
                                re = ((ExamDbDataHolder) users.get(i)).getMetalwork();
                                //  row[c] = ((ExamDbDataHolder)users.get(i)).getMetalwork();
                                // c++;
                            } else if (Subjects[a].equals("s19")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getMusic());
                                re = ((ExamDbDataHolder) users.get(i)).getMusic();
                                //   row[c] = ((ExamDbDataHolder)users.get(i)).getMusic();
                                //  c++;
                            } else if (Subjects[a].equals("s20")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getFrench());
                                re = ((ExamDbDataHolder) users.get(i)).getFrench();
                                // row[c] = ((ExamDbDataHolder)users.get(i)).getFrench();
                                // c++;
                            } else if (Subjects[a].equals("s21")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getGerman());
                                re = ((ExamDbDataHolder) users.get(i)).getGerman();
                                //  row[c] = ((ExamDbDataHolder)users.get(i)).getGerman();
                                // c++;
                            } else if (Subjects[a].equals("s22")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getArabic());
                                re = ((ExamDbDataHolder) users.get(i)).getArabic();
                                // row[c] = ((ExamDbDataHolder)users.get(i)).getArabic();
                                // c++;
                            } else if (Subjects[a].equals("s23")) {
                                tbl2.addCell(((ExamDbDataHolder) users.get(i)).getBusiness());
                                re = ((ExamDbDataHolder) users.get(i)).getBusiness();
                                //  row[c] = ((ExamDbDataHolder)users.get(i)).getBusiness();
                                // c++;
                            }
                            String[] gr = nn.checkGrade(re, Subjects[a]);
                            tbl2.addCell(gr[0]);
                            tbl2.addCell(gr[1]);
                            tbl2.addCell("");
                        }
                    }

                    //add for all other subjects

                    PdfPTable tbl3 = new PdfPTable(4);
                    //  tbl.setWidthPercentage(100);
                    tbl3.setTotalWidth(575);
                    tbl3.setLockedWidth(true);
                    tbl3.setSpacingBefore(8);
                    tbl3.setSpacingAfter(6);
                    //  tbl3.getDefaultCell().setBorderWidthTop(0);
                    tbl3.getDefaultCell().setBorder(0);
                    //  tbl3.getDefaultCell().setBorderWidthLeft(0);
                    //  tbl3.getDefaultCell().setBorderWidthRight(0);
                    tbl3.setWidths(new int[] { 1, 1, 1, 1 });
                    tbl3.addCell(creatTextCellHeader("CURRENT MARKS:"));
                    tbl3.addCell(creatTextCellHeader(String.valueOf(((ExamDbDataHolder) users.get(0)).getTotal())));
                    float g = Float.valueOf(((ExamDbDataHolder) users.get(0)).getTotal());
                    //  tbl.addCell(String.format("%.1f", g));

                    String gr = nn.checkGrade(yearid, String.format("%.1f", g / getYear()));

                    tbl3.addCell(creatTextCellHeader("PREVIOUS MARKS:"));
                    tbl3.addCell(creatTextCellHeader("459"));
                    tbl3.addCell(creatTextCellHeader("AVERAGE MARKS:"));
                    tbl3.addCell(creatTextCellHeader(String.format("%.1f", g / getYear())));
                    tbl3.addCell(creatTextCellHeader("PREVIOUS AVERAGE:"));
                    tbl3.addCell(creatTextCellHeader("50"));
                    tbl3.addCell(creatTextCellHeader("MEAN GRADE:"));
                    tbl3.addCell(creatTextCellHeader(gr));
                    tbl3.addCell(creatTextCellHeader("PREVIOUS MEAN GRADE:"));
                    tbl3.addCell(creatTextCellHeader("B"));
                    tbl3.addCell(creatTextCellHeader("POS"));
                    tbl3.addCell(creatTextCellHeader("3 "));
                    tbl3.addCell(creatTextCellHeader("OUT OF"));
                    tbl3.addCell(creatTextCellHeader("79"));

                    PdfPTable tbl4 = new PdfPTable(2);
                    //  tbl.setWidthPercentage(100);
                    tbl4.setHorizontalAlignment(0);
                    //tbl4.HorizontalAlignment=Element.ALIGN_LEFT;
                    tbl4.setTotalWidth(575 / 2);
                    tbl4.setLockedWidth(true);
                    tbl4.setSpacingBefore(8);
                    tbl4.setSpacingAfter(6);
                    //  tbl3.getDefaultCell().setBorderWidthTop(0);
                    tbl4.getDefaultCell().setBorder(0);
                    //  tbl3.getDefaultCell().setBorderWidthLeft(0);
                    //  tbl3.getDefaultCell().setBorderWidthRight(0);
                    tbl4.setWidths(new int[] { 2, 1 });
                    PdfPCell cell = new PdfPCell(new Paragraph("REMARKS"));
                    cell.setColspan(2);

                    tbl4.addCell(cell);
                    tbl4.addCell(creatTextCellHeader(
                            "CLASS TEACHERS........................................................."
                                    + ":............................\n\n\n"));
                    tbl4.addCell(creatTextCellChart("chart"));
                    tbl4.addCell(creatTextCellHeader(
                            "SIGNATURE-------------------------------------------------------------"
                                    + "-------------------------------\n\n\n"));
                    tbl4.addCell(creatTextCellHeader(
                            "-------------\n\n\n-----------------------------------------------------------"
                                    + "-----------------------------"));

                    pdfp.add(tbl1);

                    pdfp.add(tbl);

                    pdfp.add(tbl2);
                    pdfp.add(tbl3);
                    pdfp.add(tbl4);

                    //        Paragraph p=new Paragraph();
                    //        p.setAlignment(Element.ALIGN_CENTER);
                    //        p.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD,18,Font.BOLD));
                    //         Paragraph po=new Paragraph();
                    //        po.setAlignment(Element.ALIGN_CENTER);
                    //        po.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD,16,Font.BOLD));
                    //        
                    //        Paragraph pd=new Paragraph();
                    //          pd.setAlignment(Element.ALIGN_CENTER);
                    //         pd.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD,14,Font.BOLD));
                    //        p.add("ITHANGA SECONDARY SCHOOL");
                    //        po.add("PO.BOX 238  ITHANGA THIKA");
                    //         pd.add(new Date().toString());
                    //        pdfp.add(p);
                    //        pdfp.add(po);
                    //        pdfp.add(pd);
                    //         
                    //        
                    //      
                    //        pdfp.add(new Paragraph("\n.................................................................."
                    //                + ".................................................................................\n"));
                    //         String []names=  n.findSubjectname();
                    //        PdfPTable tbl=new PdfPTable(names.length+6);
                    //        tbl.setWidths(new float[]{1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
                    //       // tbl.setWidthPercentage(100);
                    //        tbl.setTotalWidth(575);
                    //        tbl.setLockedWidth(true);
                    //         PdfPTable tbl1=new PdfPTable(names.length+6);
                    //        tbl1.setWidths(new float[]{1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
                    //       // tbl.setWidthPercentage(100);
                    //        tbl1.setTotalWidth(575);
                    //        tbl1.setLockedWidth(true);
                    //     PdfPCell cell=new PdfPCell (new Paragraph("RESULTS"));
                    //     cell.setColspan((names.length+4)*2);
                    //     cell.setBackgroundColor(Color.CYAN);
                    //        tbl1.addCell(cell);
                    //        tbl1.addCell("Pos");
                    //          tbl1.addCell("id");
                    //          tbl1.addCell("Name");
                    //          int a;

                    pdfp.close();
                } catch (Exception j) {
                    j.printStackTrace();
                }

                // Image img=new Image.getInstance("j.png");

            }
        }
    }

    public PdfPCell creatTextCellHeader(String text) {
        PdfPCell cell = new PdfPCell();
        Paragraph p = new Paragraph();
        p.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD));
        p.add(text);
        cell.addElement(p);
        cell.setBorder(Rectangle.NO_BORDER);
        return cell;
    }

    public PdfPCell creatTextCellChart(String text) {
        PdfPCell cell = new PdfPCell();
        Paragraph p = new Paragraph();
        p.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD));
        p.add(text);
        cell.addElement(p);
        cell.setRowspan(2);
        cell.setBorder(Rectangle.NO_BORDER);
        return cell;
    }

    public PdfPCell createImageCell(String path) throws IOException {
        PdfPCell cell = null;
        try {
            Image img = Image.getInstance(path);
            cell = new PdfPCell(img, true);
            cell.setFixedHeight(60);
            cell.setBorder(Rectangle.NO_BORDER);

        } catch (BadElementException ex) {
            Logger.getLogger(ReportForms.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ReportForms.class.getName()).log(Level.SEVERE, null, ex);
        }

        return cell;
    }

    public PdfPCell createTextCell(String text) {

        PdfPCell cell = new PdfPCell();
        Paragraph p = new Paragraph();
        p.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD, 16, Font.BOLD));
        //  p.setFont(Font.BOLD);
        p.setAlignment(Element.ALIGN_CENTER);
        p.add(text);
        cell.addElement(p);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setVerticalAlignment(Rectangle.NO_BORDER);

        return cell;

    }

    /**
     * @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(ReportForms.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ReportForms.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ReportForms.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ReportForms.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 ReportForms().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox<String> jComboBox2;
    private javax.swing.JComboBox<String> jComboBox3;
    private javax.swing.JComboBox<String> jComboBox4;
    private javax.swing.JComboBox<String> jComboBox5;
    private javax.swing.JComboBox<String> jComboBox6;
    private javax.swing.JComboBox<String> jComboBox7;
    private javax.swing.JComboBox<String> jComboBoxExam;
    private javax.swing.JLabel jLabel1;
    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 jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private com.toedter.calendar.JYearChooser jYearChooser1;
    private javax.swing.JTextField sid;
    // End of variables declaration//GEN-END:variables

    public int getYear() {
        int subjects = 0;
        switch (yearid) {
        case 1:
            subjects = 11;
            break;
        case 2:
            subjects = 11;
            break;
        case 3:
            subjects = 8;
            break;
        case 4:
            subjects = 8;
            break;

        }
        return subjects;
    }

    public void checkPreviousResults(String uni, String k, String kcpegrade, String id) {
        String previousMarks = "null";
        String previousGrade = "null";
        String previousMean = "null";
        int ln = Exams.length;

        String ny = null;
        String nf = null;
        String nt = null;
        String ne = null;
        char fr = uni.charAt(0);
        char tr = uni.charAt(1);
        char er = uni.charAt(2);
        String yu = uni.substring(uni.length() - 4);
        int ye = Integer.valueOf(yu);
        int yg = Integer.valueOf(String.valueOf(fr));
        char a = '1';
        char b = '2';
        JOptionPane.showMessageDialog(null, yg);
        if (tr == a && er == a && fr == 1) {
            int grd = Integer.valueOf(k) / 5;
            previousMarks = k;
            previousMean = String.valueOf(grd);
            previousGrade = kcpegrade;
            JOptionPane.showMessageDialog(null, uni);

        } else if (tr == a && er == a && fr == b) {
            //int yg=Integer.valueOf(fr);
            nt = "3";
            ny = String.valueOf(ye - 1);
            ne = String.valueOf(ln);
            nf = String.valueOf(yg - 1);

            String newUniq = nf + nt + ne + id + ny;
            JOptionPane.showMessageDialog(null, newUniq);
        }

    }

    public void check(String uniq) {

    }
}