Demo entry 3368752

list

   

Submitted by jirapas on Dec 19, 2015 at 04:51
Language: Java. Code size: 5.1 kB.

package personel;

import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author sonicBlow
 */
public class ListEmployeeform extends javax.swing.JInternalFrame {
    ArrayList<Employee> empList = MainForm.empList;
    DefaultTableModel model;
    /**
     * Creates new form ListEmployeeform
     */
    public ListEmployeeform() {
        initComponents();
        
        model = (DefaultTableModel) jTable1.getModel();
        model.setRowCount(0);
        empList.add(new Salary(111, "John", "Java programmer", 30000 ));
        empList.add(new Hourly(222, "Paul", "Graphic Designer", 500, 200 ));
        empList.add(new Salary(333, "Jenny", "System Analyst", 40000 ));
        empList.add(new Salary(444, "David", "Web Developer", 25000 ));
        empList.add(new Hourly(555, "Kyle", "Java Programmer", 600, 300 ));
        updateEmployeeTable();

        
    }
    private void updateEmployeeTable() {
        model.setRowCount(0); //clear jTable row
        try {
            for (Employee emp : empList) {
                if (emp instanceof Salary) { // check object from this class ?
                Salary s = (Salary) emp;
                model.addRow(new Object[]{s.id, s.name,s.position, s.salary});
                }
                
            }
        } catch (Exception e) {
        JOptionPane.showMessageDialog(this, e);
        }
 }
    

                          

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
        
             int empId = Integer.parseInt(txtSearch.getText());
             model.setRowCount(0); //clear jTable row
             boolean found = false;
             String message = "";
             Salary s = null;
             try {
             for (Employee emp : empList) {
                if (emp instanceof Salary) {
                    if (emp.id == empId) {
                        found = true;
                        s = (Salary) emp;
                    }
                }
             }
             //update jTable
             if (found) {
                model.addRow(new Object[]{s.id, s.name,s.position, s.salary});
             } else {
                JOptionPane.showMessageDialog(this, "SalaryEmployee ID " + empId + " is not found.");
             }
             } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e);
             }
              
    }                                         

    private void btnShowAllActionPerformed(java.awt.event.ActionEvent evt) {                                           
        updateEmployeeTable();
    }                                          

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        
        int row = jTable1.getSelectedRow(); //if no rowselected, return -1
        if (row >= 0) {
            //get ID value from the first coloumn of selected row
            int empId = Integer.parseInt(jTable1.getModel().getValueAt(row,0).toString());
            int index = 0;
            //find index of object which matched the search id in EmpList
            for (int i = 0; i < empList.size(); i++) {
                if (empList.get(i) instanceof Salary) {              
                    if (empList.get(i).id == empId) {
                        index = i;
                    break;
                    }
                }
            }
            empList.remove(index);
            updateEmployeeTable();
        } else {
        JOptionPane.showMessageDialog(this, "No rowselected");
        }
    }                                         

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
         int row = jTable1.getSelectedRow();
         if (row >= 0) {
             int empId = Integer.parseInt(jTable1.getModel().getValueAt(row,0).toString());
             String name = jTable1.getModel().getValueAt(row,1).toString();
             System.out.println(name);
             String pos = jTable1.getModel().getValueAt(row,2).toString();
             
             for (int i = 0; i < empList.size(); i++) {
                if (empList.get(i) instanceof Salary) {              
                    if (empList.get(i).id == empId) {
                        empList.get(i).name = name ;
                        empList.get(i).position = pos ;
                    break;
                    }
                }
            }
            updateEmployeeTable(); 
         }else {
            JOptionPane.showMessageDialog(this, "No rowselected");
        }
         
    }                                       
    
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        
    } 

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).