Guest User

ProfileForm

a guest
Jun 8th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.81 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.sql.SQLException;
  3.  
  4. import javax.swing.ButtonGroup;
  5. import javax.swing.JDialog;
  6. import javax.swing.JInternalFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JLabel;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JTextField;
  12. import javax.swing.JPasswordField;
  13. import javax.swing.JRadioButton;
  14. import javax.swing.JButton;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.ActionEvent;
  17. import java.awt.event.KeyAdapter;
  18. import java.awt.event.KeyEvent;
  19. import java.awt.event.WindowAdapter;
  20. import java.awt.event.WindowEvent;
  21. import javax.swing.JTextArea;
  22. import javax.swing.event.InternalFrameAdapter;
  23. import javax.swing.event.InternalFrameEvent;
  24.  
  25. public class ProfileForm extends JInternalFrame {
  26. Connect con = new Connect();
  27. JRadioButton RadioButtonMale = new JRadioButton("Male");
  28. JRadioButton RadioButtonFemale = new JRadioButton("Female");
  29. ButtonGroup ButtonGroup = new ButtonGroup();
  30. JTextArea TextBoxAddress = new JTextArea();
  31. JButton ButtonEdit = new JButton("Edit");
  32. JButton ButtonSave = new JButton("Save");
  33. private final JPanel contentPanel = new JPanel();
  34. private JTextField TextBoxUsername;
  35. private JPasswordField TextBoxPassword;
  36. private JTextField TextBoxName;
  37. private JTextField TextBoxEmail;
  38. private JTextField TextBoxPhoneNumber;
  39.  
  40. public void DataUser() throws SQLException {
  41. con.rs = con.st.executeQuery("select * from MsUser where UserID='" + LoginForm.UserID + "'");
  42. if (con.rs.next()) {
  43. String Username = con.rs.getString(2);
  44. String Password = con.rs.getString(3);
  45. String Name = con.rs.getString(4);
  46. String Email = con.rs.getString(5);
  47. String PhoneNumber = con.rs.getString(6);
  48. String Address = con.rs.getString(7);
  49. String Gender = con.rs.getString(8);
  50. TextBoxUsername.setText(Username);
  51. TextBoxPassword.setText(Password);
  52. TextBoxName.setText(Name);
  53. TextBoxEmail.setText(Email);
  54. TextBoxPhoneNumber.setText(PhoneNumber);
  55. TextBoxAddress.setText(Address);
  56. if (Gender.equals("Male")) {
  57. RadioButtonMale.setSelected(true);
  58. }
  59. else if (Gender.equals("Female")) {
  60. RadioButtonFemale.setSelected(true);
  61. }
  62. }
  63. }
  64.  
  65.  
  66.  
  67. public void ProsesSimpan() throws SQLException {
  68. String UsernameLama = LoginForm.UsernameLama;
  69. String Username = TextBoxUsername.getText();
  70. String Password = String.valueOf(TextBoxPassword.getPassword());
  71. String Name = TextBoxName.getText();
  72. String Email = TextBoxEmail.getText();
  73. String PhoneNumber = TextBoxPhoneNumber.getText();
  74. String Address = TextBoxAddress.getText();
  75. String Gender = "";
  76. if (Username.equals("")) {
  77. JOptionPane.showMessageDialog(null,"Please fill Username.","Information",JOptionPane.INFORMATION_MESSAGE);
  78. TextBoxUsername.requestFocus(true);
  79. }
  80. else if(Username.length() > 15 || Username.length() < 5 ) {
  81. JOptionPane.showMessageDialog(null,"Username length must be between 5 and 15 characters.","Information",JOptionPane.INFORMATION_MESSAGE);
  82. TextBoxUsername.requestFocus(true);
  83. }
  84. else if (Password.equals("")) {
  85. JOptionPane.showMessageDialog(null,"Please fill Password.","Information",JOptionPane.INFORMATION_MESSAGE);
  86. TextBoxPassword.requestFocus(true);
  87. }
  88. else if(Password.length() > 15 || Password.length() < 5 ) {
  89. JOptionPane.showMessageDialog(null,"Password length must be between 5 and 15 characters.","Information",JOptionPane.INFORMATION_MESSAGE);
  90. TextBoxPassword.requestFocus(true);
  91. }
  92. else if (Name.equals("")) {
  93. JOptionPane.showMessageDialog(null,"Please fill Name.","Information",JOptionPane.INFORMATION_MESSAGE);
  94. TextBoxName.requestFocus(true);
  95. }
  96. else if (Email.equals("")) {
  97. JOptionPane.showMessageDialog(null,"Please fill Email.","Information",JOptionPane.INFORMATION_MESSAGE);
  98. TextBoxEmail.requestFocus(true);
  99. }
  100. else if (PhoneNumber.equals("")) {
  101. JOptionPane.showMessageDialog(null,"Please fill Phone Number.","Information",JOptionPane.INFORMATION_MESSAGE);
  102. TextBoxPhoneNumber.requestFocus(true);
  103. }
  104. else if(PhoneNumber.length() > 12 || PhoneNumber.length() < 10 ) {
  105. JOptionPane.showMessageDialog(null,"Phone Number length must be between 10 and 12 characters.","Information",JOptionPane.INFORMATION_MESSAGE);
  106. TextBoxPhoneNumber.requestFocus(true);
  107. }
  108. else if (Address.equals("")) {
  109. JOptionPane.showMessageDialog(null,"Please fill Address.","Information",JOptionPane.INFORMATION_MESSAGE);
  110. TextBoxAddress.requestFocus(true);
  111. }
  112. else if(Address.length() > 30 || Address.length() < 6 ) {
  113. JOptionPane.showMessageDialog(null,"Address length must be between 6 and 30 characters.","Information",JOptionPane.INFORMATION_MESSAGE);
  114. TextBoxAddress.requestFocus(true);
  115. }
  116. else {
  117. if (RadioButtonMale.isSelected()==true) {
  118. if (Username.equals(UsernameLama)) {
  119. Gender = "Male";
  120. con.st.executeUpdate("update MsUser set Username='" + Username + "',UserPass='" + Password + "',UserFullName='" + Name + "',UserEmail='" + Email + "',UserPhone='" + PhoneNumber + "',UserAddress='" + Address + "',UserGender='" + Gender + "' where UserID ='" + LoginForm.UserID + "'");
  121. JOptionPane.showMessageDialog(null,"Successful edit data.","Information",JOptionPane.INFORMATION_MESSAGE);
  122. DisableTextBox();
  123. }
  124. else {
  125. con.rs = con.st.executeQuery("select * from MsUser where Username='" + Username + "'");
  126. if (con.rs.next()){
  127. JOptionPane.showMessageDialog(null,"Username have been used.","Information",JOptionPane.INFORMATION_MESSAGE);
  128. TextBoxUsername.requestFocus(true);
  129. }
  130. else {
  131. Gender = "Male";
  132. con.st.executeUpdate("update MsUser set Username='" + Username + "',UserPass='" + Password + "',UserFullName='" + Name + "',UserEmail='" + Email + "',UserPhone='" + PhoneNumber + "',UserAddress='" + Address + "',UserGender='" + Gender + "' where UserID ='" + LoginForm.UserID + "'");
  133. JOptionPane.showMessageDialog(null,"Successful edit data.","Information",JOptionPane.INFORMATION_MESSAGE);
  134. DisableTextBox();
  135. }
  136. }
  137. }
  138. else if (RadioButtonFemale.isSelected()==true) {
  139. if (Username.equals(UsernameLama)) {
  140. Gender = "Female";
  141. con.st.executeUpdate("update MsUser set Username='" + Username + "',UserPass='" + Password + "',UserFullName='" + Name + "',UserEmail='" + Email + "',UserPhone='" + PhoneNumber + "',UserAddress='" + Address + "',UserGender='" + Gender + "' where UserID ='" + LoginForm.UserID + "'");
  142. JOptionPane.showMessageDialog(null,"Successful edit data.","Information",JOptionPane.INFORMATION_MESSAGE);
  143. DisableTextBox();
  144. }
  145. else {
  146. con.rs = con.st.executeQuery("select * from MsUser where Username='" + Username + "'");
  147. if (con.rs.next()){
  148. JOptionPane.showMessageDialog(null,"Username have been used.","Information",JOptionPane.INFORMATION_MESSAGE);
  149. TextBoxUsername.requestFocus(true);
  150. }
  151. else {
  152. Gender = "Female";
  153. con.st.executeUpdate("update MsUser set Username='" + Username + "',UserPass='" + Password + "',UserFullName='" + Name + "',UserEmail='" + Email + "',UserPhone='" + PhoneNumber + "',UserAddress='" + Address + "',UserGender='" + Gender + "' where UserID ='" + LoginForm.UserID + "'");
  154. JOptionPane.showMessageDialog(null,"Successful edit data.","Information",JOptionPane.INFORMATION_MESSAGE);
  155. DisableTextBox();
  156. }
  157. }
  158. }
  159. else {
  160. JOptionPane.showMessageDialog(null,"Please fill Gender.","Information",JOptionPane.INFORMATION_MESSAGE);
  161. }
  162. }
  163. }
  164.  
  165. public void DisableTextBox() {
  166. TextBoxUsername.setEnabled(false);
  167. TextBoxPassword.setEnabled(false);
  168. TextBoxName.setEnabled(false);
  169. TextBoxEmail.setEnabled(false);
  170. TextBoxPhoneNumber.setEnabled(false);
  171. TextBoxAddress.setEnabled(false);
  172. RadioButtonMale.setEnabled(false);
  173. RadioButtonFemale.setEnabled(false);
  174. ButtonSave.setEnabled(false);
  175. ButtonEdit.setEnabled(true);
  176. }
  177.  
  178. public void EnableTextBox() {
  179. TextBoxUsername.setEnabled(true);
  180. TextBoxPassword.setEnabled(true);
  181. TextBoxName.setEnabled(true);
  182. TextBoxEmail.setEnabled(true);
  183. TextBoxPhoneNumber.setEnabled(true);
  184. TextBoxAddress.setEnabled(true);
  185. RadioButtonMale.setEnabled(true);
  186. RadioButtonFemale.setEnabled(true);
  187. ButtonSave.setEnabled(true);
  188. ButtonEdit.setEnabled(false);
  189. TextBoxUsername.requestFocus(true);
  190. }
  191.  
  192. public ProfileForm() throws SQLException {
  193. setClosable(true);
  194. setTitle("Aplikasi Toko Pakaian - My Profile");
  195. setResizable(false);
  196. setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  197. setBounds(100, 100, 360, 300);
  198. getContentPane().setLayout(new BorderLayout());
  199. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  200. getContentPane().add(contentPanel, BorderLayout.CENTER);
  201. contentPanel.setLayout(null);
  202.  
  203. JLabel label = new JLabel("Username :");
  204. label.setBounds(44, 40, 72, 14);
  205. contentPanel.add(label);
  206.  
  207. TextBoxUsername = new JTextField();
  208. TextBoxUsername.addKeyListener(new KeyAdapter() {
  209. public void keyTyped(KeyEvent evt) {
  210. RegisterForm.ValidasiUsername(evt);
  211. }
  212. });
  213. TextBoxUsername.setColumns(10);
  214. TextBoxUsername.setBounds(169, 36, 141, 20);
  215. contentPanel.add(TextBoxUsername);
  216.  
  217. JLabel label_1 = new JLabel("Password :");
  218. label_1.setBounds(44, 65, 72, 14);
  219. contentPanel.add(label_1);
  220.  
  221. TextBoxPassword = new JPasswordField();
  222. TextBoxPassword.setBounds(169, 62, 141, 20);
  223. contentPanel.add(TextBoxPassword);
  224.  
  225. JLabel label_2 = new JLabel("Name :");
  226. label_2.setBounds(44, 90, 72, 14);
  227. contentPanel.add(label_2);
  228.  
  229. TextBoxName = new JTextField();
  230. TextBoxName.addKeyListener(new KeyAdapter() {
  231. public void keyTyped(KeyEvent evt) {
  232. RegisterForm.ValidasiName(evt);
  233. }
  234. });
  235. TextBoxName.setColumns(10);
  236. TextBoxName.setBounds(169, 86, 141, 20);
  237. contentPanel.add(TextBoxName);
  238.  
  239. JLabel label_3 = new JLabel("Email :");
  240. label_3.setBounds(44, 115, 72, 14);
  241. contentPanel.add(label_3);
  242.  
  243. TextBoxEmail = new JTextField();
  244. TextBoxEmail.setColumns(10);
  245. TextBoxEmail.setBounds(169, 111, 141, 20);
  246. contentPanel.add(TextBoxEmail);
  247.  
  248. JLabel label_4 = new JLabel("Phone Number :");
  249. label_4.setBounds(44, 140, 97, 14);
  250. contentPanel.add(label_4);
  251.  
  252. TextBoxPhoneNumber = new JTextField();
  253. TextBoxPhoneNumber.addKeyListener(new KeyAdapter() {
  254. public void keyTyped(KeyEvent evt) {
  255. RegisterForm.ValidasiPhoneNumber(evt);
  256. }
  257. });
  258. TextBoxPhoneNumber.setColumns(10);
  259. TextBoxPhoneNumber.setBounds(169, 136, 141, 20);
  260. contentPanel.add(TextBoxPhoneNumber);
  261.  
  262. JLabel label_5 = new JLabel("Address :");
  263. label_5.setBounds(44, 165, 97, 14);
  264. contentPanel.add(label_5);
  265.  
  266. JLabel label_6 = new JLabel("Gender :");
  267. label_6.setBounds(44, 190, 97, 14);
  268. contentPanel.add(label_6);
  269.  
  270. RadioButtonMale.setBounds(169, 185, 55, 23);
  271. contentPanel.add(RadioButtonMale);
  272.  
  273. RadioButtonFemale.setBounds(238, 185, 72, 23);
  274. contentPanel.add(RadioButtonFemale);
  275.  
  276. ButtonGroup.add(RadioButtonMale);
  277. ButtonGroup.add(RadioButtonFemale);
  278.  
  279. ButtonEdit.addActionListener(new ActionListener() {
  280. public void actionPerformed(ActionEvent e) {
  281. EnableTextBox();
  282. }
  283. });
  284.  
  285. ButtonEdit.setBounds(76, 225, 89, 23);
  286. contentPanel.add(ButtonEdit);
  287.  
  288. ButtonSave.setBounds(175, 225, 89, 23);
  289. ButtonSave.addActionListener(new ActionListener() {
  290. public void actionPerformed(ActionEvent e) {
  291. try {
  292. ProsesSimpan();
  293. } catch (SQLException e1) {
  294. e1.printStackTrace();
  295. }
  296. }
  297. });
  298. contentPanel.add(ButtonSave);
  299.  
  300. TextBoxAddress.setBounds(169, 160, 141, 22);
  301. contentPanel.add(TextBoxAddress);
  302.  
  303. DisableTextBox();
  304. DataUser();
  305. }
  306.  
  307. public static void main(String[] args) throws SQLException {
  308. ProfileForm dialog = new ProfileForm();
  309. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  310. dialog.setVisible(true);
  311. }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment