Saturday, July 1, 2017

Student Information System in Swing Java

Here is a simple CRUD application that I named Student Information System in Swing Java that I wrote using Java programming language. Which allows the user to add, update, view and delete student records. All the student records in being store in a text file. The application is very basic and simple for learners to understand how Swing library in Java works. I hope you will find my work useful. I enjoy every minute working in this sample program. By the way I am using Netbeans as my tool in developing this program.  Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.








Sample Program Output


Program Listing

Student.java

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


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;

import java.io.FileWriter;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Jake R. Pomperada, MAED-IT
 * Date: July 1, 2017, Saturday
 * Email : jakerpomperada@gmail.com and jakerpomperada@outlook.com
 */
public class Student extends javax.swing.JFrame {

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

    /**
     * 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">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtFirstName = new javax.swing.JTextField();
        txtMiddleName = new javax.swing.JTextField();
        txtCourse = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        btnQuit = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        Table = new javax.swing.JTable();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtLastName = new javax.swing.JTextField();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(204, 204, 204));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Student ID");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("First Name");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Middle Name");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Last Name");

        txtFirstName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFirstNameActionPerformed(evt);
            }
        });

        txtCourse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCourseActionPerformed(evt);
            }
        });

        btnAdd.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAdd.setText("Add");
        btnAdd.setToolTipText("Click here to save student records.");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnClear.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnClear.setText("Clear");
        btnClear.setToolTipText("Click here to clear the text box.");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        btnQuit.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnQuit.setText("Quit ");
        btnQuit.setToolTipText("Click here to quit the program");
        btnQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitActionPerformed(evt);
            }
        });

        Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        Table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(Table);

        btnUpdate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.setToolTipText("Click here to update the student record.");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.setToolTipText("Click here to remove the student record.");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Course");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel6.setText("Student Information System");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 91, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(30, 30, 30)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtID)
                                    .addComponent(txtFirstName)
                                    .addComponent(txtMiddleName)
                                    .addComponent(txtCourse, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                                    .addComponent(txtLastName))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnQuit, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnUpdate))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMiddleName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(btnDelete))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtLastName, 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.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtCourse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnClear)
                    .addComponent(btnQuit))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>                        

    private void txtFirstNameActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            
    
    DefaultTableModel mod;
    String Id, FirstName, MiddleName, LastName,Course, line;
  
    
    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        mod = (DefaultTableModel)Table.getModel();
        mod.addColumn("ID");
        mod.addColumn("FIRST NAME");
        mod.addColumn("MIDDLE NAME");
        mod.addColumn("LAST NAME");
        mod.addColumn("COURSE");        
        
        try{
        BufferedReader bfw = new BufferedReader(new FileReader("student_db.txt"));
        while( (line = bfw.readLine() ) != null ) {
        mod.addRow( line.split(",") );
        }
            bfw.close();
        }
        catch(Exception ex){
        }

    }                                 

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
        this.Id = txtID.getText().toUpperCase();
        this.FirstName = txtFirstName.getText().toUpperCase();
        this.MiddleName = txtMiddleName.getText().toUpperCase();
        this.LastName = txtLastName.getText().toUpperCase();
        this.Course = txtCourse.getText().toUpperCase();
              
        this.txtID.setText("");
        this.txtFirstName.setText("");
        this.txtMiddleName.setText("");
        this.txtLastName.setText("");
        this.txtCourse.setText("");
        
        String st[] = {Id, FirstName, MiddleName, LastName,Course};
        mod.addRow(st);
        saveFile();
        
        this.txtID.requestFocus();
    }                                      

    private void saveFile(){
        try{
          File file = new File("student_db.txt");
          if(!file.exists()){
              file.createNewFile();
          }
          FileWriter fw = new FileWriter(file.getAbsoluteFile());
          BufferedWriter bw = new BufferedWriter(fw);
          
          for(int i=0; i<Table.getRowCount(); i++){
              for(int j=0; j<Table.getColumnCount(); j++){
                  bw.write((String)Table.getModel().getValueAt(i,j)+",");
              }
              bw.write("\n");
          }
        bw.close();
        fw.close();
      }catch(Exception ex){
          
      }
    }
    private void btnQuitActionPerformed(java.awt.event.ActionEvent evt) {                                        
       String message = "Do you want to quit the program?";
       String title = "Really Program Quit?";

    int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
    
    if (reply ==JOptionPane.NO_OPTION)
    {
        this.txtID.setText("");
        this.txtFirstName.setText("");
        this.txtMiddleName.setText("");
        this.txtLastName.setText("");
        this.txtCourse.setText(""); 
        this.txtID.requestFocus();
    }
    
    if (reply == JOptionPane.YES_OPTION)
    {
      System.exit(0);
    }
        
     
      
    }                                       

    private void TableMouseClicked(java.awt.event.MouseEvent evt) {                                   
        int index =Table.getSelectedRow();
        TableModel model = Table.getModel();
        Id = model.getValueAt(index, 0).toString().toUpperCase();
        FirstName = model.getValueAt(index, 1).toString().toUpperCase();
        MiddleName = model.getValueAt(index, 2).toString().toUpperCase();
        LastName = model.getValueAt(index, 3).toString().toUpperCase();
        Course = model.getValueAt(index, 4).toString().toUpperCase();
               
        txtID.setText(Id);
        txtFirstName.setText(FirstName);
        txtMiddleName.setText(MiddleName);
        txtLastName.setText(LastName);
        txtCourse.setText(Course);
    }                                  

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        mod = (DefaultTableModel) Table.getModel();
        
        if(Table.getSelectedRow()==-1){
            if(Table.getRowCount()==0){
                JOptionPane.showMessageDialog(rootPane,"Table is Empty");
            }else{
                JOptionPane.showMessageDialog(rootPane, "You must select a record");
            }
        }else{
            mod.setValueAt(txtID.getText().toUpperCase(),Table.getSelectedRow(), 0);
            mod.setValueAt(txtFirstName.getText().toUpperCase(), Table.getSelectedRow(), 1);
            mod.setValueAt(txtMiddleName.getText().toUpperCase(), Table.getSelectedRow(), 2);
            mod.setValueAt(txtLastName.getText().toUpperCase(), Table.getSelectedRow(), 3);
            mod.setValueAt(txtCourse.getText().toUpperCase(), Table.getSelectedRow(), 4);
        }
              
        saveFile();
    }                                         

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        mod = (DefaultTableModel) Table.getModel();
        
        if(Table.getSelectedRow()==-1){
            if(Table.getRowCount()==0){
                JOptionPane.showMessageDialog(rootPane,"Table is Empty");
            }else{
                JOptionPane.showMessageDialog(rootPane, "You must select a record");
            }
        }else{
            JOptionPane.showMessageDialog(rootPane, "Record is deleted!");
            mod.removeRow(Table.getSelectedRow());
            this.txtID.setText("");
            this.txtFirstName.setText("");
            this.txtMiddleName.setText("");
            this.txtLastName.setText("");
            this.txtCourse.setText("");
            
            this.txtID.requestFocus();
        }
        saveFile();
    }                                         

    private void txtCourseActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                         
            this.txtID.setText("");
            this.txtFirstName.setText("");
            this.txtMiddleName.setText("");
            this.txtLastName.setText("");
            this.txtCourse.setText("");
            
            this.txtID.requestFocus();
    }                                        

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

    // Variables declaration - do not modify                     
    private javax.swing.JTable Table;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnQuit;
    private javax.swing.JButton btnUpdate;
    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.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtCourse;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtMiddleName;
    // End of variables declaration                   
}










Sum of Two Numbers Using Swing in Java

Hi guys thank you for visiting my site in this article I would like to share with you a sample program that I wrote using Netbeans and Swing in Java. I called this program sum of two numbers using swing in Java. The code is very simple and easy to understand. From my experience writing a GUI application for desktop application in Netbeans is very easy and simple I hope you will find my work uesful. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.








Sample Program Output


Program Listing

sum_two.java


import javax.swing.JOptionPane;

/*
 * 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.
 */

/**
 *
 * @author Jacob
 */
public class sum_two extends javax.swing.JFrame {

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

    /**
     * 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">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_val1 = new javax.swing.JTextField();
        txt_val2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txt_sum = new javax.swing.JTextField();
        btn_quit = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btn_sum = new javax.swing.JButton();
        btn_clear1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Enter Value Number One");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Enter Value Number Two");

        txt_val1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        txt_val2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("The total sum is");

        txt_sum.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        btn_quit.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btn_quit.setText("Quit");
        btn_quit.setToolTipText("Click here to quit program.");
        btn_quit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_quitActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setText("Sum of Two Numbers in Java Swing");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setText("Created By Mr. Jake R. Pomperada");

        btn_sum.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btn_sum.setText("Sum");
        btn_sum.setToolTipText("Click here to find the sum of two numbers.");
        btn_sum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_sumActionPerformed(evt);
            }
        });

        btn_clear1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btn_clear1.setText("Clear");
        btn_clear1.setToolTipText("Click here to clear the text box.");
        btn_clear1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_clear1ActionPerformed(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()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txt_val2)
                    .addComponent(txt_val1)
                    .addComponent(txt_sum, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(btn_sum)
                .addGap(59, 59, 59)
                .addComponent(btn_clear1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btn_quit)
                .addGap(21, 21, 21))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(67, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addGap(91, 91, 91))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jLabel4)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_val1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_val2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_sum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_sum)
                    .addComponent(btn_clear1)
                    .addComponent(btn_quit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>                        

    private void btn_quitActionPerformed(java.awt.event.ActionEvent evt) {                                         
      String message = "Do you want to quit the program?";
      String title = "Really Program Quit?";

    int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
    
    if (reply ==JOptionPane.NO_OPTION)
    {
       this.txt_val1.setText("");
       this.txt_val2.setText("");
       this.txt_sum.setText("");
       
       this.txt_val1.requestFocus();
       
    }
    
    if (reply == JOptionPane.YES_OPTION)
    {
      System.exit(0);
    }
             
    }                                        

    private void btn_sumActionPerformed(java.awt.event.ActionEvent evt) {                                        
        int a = Integer.parseInt(this.txt_val1.getText());
        int b = Integer.parseInt(this.txt_val2.getText());
        
        int total_sum = (a+b);
        
        this.txt_sum.setText(String.valueOf(total_sum));
                
    }                                       

    private void btn_clear1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
       this.txt_val1.setText("");
       this.txt_val2.setText("");
       this.txt_sum.setText("");
       
       this.txt_val1.requestFocus();
       
    }                                          

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton btn_clear1;
    private javax.swing.JButton btn_quit;
    private javax.swing.JButton btn_sum;
    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.JTextField txt_sum;
    private javax.swing.JTextField txt_val1;
    private javax.swing.JTextField txt_val2;
    // End of variables declaration                   
}










Monday, June 26, 2017

Addition of Two Numbers Using Model View Controller in PHP

A program that demonstrate how to use the MVC or Model View Controller in PHP to add the sum of two numbers the code is very short and easy to understand. Thank you for visiting the site.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing

<?php
/**
 * Created by PhpStorm.
 * User: Jacob Pomperada
 * Date: 6/26/2017
 * Time: 8:24 AM
 */


class Model {
    public $a;
    public $b;

    public function __construct() {
        $this->a=5;
        $this->b=5;
    }
}

class View {
    private $model;

    public function __construct(Model $model) {
        $this->model = $model;
    }

    public function output() {
        echo "<body bgcolor='lightgreen'>";
        echo "<font-face='arial'>";
        echo "<br><br>";
        echo "<span style=\"font-size: 20px !important; font-weight:bold; font-family: arial; color: blue;\">";
        echo "Sum of Two Numbers Using Model View Controller Concepts in PHP </span>";
        echo "<br><br><br>";

        $sum = ($this->model->a + $this->model->b);

        $c = $this->model->a;
        $d = $this->model->b;

        echo "<span style=\"font-size: 20px !important; font-family: arial; font-weight:bold; color: blue;\">";
        echo " The sum of $c and $c is $sum. </span>";
        echo "</font></body>";
    }

}

class Controller {
    private $model;

    public function __construct(Model $model) {
        $this->model = $model;
    }

}

$model = new Model();
$controller = new Controller($model);
$view = new View($model);
if (isset($_GET['action'])) $controller->{$_GET['action']}();
echo $view->output();

?>

Hello World Using Model View Controller Concept in PHP

Here is a program that I wrote using PHP to demonstrate the concept of MVC or Model View Controller this program will display hello world message on the screen. The code is very easy to understand and use. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing

hello.php

<?php

class Model {
    public $text;

    public function __construct() {
        $this->text = 'Click Me';
    }
}

class View {
    private $model;

    public function __construct(Model $model) {
        $this->model = $model;
    }

    public function output() {
        echo "<body bgcolor='lightgreen'>";
        echo "<font-face='arial'>";
        echo "<br><br>";
        echo "<h1> Hello World Using Model View Controller Concepts in PHP </h1>";
        echo "<br>";
        return '<a href="demo.php?action=pressme">' . $this->model->text . '</a>';
        echo "</font></body>";
    }

}

class Controller {
    private $model;

    public function __construct(Model $model) {
        $this->model = $model;
    }

    public function pressme() {
        $this->model->text = 'Hello World Mr. Jake Pomperada !!!!';
    }
}


$model = new Model();
$controller = new Controller($model);
$view = new View($model);
if (isset($_GET['action'])) $controller->{$_GET['action']}();
echo $view->output();
?>