Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. package hashmap;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Font;
  7. import java.awt.GridBagConstraints;
  8. import java.awt.GridBagLayout;
  9. import java.awt.Insets;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.util.HashMap;
  13.  
  14. import javax.swing.JButton;
  15. import javax.swing.JComboBox;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JTextField;
  20.  
  21. public class UpdateForm extends JFrame implements ActionListener {
  22.  
  23. private JLabel lblTitle, lblStudentID, lblForename, lblSurname, lblSubject, lblResult, lblMessage;
  24. private JTextField txtStudentID, txtForename, txtSurname, txtResult;
  25. private JComboBox cmbSubject;
  26. private JButton btnFindStudent, btnUpdate, btnExit;
  27. private Container cn;
  28.  
  29. private static HashMap<String, Student> privateMap;
  30. static Student foundItem;
  31.  
  32. public UpdateForm(HashMap localMap)
  33. {
  34. privateMap = localMap;
  35.  
  36. cn = getContentPane();
  37. cn.setLayout(new GridBagLayout());
  38.  
  39. lblTitle = new JLabel("Student Results", JLabel.CENTER);
  40. lblTitle.setFont(new Font("SansSerif", Font.BOLD,17));
  41. lblTitle.setForeground(Color.decode("#00000"));
  42.  
  43. lblStudentID = new JLabel("Student ID", JLabel.RIGHT);
  44. lblStudentID.setFont(new Font("SansSerif", Font.BOLD,17));
  45. lblStudentID.setForeground(Color.decode("#00000"));
  46.  
  47. lblForename = new JLabel("Forename", JLabel.RIGHT);
  48. lblForename.setFont(new Font("SansSerif", Font.BOLD,17));
  49. lblForename.setForeground(Color.decode("#00000"));
  50.  
  51. lblSurname = new JLabel("Surname", JLabel.RIGHT);
  52. lblSurname.setFont(new Font("SansSerif", Font.BOLD,17));
  53. lblSurname.setForeground(Color.decode("#00000"));
  54.  
  55. lblSubject = new JLabel("Subject", JLabel.RIGHT);
  56. lblSubject.setFont(new Font("SansSerif", Font.BOLD,17));
  57. lblSubject.setForeground(Color.decode("#00000"));
  58.  
  59. lblResult = new JLabel("Result", JLabel.RIGHT);
  60. lblResult.setFont(new Font("SansSerif", Font.BOLD,17));
  61. lblResult.setForeground(Color.decode("#00000"));
  62.  
  63. lblMessage = new JLabel("Message will appear...", JLabel.RIGHT);
  64. lblMessage.setFont(new Font("SansSerif", Font.BOLD,17));
  65. lblMessage.setForeground(Color.decode("#00000"));
  66.  
  67. txtStudentID= new JTextField(10);
  68. txtStudentID.setFont(new Font("SansSerif", Font.BOLD,14));
  69.  
  70. txtForename= new JTextField(10);
  71. txtForename.setFont(new Font("SansSerif", Font.BOLD,14));
  72.  
  73. txtSurname= new JTextField(10);
  74. txtSurname.setFont(new Font("SansSerif", Font.BOLD,14));
  75.  
  76. String[] sub = {"Computing", "Math", "English", "History"};
  77.  
  78. cmbSubject = new JComboBox(sub);
  79. cmbSubject.setFont(new Font("SansSerif", Font.PLAIN,16));
  80. cmbSubject.addActionListener(this);
  81.  
  82. txtResult= new JTextField(10);
  83. txtResult.setFont(new Font("SansSerif", Font.BOLD,14));
  84.  
  85. btnFindStudent = new JButton ("Find Student");
  86. btnFindStudent.setFont(new Font("SansSerif", Font.BOLD,17));
  87. btnFindStudent.addActionListener(this);
  88.  
  89. btnUpdate = new JButton ("Update");
  90. btnUpdate.setFont(new Font("SansSerif", Font.BOLD,17));
  91. btnUpdate.addActionListener(this);
  92.  
  93. btnExit = new JButton ("Exit");
  94. btnExit.setFont(new Font("SansSerif", Font.BOLD,17));
  95. btnExit.addActionListener(this);
  96.  
  97. getContentPane().setBackground(Color.decode("#EEEEEE"));
  98.  
  99. //disable
  100. txtForename.setEnabled(false);
  101. txtSurname.setEnabled(false);
  102. cmbSubject.setEnabled(false);
  103. txtResult.setEnabled(false);
  104. btnUpdate.setEnabled(false);
  105.  
  106. addComp(lblTitle, 1, 0, 1, 1, 0, 0);
  107. addComp(lblStudentID, 0, 1, 1, 1, 0, 0);
  108. addComp(lblForename, 0, 2, 1, 1, 0, 0);
  109. addComp(lblSurname, 0, 3, 1, 1, 0, 0);
  110. addComp(lblSubject, 0, 4, 1, 1, 0, 0);
  111. addComp(lblResult, 0, 5, 1, 1, 0, 0);
  112. addComp(lblMessage, 0, 6, 1, 1, 0, 0);
  113. addComp(txtStudentID, 1, 1, 2, 1, 0, 0);
  114. addComp(txtForename, 1, 2, 2, 1, 0, 0);
  115. addComp(txtSurname, 1, 3, 2, 1, 0, 0);
  116. addComp(cmbSubject, 1, 4, 1, 1, 0, 0);
  117. addComp(txtResult, 1, 5, 2, 1, 0, 0);
  118. addComp(btnFindStudent, 0, 7, 2, 1, 0, 0);
  119. addComp(btnUpdate, 2, 7, 1, 1, 0, 0);
  120. addComp(btnExit, 3, 7, 1, 1, 0, 0);
  121. }
  122.  
  123. public static void main(String[] args) {
  124. // TODO Auto-generated method stub
  125.  
  126. UpdateForm u = new UpdateForm(privateMap);
  127. u.setTitle("Update Student");
  128. u.setSize(600, 500);
  129. u.setLocation(900, 500);
  130. u.setVisible(true);
  131. u.setDefaultCloseOperation(EXIT_ON_CLOSE);
  132. }
  133.  
  134. private void addComp(Component c,
  135. int column, int row, int numColumns,
  136. int numRows, int weightx, int weighty)
  137. {
  138. GridBagConstraints gbConstraints = new GridBagConstraints();
  139. //set parameters
  140.  
  141. gbConstraints.fill = GridBagConstraints.BOTH;
  142. gbConstraints.anchor = GridBagConstraints.CENTER;
  143. gbConstraints.gridx = column;
  144. gbConstraints.gridy = row;
  145. gbConstraints.gridwidth = numColumns;
  146. gbConstraints.gridheight = numRows;
  147. gbConstraints.weightx = weightx;
  148. gbConstraints.weighty = weighty;
  149. gbConstraints.insets = new Insets(5,5,5,5);
  150.  
  151. //add to container
  152. cn.add(c, gbConstraints);
  153. }
  154.  
  155. @Override
  156. public void actionPerformed(ActionEvent e) {
  157. // TODO Auto-generated method stub
  158.  
  159. if(e.getSource() == btnExit)
  160. {
  161. setVisible(false);
  162. }
  163.  
  164. if(e.getSource() == btnFindStudent)
  165. {
  166. String findStudentID = "";
  167. findStudentID = txtStudentID.getText().trim();
  168. foundItem = privateMap.get(findStudentID);
  169.  
  170. if(foundItem != null)
  171. {
  172. txtStudentID.setEnabled(false);
  173. btnUpdate.setEnabled(true);
  174. txtForename.setText(foundItem.getForename());
  175. txtSurname.setText(foundItem.getSurname());
  176. cmbSubject.setSelectedItem(foundItem.getSubject());
  177. String r = Integer.toString(foundItem.getResult());
  178. txtResult.setText(r);
  179.  
  180. txtForename.setEnabled(true);
  181. txtSurname.setEnabled(true);
  182. cmbSubject.setEnabled(true);
  183. txtResult.setEnabled(true);
  184. btnUpdate.setEnabled(true);
  185.  
  186. }
  187. }
  188.  
  189. if(e.getSource() == btnUpdate)
  190. {
  191. foundItem.setForename(txtForename.getText());
  192. foundItem.setSurname(txtSurname.getText());
  193. foundItem.setSubject(cmbSubject.getSelectedItem().toString());
  194. int sR = Integer.parseInt(txtResult.getText());
  195. foundItem.setResult(sR);
  196. }
  197.  
  198. }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement