×

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:07 PM
Views: 10
Tags: no tags
GUI class to add ,edit supplier related information
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package inventorysystem.gui;
  7.  
  8. import interfaces.Switchable;
  9. import inventorysystem.data.Suppler;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JPanel;
  15.  
  16. /**
  17.  *
  18.  * @author Epic
  19.  */
  20. public class Supplier extends javax.swing.JPanel implements Switchable {
  21.  
  22.     private boolean businessNameExist = false;
  23.  
  24.     /**
  25.      * Creates new form Supplier
  26.      */
  27.     public Supplier() {
  28.         initComponents();
  29.         this.setName("Suppler");
  30.     }
  31.  
  32.     /**
  33.      * This method is called from within the constructor to initialize the form.
  34.      * WARNING: Do NOT modify this code. The content of this method is always
  35.      * regenerated by the Form Editor.
  36.      */
  37.     @SuppressWarnings("unchecked")
  38.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  39.     private void initComponents() {
  40.  
  41.         jLabel1 = new javax.swing.JLabel();
  42.         txtName = new javax.swing.JTextField();
  43.         jLabel2 = new javax.swing.JLabel();
  44.         txtAddress = new javax.swing.JTextField();
  45.         jLabel3 = new javax.swing.JLabel();
  46.         txtPhoneno = new javax.swing.JTextField();
  47.         jLayeredPane1 = new javax.swing.JLayeredPane();
  48.         btnSave = new javax.swing.JButton();
  49.         btnClear = new javax.swing.JButton();
  50.         jLabel4 = new javax.swing.JLabel();
  51.         txtBusinessName = new javax.swing.JTextField();
  52.  
  53.         jLabel1.setText("Name:");
  54.  
  55.         jLabel2.setText("Address:");
  56.  
  57.         jLabel3.setText("Phone No:");
  58.  
  59.         jLayeredPane1.setLayout(new java.awt.GridLayout());
  60.  
  61.         btnSave.setText("Save");
  62.         btnSave.addActionListener(new java.awt.event.ActionListener() {
  63.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  64.                 btnSaveActionPerformed(evt);
  65.             }
  66.         });
  67.         jLayeredPane1.add(btnSave);
  68.  
  69.         btnClear.setText("Clear");
  70.         btnClear.addActionListener(new java.awt.event.ActionListener() {
  71.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  72.                 btnClearActionPerformed(evt);
  73.             }
  74.         });
  75.         jLayeredPane1.add(btnClear);
  76.  
  77.         jLabel4.setText("Business Name");
  78.  
  79.         txtBusinessName.addFocusListener(new java.awt.event.FocusAdapter() {
  80.             public void focusLost(java.awt.event.FocusEvent evt) {
  81.                 txtBusinessNameFocusLost(evt);
  82.             }
  83.         });
  84.  
  85.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  86.         this.setLayout(layout);
  87.         layout.setHorizontalGroup(
  88.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  89.             .addGroup(layout.createSequentialGroup()
  90.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  91.                     .addGroup(layout.createSequentialGroup()
  92.                         .addGap(22, 22, 22)
  93.                         .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE))
  94.                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  95.                         .addGap(23, 23, 23)
  96.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  97.                             .addComponent(jLabel1)
  98.                             .addComponent(txtAddress)
  99.                             .addComponent(jLabel3)
  100.                             .addComponent(txtPhoneno)
  101.                             .addComponent(jLabel4)
  102.                             .addComponent(jLabel2)
  103.                             .addComponent(txtBusinessName)
  104.                             .addComponent(txtName))))
  105.                 .addGap(41, 41, 41))
  106.         );
  107.         layout.setVerticalGroup(
  108.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  109.             .addGroup(layout.createSequentialGroup()
  110.                 .addContainerGap()
  111.                 .addComponent(jLabel1)
  112.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  113.                 .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  114.                 .addGap(2, 2, 2)
  115.                 .addComponent(jLabel4)
  116.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  117.                 .addComponent(txtBusinessName, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  118.                 .addGap(7, 7, 7)
  119.                 .addComponent(jLabel2)
  120.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  121.                 .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  122.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  123.                 .addComponent(jLabel3)
  124.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  125.                 .addComponent(txtPhoneno, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  126.                 .addGap(30, 30, 30)
  127.                 .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  128.                 .addContainerGap(109, Short.MAX_VALUE))
  129.         );
  130.     }// </editor-fold>                        
  131.  
  132.     private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
  133.         // TODO add your handling code here:
  134.         Suppler newSuppler = new Suppler(0,
  135.                 txtName.getText(),
  136.                 txtAddress.getText(),
  137.                 txtPhoneno.getText(),
  138.                 txtBusinessName.getText()
  139.         );
  140.         if (!businessNameExist) {
  141.             try {
  142.                 boolean saved = newSuppler.addSupplier();
  143.                 if (!saved) {
  144.                     JOptionPane.showMessageDialog(null, "New suppler saved", "Information", JOptionPane.INFORMATION_MESSAGE);
  145.                     clearFields();
  146.                 } else if (saved) {
  147.                     JOptionPane.showMessageDialog(null, "Error saving suppler", "Information", JOptionPane.ERROR_MESSAGE);
  148.                 }
  149.             } catch (SQLException ex) {
  150.                 Logger.getLogger(Supplier.class.getName()).log(Level.SEVERE, null, ex);
  151.             }
  152.         }else{
  153.             JOptionPane.showMessageDialog(null, "Business Name must be unique", "Information", JOptionPane.INFORMATION_MESSAGE);
  154.         }
  155.     }                                      
  156.  
  157.     private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                        
  158.         // TODO add your handling code here:
  159.         clearFields();
  160.     }                                        
  161.  
  162.     private void txtBusinessNameFocusLost(java.awt.event.FocusEvent evt) {                                          
  163.         // TODO add your handling code here:
  164.         Suppler checkBusinessName = new Suppler();
  165.         try {
  166.  
  167.             if (checkBusinessName.checkIfExist("supplier_biz_name", txtBusinessName.getText())) {
  168.                 JOptionPane.showMessageDialog(null, "This Business name already exist");
  169.                 businessNameExist = true;
  170.             }
  171.  
  172.         } catch (SQLException ex) {
  173.             Logger.getLogger(Supplier.class.getName()).log(Level.SEVERE, null, ex);
  174.         }
  175.     }                                        
  176.  
  177.  
  178.     // Variables declaration - do not modify                    
  179.     private javax.swing.JButton btnClear;
  180.     private javax.swing.JButton btnSave;
  181.     private javax.swing.JLabel jLabel1;
  182.     private javax.swing.JLabel jLabel2;
  183.     private javax.swing.JLabel jLabel3;
  184.     private javax.swing.JLabel jLabel4;
  185.     private javax.swing.JLayeredPane jLayeredPane1;
  186.     private javax.swing.JTextField txtAddress;
  187.     private javax.swing.JTextField txtBusinessName;
  188.     private javax.swing.JTextField txtName;
  189.     private javax.swing.JTextField txtPhoneno;
  190.     // End of variables declaration                  
  191.  
  192.     @Override
  193.     public void OnPanelSwitched(JPanel panelToSwitch) {
  194.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  195.     }
  196.  
  197.     /**
  198.      * Clear All fields in the form.
  199.      */
  200.     private void clearFields() {
  201.         txtName.setText("");
  202.         txtAddress.setText("");
  203.         txtPhoneno.setText("");
  204.         txtBusinessName.setText("");
  205.     }
  206. }
  207.