Java tutorial
/* * 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 Forms; import Classes.CustomersUpdater; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import java.awt.Color; /** * * @author Vindu */ public class AddCustomers extends javax.swing.JFrame { /** * Creates new form AddCustomers */ public AddCustomers() { 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">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); fNameTxt = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); lNameTxt = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); memberCmbTxt = new javax.swing.JComboBox<>(); jLabel5 = new javax.swing.JLabel(); cusIdTxt = new javax.swing.JTextField(); jSeparator1 = new javax.swing.JSeparator(); addCusBtn = new javax.swing.JButton(); jLabel6 = new javax.swing.JLabel(); cardBtn = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setBackground(new java.awt.Color(25, 25, 25)); setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosed(java.awt.event.WindowEvent evt) { formWindowClosed(evt); } public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } }); jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/user-group-icon.png"))); // NOI18N jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N jLabel2.setText("First Name"); jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N jLabel3.setText("Last Name"); jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel4.setText("Membership"); memberCmbTxt .setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Platinam", "Gold", "Silver" })); memberCmbTxt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { memberCmbTxtActionPerformed(evt); } }); jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N jLabel5.setText("Set Card ID"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout .createSequentialGroup().addGap(20, 20, 20).addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel3).addComponent(jLabel2)) .addGap(26, 26, 26) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(lNameTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE) .addComponent(fNameTxt))) .addGroup(jPanel1Layout.createSequentialGroup().addGap(22, 22, 22) .addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jLabel5)) .addGap(18, 18, 18).addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(memberCmbTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cusIdTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)))) .addGap(20, 20, 20))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2) .addComponent(fNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(lNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(memberCmbTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(cusIdTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(23, Short.MAX_VALUE))); addCusBtn.setText("ADD Customer"); addCusBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addCusBtnActionPerformed(evt); } }); jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N jLabel6.setText("Add Customers"); cardBtn.setText("CARD "); cardBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cardBtnActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSeparator1) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(294, 294, 294).addComponent(jLabel6)) .addGroup(layout.createSequentialGroup().addGap(61, 61, 61).addComponent(jLabel1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(83, 83, 83).addComponent( jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(130, 130, 130) .addComponent(addCusBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(36, 36, 36) .addComponent(cardBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap(21, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap(28, Short.MAX_VALUE).addComponent(jLabel6) .addGap(18, 18, 18) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(43, 43, 43) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(31, 31, 31) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(addCusBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cardBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(58, 58, 58))); getAccessibleContext().setAccessibleName("pane1"); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened // TODO add your handling code here: }//GEN-LAST:event_formWindowOpened private void memberCmbTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memberCmbTxtActionPerformed // TODO add your handling code here: }//GEN-LAST:event_memberCmbTxtActionPerformed private void addCusBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCusBtnActionPerformed String fname = fNameTxt.getText(); String lname = lNameTxt.getText(); int choice = memberCmbTxt.getSelectedIndex(); String mtype = null; String dis = null; if (choice == 0) { mtype = "Platinam"; dis = "15"; } else if (choice == 1) { mtype = "Gold"; dis = "10"; } else if (choice == 2) { mtype = "Silver"; dis = "7"; } String cid = cusIdTxt.getText(); if (new CustomersUpdater().add(fname, lname, mtype, dis, cid)) { JOptionPane.showMessageDialog(null, "Successfuly Inserted"); //jTextField1.setText(""); //jTextField2.setText(""); //jTextField3.setText(""); } else { JOptionPane.showMessageDialog(null, "Not Saved"); } }//GEN-LAST:event_addCusBtnActionPerformed private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed }//GEN-LAST:event_formWindowClosed private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing }//GEN-LAST:event_formWindowClosing private void cardBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cardBtnActionPerformed String name = fNameTxt.getText() + " " + lNameTxt.getText(); String cid = cusIdTxt.getText(); Document document = new Document(); Rectangle test = new Rectangle(350, 200); BaseColor color = new BaseColor(63, 72, 204); test.setBackgroundColor(color); document.setPageSize(test); File theDir = new File("E:\\CustomerCard"); // if the directory does not exist, create it if (!theDir.exists()) { //System.out.println("creating directory: " + directoryName); boolean result = false; try { theDir.mkdir(); result = true; } catch (SecurityException se) { System.out.println(se.getMessage()); } } try { PdfWriter.getInstance(document, new FileOutputStream("E:\\CustomerCard\\card(" + cid + ").pdf")); document.open(); //Image image; //image = Image.getInstance("api.png"); //document.add(image); document.add(new Paragraph(" Customer Discount Card", FontFactory.getFont(FontFactory.TIMES_BOLD, 16, Font.BOLD, BaseColor.GRAY))); document.add(new Paragraph(" ")); document.add(new Paragraph(" Name : " + name, FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLACK))); document.add(new Paragraph(" ")); document.add(new Paragraph(" Card ID: " + cid, FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLACK))); document.close(); try { //Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+"E:\\CustomerCard\\card("+cid+").pdf"); Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:\\Users\\Vidu\\Desktop\\CustomerIDCard\\card(" + cid + ").pdf"); } catch (Exception ex) { Logger.getLogger(SellPage.class.getName()).log(Level.SEVERE, null, ex); } //document.add(new Paragraph(date,FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL,BaseColor.BLACK))); fNameTxt.setText(""); lNameTxt.setText(""); cusIdTxt.setText(""); } catch (Exception ex) { ex.printStackTrace(); } }//GEN-LAST:event_cardBtnActionPerformed /** * @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 ("Windows".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(AddCustomers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AddCustomers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AddCustomers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AddCustomers.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 AddCustomers().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addCusBtn; private javax.swing.JButton cardBtn; private javax.swing.JTextField cusIdTxt; private javax.swing.JTextField fNameTxt; 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.JPanel jPanel1; private javax.swing.JSeparator jSeparator1; private javax.swing.JTextField lNameTxt; private javax.swing.JComboBox<String> memberCmbTxt; // End of variables declaration//GEN-END:variables }