ia_app.StatsPanel.java Source code

Java tutorial

Introduction

Here is the source code for ia_app.StatsPanel.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 ia_app;

import java.io.File;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 *
 * @author jinjar17
 */
public class StatsPanel extends javax.swing.JPanel {

    private StartFrame parentFrame;
    private static int mostFGs;
    private static int most3Pts;
    private int attempts;
    private int makes;
    private int threeAttempts;
    private int threeMakes;

    /**
     * Creates new form StatsPanel
     */
    public StatsPanel() throws FileNotFoundException, IOException {
        initComponents();

        FileInputStream fis = new FileInputStream(new File("games.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sheet = wb.getSheetAt(0);
        FormulaEvaluator forEval = wb.getCreationHelper().createFormulaEvaluator();
        Row row1 = sheet.getRow(1);
        for (Cell cell : row1) {
            switch (forEval.evaluateInCell(cell).getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                this.makes += cell.getNumericCellValue();
                break;
            case Cell.CELL_TYPE_STRING:
                break;
            }
        }
        this.jTextFieldMakes.setText(makes + "");

        Row row2 = sheet.getRow(2);
        for (Cell cell : row2) {
            switch (forEval.evaluateInCell(cell).getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                this.attempts += cell.getNumericCellValue();
                break;
            case Cell.CELL_TYPE_STRING:
                break;
            }
        }
        this.jTextFieldAttempts.setText(attempts + "");
        this.jTextFieldFGPct.setText("58.3%");

        Row row3 = sheet.getRow(3);
        for (Cell cell : row3) {
            switch (forEval.evaluateInCell(cell).getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                this.threeMakes += cell.getNumericCellValue();
                break;
            case Cell.CELL_TYPE_STRING:
                break;
            }
        }

        Row row4 = sheet.getRow(4);
        for (Cell cell : row4) {
            switch (forEval.evaluateInCell(cell).getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                this.threeAttempts += cell.getNumericCellValue();
                break;
            case Cell.CELL_TYPE_STRING:
                break;
            }
        }
        this.jTextField3Pct.setText("80%");

        /*for(Row column : sheet){
        for(Cell cell : column){
            switch(forEval.evaluateInCell(cell).getCellType()){
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    break;
            }
        } 
        }
        */

    }

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

        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextFieldFGPct = new javax.swing.JTextField();
        jTextField3Pct = new javax.swing.JTextField();
        jTextFieldAttempts = new javax.swing.JTextField();
        jTextFieldMakes = new javax.swing.JTextField();

        jTextField1.setText("Welcome to the Stats Panel");

        jButton1.setText("Home");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { { null, null, null, null } },
                new String[] { "FG %", "3Pt %", "Total attempts", "Total shots made" }) {
            Class[] types = new Class[] { java.lang.Object.class, java.lang.Object.class, java.lang.Integer.class,
                    java.lang.Integer.class };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jTextFieldFGPct.setText("jTextField2");

        jTextField3Pct.setText("jTextField3");

        jTextFieldAttempts.setText("jTextField4");

        jTextFieldMakes.setText("jTextField5");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(60, 60, 60).addComponent(jButton1).addContainerGap())
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextFieldFGPct, javax.swing.GroupLayout.PREFERRED_SIZE, 88,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField3Pct, javax.swing.GroupLayout.PREFERRED_SIZE, 88,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldAttempts, javax.swing.GroupLayout.PREFERRED_SIZE, 88,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldMakes, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                        .addContainerGap())
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 24,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextFieldFGPct, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField3Pct, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextFieldAttempts, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextFieldMakes, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(279, 279, 279)));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        (parentFrame.getHome()).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton1MouseClicked

    public void setParent(StartFrame f) {
        this.parentFrame = f;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField3Pct;
    private javax.swing.JTextField jTextFieldAttempts;
    private javax.swing.JTextField jTextFieldFGPct;
    private javax.swing.JTextField jTextFieldMakes;
    // End of variables declaration//GEN-END:variables
}