Guest User

ProfileForm

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