Advertisement
Guest User

RegisterForm

a guest
Jun 8th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.56 KB | None | 0 0
  1. import java.awt.FlowLayout;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.sql.SQLException;
  8. import java.util.Vector;
  9.  
  10. import javax.swing.ButtonGroup;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPanel;
  17. import javax.swing.JPasswordField;
  18. import javax.swing.JRadioButton;
  19. import javax.swing.JTextField;
  20. import javax.swing.table.DefaultTableModel;
  21.  
  22.  
  23. public class RegisterForm extends JFrame implements ActionListener{
  24.    
  25.     JPanel paneljudul = new JPanel(new FlowLayout());
  26.     JPanel panelbutton = new JPanel(new GridLayout(2,1));
  27.     JPanel paneltengah = new JPanel(new GridLayout(2, 1));
  28.     JPanel panelform = new JPanel(new GridLayout(8, 2));
  29.     JPanel panelgender = new JPanel(new GridLayout(1, 2));
  30.    
  31.     JLabel title = new JLabel("Register");
  32.     private JTextField txtid = new JTextField();
  33.     JTextField txtname = new JTextField();
  34.     JTextField txtuser = new JTextField();
  35.     JTextField txtemail = new JTextField();
  36.     JTextField txtphone = new JTextField();
  37.     JTextField txtaddress = new JTextField();
  38.     JPasswordField txtpass = new JPasswordField();
  39.     JRadioButton female = new JRadioButton("Female");
  40.     JRadioButton male = new JRadioButton("Male");
  41.     ButtonGroup bg = new ButtonGroup();
  42.     JButton insert = new JButton("Insert");
  43.     JButton cancel = new JButton("Cancel");
  44.    
  45.     Connect con = new Connect();
  46.    
  47.     public void AutoIDUser() {
  48.         try {
  49.             con.rs = con.st.executeQuery("select count(UserID) from MsUser");
  50.             con.rs.next();
  51.             int JumlahData = con.rs.getInt(1);
  52.             if (JumlahData == 0){
  53.                  txtid.setText("U001");  
  54.             }
  55.             else if (JumlahData < 9) {
  56.                 con.rs = con.st.executeQuery("select max(UserID) from MsUser");
  57.                 while (con.rs.next()){      
  58.                     String UserID = con.rs.getString(1);
  59.                     String AngkaUserID = UserID.substring(3,4);
  60.                     int UserIDBaru = Integer.parseInt(AngkaUserID)+1;
  61.                     txtid.setText(String.valueOf("U00"+UserIDBaru));
  62.                 }
  63.             }
  64.             else if (JumlahData < 99) {
  65.                 con.rs = con.st.executeQuery("select max(UserID) from MsUser");
  66.                 while (con.rs.next()){      
  67.                     String UserID = con.rs.getString(1);
  68.                     String AngkaUserID = UserID.substring(2,4);
  69.                     int UserIDBaru = Integer.parseInt(AngkaUserID)+1;
  70.                     txtid.setText(String.valueOf("U0"+UserIDBaru));
  71.                 }            
  72.             }
  73.             else if (JumlahData >= 99) {
  74.                 con.rs = con.st.executeQuery("select max(UserID) from MsUser");
  75.                 while (con.rs.next()){      
  76.                     String UserID = con.rs.getString(1);
  77.                     String AngkaUserID = UserID.substring(1,4);
  78.                     int UserIDBaru = Integer.parseInt(AngkaUserID)+1;
  79.                     txtid.setText(String.valueOf("U"+UserIDBaru));
  80.                 }
  81.             }
  82.         } catch (SQLException e) {
  83.             e.printStackTrace();
  84.         }
  85.     }
  86.    
  87.     public void setUI(){
  88.    
  89.         paneljudul.add(title);
  90.         add(paneljudul, "North");  
  91.        
  92.         paneltengah.add(panelform);
  93.         add(paneltengah, "Center");
  94.        
  95.         panelgender.add(male);
  96.         panelgender.add(female);
  97.         bg.add(male);
  98.         bg.add(female);
  99.          
  100.         panelform.add(new JLabel("User ID"));
  101.         panelform.add(txtid); txtid.setEnabled(false);
  102.         panelform.add(new JLabel("Username"));
  103.         panelform.add(txtuser);
  104.         panelform.add(new JLabel("Password"));
  105.         panelform.add(txtpass);
  106.         panelform.add(new JLabel("Name"));
  107.         panelform.add(txtname);
  108.         panelform.add(new JLabel("Email"));
  109.         panelform.add(txtemail);
  110.         panelform.add(new JLabel("Phone Number"));
  111.         panelform.add(txtphone);
  112.         panelform.add(new JLabel("Address"));
  113.         panelform.add(txtaddress);
  114.         panelform.add(new JLabel("Gender"));
  115.         panelform.add(panelgender);
  116.        
  117.         panelbutton.add(insert);
  118.         panelbutton.add(cancel);
  119.         add(panelbutton, "South");
  120.        
  121.          // INI DIGANTI
  122.        
  123.         //table.addMouseListener(this);
  124.         // SET ACTION LISTENER
  125.         insert.addActionListener(this);
  126.         cancel.addActionListener(this);
  127.         // END SET ACTION LISTENER
  128.        
  129.        
  130.     }
  131.    
  132.     public RegisterForm() {
  133.         setUI();
  134.         setSize(450, 500);
  135.         setLocationRelativeTo(null);
  136.         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  137.         setVisible(true);
  138.         AutoIDUser();
  139.     }
  140.  
  141.     @Override
  142.     public void actionPerformed(ActionEvent e) {
  143.         if(e.getSource() == insert){
  144.             String UserID = txtid.getText();
  145.             String UserFullName = txtname.getText();
  146.             String Username = txtuser.getText();
  147.             String UserEmail = txtemail.getText();
  148.             String UserPhone = txtphone.getText();
  149.             String UserAddress = txtaddress.getText();
  150.             String UserPass = String.valueOf(txtpass.getPassword());
  151.             String UserGender;
  152.             if(male.isSelected()==true){
  153.                 UserGender = "Male";
  154.             }
  155.             else{
  156.                 UserGender = "Female";
  157.             }
  158.             if(txtuser.getText().equals("")){
  159.                 JOptionPane.showMessageDialog(null, "USERNAME is blank");
  160.                 txtuser.requestFocus();
  161.             }else if(Username.length() > 15 || Username.length() < 5 ) {
  162.                 JOptionPane.showMessageDialog(null,"Username length must be between 5 and 15 characters.","Information",JOptionPane.INFORMATION_MESSAGE);
  163.                 txtuser.requestFocus(true);
  164.             }else if(txtemail.getText().equals("")){
  165.                 JOptionPane.showMessageDialog(null, "EMAIL is blank");
  166.                 txtemail.requestFocus();
  167.             }else if(txtname.getText().equals("")){
  168.                 JOptionPane.showMessageDialog(null, "NAME is blank");
  169.                 txtname.requestFocus();
  170.             }else if(txtpass.getPassword().equals("")){
  171.                 JOptionPane.showMessageDialog(null, "PASSWORD is blank");
  172.                 txtpass.requestFocus();
  173.             }else if(UserPass.length() > 15 || UserPass.length() < 5 ) {
  174.                 JOptionPane.showMessageDialog(null,"Password length must be between 5 and 15 characters.","Information",JOptionPane.INFORMATION_MESSAGE);
  175.                 txtpass.requestFocus(true);
  176.             }else if(txtaddress.getText().equals("")){
  177.                 JOptionPane.showMessageDialog(null, "ADDRESS is blank");
  178.                 txtaddress.requestFocus();
  179.             }else if(txtphone.getText().equals("")){
  180.                 JOptionPane.showMessageDialog(null, "PHONE is blank");
  181.                 txtphone.requestFocus();
  182.             }else{
  183.                 String Role = "User";
  184.                 con.executeInsertUser(UserID, Username, UserPass, UserFullName, UserEmail, UserPhone, UserAddress, UserGender, Role);
  185.                 JOptionPane.showMessageDialog(null, "Input Success");
  186.                 new MainForm();
  187.                 this.dispose();
  188.             }
  189.         }else if(e.getSource() == cancel){
  190.             new MenuForm();
  191.             this.dispose();
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement