Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.sql.SQLException;
- import java.util.Vector;
- import javax.swing.ButtonGroup;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JRadioButton;
- import javax.swing.JTextField;
- import javax.swing.table.DefaultTableModel;
- public class RegisterForm extends JFrame implements ActionListener{
- JPanel paneljudul = new JPanel(new FlowLayout());
- JPanel panelbutton = new JPanel(new GridLayout(2,1));
- JPanel paneltengah = new JPanel(new GridLayout(2, 1));
- JPanel panelform = new JPanel(new GridLayout(8, 2));
- JPanel panelgender = new JPanel(new GridLayout(1, 2));
- JLabel title = new JLabel("Register");
- private JTextField txtid = new JTextField();
- JTextField txtname = new JTextField();
- JTextField txtuser = new JTextField();
- JTextField txtemail = new JTextField();
- JTextField txtphone = new JTextField();
- JTextField txtaddress = new JTextField();
- JPasswordField txtpass = new JPasswordField();
- JRadioButton female = new JRadioButton("Female");
- JRadioButton male = new JRadioButton("Male");
- ButtonGroup bg = new ButtonGroup();
- JButton insert = new JButton("Insert");
- JButton cancel = new JButton("Cancel");
- Connect con = new Connect();
- public void AutoIDUser() {
- try {
- con.rs = con.st.executeQuery("select count(UserID) from MsUser");
- con.rs.next();
- int JumlahData = con.rs.getInt(1);
- if (JumlahData == 0){
- txtid.setText("U001");
- }
- else if (JumlahData < 9) {
- con.rs = con.st.executeQuery("select max(UserID) from MsUser");
- while (con.rs.next()){
- String UserID = con.rs.getString(1);
- String AngkaUserID = UserID.substring(3,4);
- int UserIDBaru = Integer.parseInt(AngkaUserID)+1;
- txtid.setText(String.valueOf("U00"+UserIDBaru));
- }
- }
- else if (JumlahData < 99) {
- con.rs = con.st.executeQuery("select max(UserID) from MsUser");
- while (con.rs.next()){
- String UserID = con.rs.getString(1);
- String AngkaUserID = UserID.substring(2,4);
- int UserIDBaru = Integer.parseInt(AngkaUserID)+1;
- txtid.setText(String.valueOf("U0"+UserIDBaru));
- }
- }
- else if (JumlahData >= 99) {
- con.rs = con.st.executeQuery("select max(UserID) from MsUser");
- while (con.rs.next()){
- String UserID = con.rs.getString(1);
- String AngkaUserID = UserID.substring(1,4);
- int UserIDBaru = Integer.parseInt(AngkaUserID)+1;
- txtid.setText(String.valueOf("U"+UserIDBaru));
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void setUI(){
- paneljudul.add(title);
- add(paneljudul, "North");
- paneltengah.add(panelform);
- add(paneltengah, "Center");
- panelgender.add(male);
- panelgender.add(female);
- bg.add(male);
- bg.add(female);
- panelform.add(new JLabel("User ID"));
- panelform.add(txtid); txtid.setEnabled(false);
- panelform.add(new JLabel("Username"));
- panelform.add(txtuser);
- panelform.add(new JLabel("Password"));
- panelform.add(txtpass);
- panelform.add(new JLabel("Name"));
- panelform.add(txtname);
- panelform.add(new JLabel("Email"));
- panelform.add(txtemail);
- panelform.add(new JLabel("Phone Number"));
- panelform.add(txtphone);
- panelform.add(new JLabel("Address"));
- panelform.add(txtaddress);
- panelform.add(new JLabel("Gender"));
- panelform.add(panelgender);
- panelbutton.add(insert);
- panelbutton.add(cancel);
- add(panelbutton, "South");
- // INI DIGANTI
- //table.addMouseListener(this);
- // SET ACTION LISTENER
- insert.addActionListener(this);
- cancel.addActionListener(this);
- // END SET ACTION LISTENER
- }
- public RegisterForm() {
- setUI();
- setSize(450, 500);
- setLocationRelativeTo(null);
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setVisible(true);
- AutoIDUser();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == insert){
- String UserID = txtid.getText();
- String UserFullName = txtname.getText();
- String Username = txtuser.getText();
- String UserEmail = txtemail.getText();
- String UserPhone = txtphone.getText();
- String UserAddress = txtaddress.getText();
- String UserPass = String.valueOf(txtpass.getPassword());
- String UserGender;
- if(male.isSelected()==true){
- UserGender = "Male";
- }
- else{
- UserGender = "Female";
- }
- if(txtuser.getText().equals("")){
- JOptionPane.showMessageDialog(null, "USERNAME is blank");
- txtuser.requestFocus();
- }else if(Username.length() > 15 || Username.length() < 5 ) {
- JOptionPane.showMessageDialog(null,"Username length must be between 5 and 15 characters.","Information",JOptionPane.INFORMATION_MESSAGE);
- txtuser.requestFocus(true);
- }else if(txtemail.getText().equals("")){
- JOptionPane.showMessageDialog(null, "EMAIL is blank");
- txtemail.requestFocus();
- }else if(txtname.getText().equals("")){
- JOptionPane.showMessageDialog(null, "NAME is blank");
- txtname.requestFocus();
- }else if(txtpass.getPassword().equals("")){
- JOptionPane.showMessageDialog(null, "PASSWORD is blank");
- txtpass.requestFocus();
- }else if(UserPass.length() > 15 || UserPass.length() < 5 ) {
- JOptionPane.showMessageDialog(null,"Password length must be between 5 and 15 characters.","Information",JOptionPane.INFORMATION_MESSAGE);
- txtpass.requestFocus(true);
- }else if(txtaddress.getText().equals("")){
- JOptionPane.showMessageDialog(null, "ADDRESS is blank");
- txtaddress.requestFocus();
- }else if(txtphone.getText().equals("")){
- JOptionPane.showMessageDialog(null, "PHONE is blank");
- txtphone.requestFocus();
- }else{
- String Role = "User";
- con.executeInsertUser(UserID, Username, UserPass, UserFullName, UserEmail, UserPhone, UserAddress, UserGender, Role);
- JOptionPane.showMessageDialog(null, "Input Success");
- new MainForm();
- this.dispose();
- }
- }else if(e.getSource() == cancel){
- new MenuForm();
- this.dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement