×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Israel Edet
Added: Jan 4, 2017 10:06 PM
Modified: Jan 4, 2017 10:26 PM
Views: 13
Tags: no tags
GUI class to handle adding of products
  1.  
  2. package inventorysystem.gui;
  3.  
  4. import com.sun.glass.events.KeyEvent;
  5. import interfaces.Switchable;
  6. import inventorysystem.data.NumberFormater;
  7. import inventorysystem.data.Product;
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10. import java.util.Vector;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.swing.DefaultComboBoxModel;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16.  
  17. /**
  18.  *
  19.  * @author Epic
  20.  */
  21. public class Products extends javax.swing.JPanel implements Switchable {
  22.  
  23.     /**
  24.      * Creates new form Products
  25.      */
  26.     public Products() {
  27.         initComponents();
  28.         this.setName("Product");
  29.         this.fillSupplierList();
  30.        
  31.        
  32.     }
  33.    
  34.    
  35.     /**
  36.      * This method is called from within the constructor to initialize the form.
  37.      * WARNING: Do NOT modify this code. The content of this method is always
  38.      * regenerated by the Form Editor.
  39.      */
  40.     @SuppressWarnings("unchecked")
  41.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  42.     private void initComponents() {
  43.  
  44.         jLabel1 = new javax.swing.JLabel();
  45.         txtName = new javax.swing.JTextField();
  46.         jLabel2 = new javax.swing.JLabel();
  47.         jScrollPane1 = new javax.swing.JScrollPane();
  48.         taDescription = new javax.swing.JTextArea();
  49.         jLabel3 = new javax.swing.JLabel();
  50.         jLabel4 = new javax.swing.JLabel();
  51.         jLabel5 = new javax.swing.JLabel();
  52.         txtQuantity = new javax.swing.JTextField();
  53.         jLabel6 = new javax.swing.JLabel();
  54.         cbmSupplier = new javax.swing.JComboBox<>();
  55.         jLayeredPane1 = new javax.swing.JLayeredPane();
  56.         btnSave = new javax.swing.JButton();
  57.         btnCancel = new javax.swing.JButton();
  58.         jLabel7 = new javax.swing.JLabel();
  59.         cbmManufactureDate = new datechooser.beans.DateChooserCombo();
  60.         cbmExpiryDate = new datechooser.beans.DateChooserCombo();
  61.         jLabel8 = new javax.swing.JLabel();
  62.         jLabel9 = new javax.swing.JLabel();
  63.         txtBatchNo = new javax.swing.JTextField();
  64.         txtCost = new javax.swing.JFormattedTextField();
  65.         txtSales = new javax.swing.JFormattedTextField();
  66.         costError = new javax.swing.JLabel();
  67.         salesError = new javax.swing.JLabel();
  68.  
  69.         jLabel1.setText("Name:");
  70.  
  71.         jLabel2.setText("Description:");
  72.  
  73.         taDescription.setColumns(20);
  74.         taDescription.setRows(5);
  75.         jScrollPane1.setViewportView(taDescription);
  76.  
  77.         jLabel3.setText("Cost Price:");
  78.  
  79.         jLabel4.setText("Sale Price:");
  80.  
  81.         jLabel5.setText("Quantity:");
  82.  
  83.         jLabel6.setText("Supplier:");
  84.  
  85.         jLayeredPane1.setLayout(new java.awt.GridLayout(1, 0));
  86.  
  87.         btnSave.setText("Save");
  88.         btnSave.addActionListener(new java.awt.event.ActionListener() {
  89.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  90.                 btnSaveActionPerformed(evt);
  91.             }
  92.         });
  93.         jLayeredPane1.add(btnSave);
  94.  
  95.         btnCancel.setText("Cancel");
  96.         btnCancel.addActionListener(new java.awt.event.ActionListener() {
  97.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  98.                 btnCancelActionPerformed(evt);
  99.             }
  100.         });
  101.         jLayeredPane1.add(btnCancel);
  102.  
  103.         jLabel7.setText("Manufacture Date:");
  104.  
  105.         cbmManufactureDate.setCurrentView(new datechooser.view.appearance.AppearancesList("Swing",
  106.             new datechooser.view.appearance.ViewAppearance("custom",
  107.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  108.                     new java.awt.Color(222, 222, 222),
  109.                     new java.awt.Color(0, 0, 255),
  110.                     false,
  111.                     true,
  112.                     new datechooser.view.appearance.swing.ButtonPainter()),
  113.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  114.                     new java.awt.Color(222, 222, 222),
  115.                     new java.awt.Color(0, 0, 255),
  116.                     true,
  117.                     true,
  118.                     new datechooser.view.appearance.swing.ButtonPainter()),
  119.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  120.                     new java.awt.Color(0, 0, 255),
  121.                     new java.awt.Color(0, 0, 255),
  122.                     false,
  123.                     true,
  124.                     new datechooser.view.appearance.swing.ButtonPainter()),
  125.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  126.                     new java.awt.Color(128, 128, 128),
  127.                     new java.awt.Color(0, 0, 255),
  128.                     false,
  129.                     true,
  130.                     new datechooser.view.appearance.swing.LabelPainter()),
  131.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  132.                     new java.awt.Color(222, 222, 222),
  133.                     new java.awt.Color(0, 0, 255),
  134.                     false,
  135.                     true,
  136.                     new datechooser.view.appearance.swing.LabelPainter()),
  137.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  138.                     new java.awt.Color(222, 222, 222),
  139.                     new java.awt.Color(255, 0, 0),
  140.                     false,
  141.                     false,
  142.                     new datechooser.view.appearance.swing.ButtonPainter()),
  143.                 (datechooser.view.BackRenderer)null,
  144.                 false,
  145.                 true)));
  146.  
  147.     jLabel8.setText("Expiry Date:");
  148.  
  149.     jLabel9.setText("Batch Number:");
  150.  
  151.     txtCost.addActionListener(new java.awt.event.ActionListener() {
  152.         public void actionPerformed(java.awt.event.ActionEvent evt) {
  153.             txtCostActionPerformed(evt);
  154.         }
  155.     });
  156.     txtCost.addKeyListener(new java.awt.event.KeyAdapter() {
  157.         public void keyTyped(java.awt.event.KeyEvent evt) {
  158.             txtCostKeyTyped(evt);
  159.         }
  160.     });
  161.  
  162.     txtSales.addKeyListener(new java.awt.event.KeyAdapter() {
  163.         public void keyTyped(java.awt.event.KeyEvent evt) {
  164.             txtSalesKeyTyped(evt);
  165.         }
  166.     });
  167.  
  168.     costError.setForeground(new java.awt.Color(204, 0, 0));
  169.  
  170.     salesError.setForeground(new java.awt.Color(204, 0, 0));
  171.  
  172.     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  173.     this.setLayout(layout);
  174.     layout.setHorizontalGroup(
  175.         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  176.         .addGroup(layout.createSequentialGroup()
  177.             .addContainerGap()
  178.             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  179.                 .addGroup(layout.createSequentialGroup()
  180.                     .addComponent(cbmManufactureDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  181.                     .addGap(18, 18, 18)
  182.                     .addComponent(cbmExpiryDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  183.                     .addGap(18, 18, 18)
  184.                     .addComponent(txtBatchNo))
  185.                 .addComponent(txtName)
  186.                 .addComponent(txtQuantity)
  187.                 .addComponent(jScrollPane1)
  188.                 .addComponent(cbmSupplier, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  189.                 .addComponent(jLayeredPane1)
  190.                 .addComponent(txtCost)
  191.                 .addGroup(layout.createSequentialGroup()
  192.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  193.                         .addComponent(jLabel1)
  194.                         .addComponent(jLabel2)
  195.                         .addGroup(layout.createSequentialGroup()
  196.                             .addComponent(jLabel3)
  197.                             .addGap(18, 18, 18)
  198.                             .addComponent(costError, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
  199.                         .addGroup(layout.createSequentialGroup()
  200.                             .addComponent(jLabel4)
  201.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  202.                             .addComponent(salesError, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE))
  203.                         .addComponent(jLabel5)
  204.                         .addComponent(jLabel6)
  205.                         .addGroup(layout.createSequentialGroup()
  206.                             .addComponent(jLabel7)
  207.                             .addGap(84, 84, 84)
  208.                             .addComponent(jLabel8)
  209.                             .addGap(115, 115, 115)
  210.                             .addComponent(jLabel9)))
  211.                     .addGap(0, 109, Short.MAX_VALUE))
  212.                 .addComponent(txtSales))
  213.             .addContainerGap())
  214.     );
  215.     layout.setVerticalGroup(
  216.         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  217.         .addGroup(layout.createSequentialGroup()
  218.             .addContainerGap()
  219.             .addComponent(jLabel1)
  220.             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  221.             .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  222.             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  223.             .addComponent(jLabel2)
  224.             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  225.             .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
  226.             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  227.             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  228.                 .addComponent(jLabel3)
  229.                 .addComponent(costError))
  230.             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  231.             .addComponent(txtCost, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  232.             .addGap(25, 25, 25)
  233.             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  234.                 .addComponent(jLabel4)
  235.                 .addComponent(salesError))
  236.             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  237.             .addComponent(txtSales, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  238.             .addGap(25, 25, 25)
  239.             .addComponent(jLabel5)
  240.             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  241.             .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  242.             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  243.             .addComponent(jLabel6)
  244.             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  245.             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  246.                 .addGroup(layout.createSequentialGroup()
  247.                     .addComponent(cbmSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  248.                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  249.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  250.                         .addComponent(jLabel7)
  251.                         .addComponent(jLabel9)))
  252.                 .addComponent(jLabel8))
  253.             .addGap(18, 18, 18)
  254.             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  255.                 .addGroup(layout.createSequentialGroup()
  256.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  257.                         .addComponent(cbmManufactureDate, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  258.                         .addComponent(cbmExpiryDate, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
  259.                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 45, Short.MAX_VALUE)
  260.                     .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  261.                 .addGroup(layout.createSequentialGroup()
  262.                     .addComponent(txtBatchNo, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  263.                     .addGap(0, 0, Short.MAX_VALUE)))
  264.             .addContainerGap())
  265.     );
  266.     }// </editor-fold>                        
  267.  
  268.     private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
  269.         // TODO add your handling code here:
  270.         Product product = new Product(0, txtName.getText(),
  271.                 Integer.parseInt(txtQuantity.getText()),
  272.                 Float.valueOf(txtSales.getText()),
  273.                 "", taDescription.getText(),
  274.                 cbmManufactureDate.getText(),
  275.                 cbmExpiryDate.getText(),
  276.                  
  277.                 txtBatchNo.getText(),
  278.                 Float.valueOf(txtCost.getText()));
  279.        
  280.                
  281.        
  282.        
  283.         try {
  284.             // since the login class has access to the currently logged in employee
  285.             if(product.addProduct(
  286.                     ((String)cbmSupplier.getSelectedItem()),//supplier from the combo selection
  287.                     login.getCurrentEmployee().getEmployeeId() //currently logged in employee in the system
  288.             )){
  289.                 JOptionPane.showMessageDialog(null, "New Product saved..");
  290.             }
  291.         } catch (SQLException ex) {
  292.             Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  293.         }
  294.     }                                      
  295.  
  296.     private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
  297.         // TODO add your handling code here:
  298.     }                                        
  299.  
  300.     private void txtCostActionPerformed(java.awt.event.ActionEvent evt) {                                        
  301.         // TODO add your handling code here:
  302.     }                                      
  303.  
  304.     private void txtCostKeyTyped(java.awt.event.KeyEvent evt) {                                
  305.         // TODO add your handling code here:
  306.         NumberFormater.integerOnlyField(evt, costError);
  307.        
  308.     }                                
  309.  
  310.     private void txtSalesKeyTyped(java.awt.event.KeyEvent evt) {                                  
  311.         // TODO add your handling code here:
  312.         NumberFormater.integerOnlyField(evt, salesError);
  313.     }                                
  314.  
  315.  
  316.     // Variables declaration - do not modify                    
  317.     private javax.swing.JButton btnCancel;
  318.     private javax.swing.JButton btnSave;
  319.     private datechooser.beans.DateChooserCombo cbmExpiryDate;
  320.     private datechooser.beans.DateChooserCombo cbmManufactureDate;
  321.     private javax.swing.JComboBox<String> cbmSupplier;
  322.     private javax.swing.JLabel costError;
  323.     private javax.swing.JLabel jLabel1;
  324.     private javax.swing.JLabel jLabel2;
  325.     private javax.swing.JLabel jLabel3;
  326.     private javax.swing.JLabel jLabel4;
  327.     private javax.swing.JLabel jLabel5;
  328.     private javax.swing.JLabel jLabel6;
  329.     private javax.swing.JLabel jLabel7;
  330.     private javax.swing.JLabel jLabel8;
  331.     private javax.swing.JLabel jLabel9;
  332.     private javax.swing.JLayeredPane jLayeredPane1;
  333.     private javax.swing.JScrollPane jScrollPane1;
  334.     private javax.swing.JLabel salesError;
  335.     private javax.swing.JTextArea taDescription;
  336.     private javax.swing.JTextField txtBatchNo;
  337.     private javax.swing.JFormattedTextField txtCost;
  338.     private javax.swing.JTextField txtName;
  339.     private javax.swing.JTextField txtQuantity;
  340.     private javax.swing.JFormattedTextField txtSales;
  341.     // End of variables declaration                  
  342.  
  343.     @Override
  344.     public void OnPanelSwitched(JPanel panelToSwitch) {
  345.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  346.     }
  347.    
  348.     /**
  349.      * Get a list of suppliers and fill the suppler comboList
  350.      */
  351.     private void fillSupplierList(){
  352.         inventorysystem.data.Suppler s = new inventorysystem.data.Suppler();
  353.         Vector supplierList = new Vector(); // since a DefaultComboBoxModel takes a vector for initialization
  354.         supplierList.add(0,"Select Suppler");
  355.         try {
  356.             ArrayList<inventorysystem.data.Suppler> suppliers =  s.getSupplier("", true);
  357.             for (int i = 0; i < suppliers.size(); i++) {
  358.                
  359.                 supplierList.add(suppliers.get(i).getSupplerBusinessName());
  360.                 cbmSupplier.setModel(new DefaultComboBoxModel<>(supplierList));
  361.              }
  362.         } catch (SQLException ex) {
  363.             Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  364.         }
  365.     }
  366. }
  367.  

1 comment

Israel Edet 8 months ago
Hello Prof, on line 291 i needed the ID of the currently logged in user for log purposes, instead of going back to the database to get that data. i remembered that during login in the login.java class the employee object was populated and still persist. i now made the variable static so that i can use it in this class.
I tried to do this earlier by creating an instance of the login class but the variable (employee) returned null every time, but making it static solved the problem.
Question: was i right to have done this, and is there a better way? thanks sir

Write a comment