Advertisement
Guest User

full

a guest
Oct 3rd, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.event.ActionEvent.*;
  4. import java.util.Date.*;
  5. import javax.swing.*;
  6. import javax.swing.ImageIcon.*;
  7. import javax.swing.table.*;
  8. import java.sql.*;
  9.  
  10. public class labKeys extends JFrame implements ActionListener {
  11.  
  12. Container con;
  13. JLabel lblTT, lblTR, lblName, lblPhone, lblLab, lblDep, lblGTT, lblGTR, lblGName, lblGPhone, lblGLab, lblGDep;
  14. JTextField txtTT, txtTR, txtName, txtPhone;
  15. JComboBox cboLab;
  16. JRadioButton rbDJTMK, rbDJPA;
  17. ButtonGroup bGroup;
  18. JButton btnSave, btnDisplay, btnClose;
  19. ImageIcon icon;
  20.  
  21. Connection conn = null;
  22. Statement stmt = null;
  23.  
  24. public labKeys() {
  25.  
  26. setTitle("Tracking Lab Keys");
  27. setVisible(true);
  28. setLayout(new FlowLayout());
  29. setSize(400, 500);
  30. setBackground(Color.LIGHT_GRAY);
  31. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.  
  33. con = getContentPane();
  34.  
  35. icon = new ImageIcon("icon.PNG");
  36. Image Image = icon.getImage();
  37. this.setIconImage(Image);
  38.  
  39. lblTT = new JLabel("Time Taken");
  40. lblTR = new JLabel("Time Returned");
  41. lblName = new JLabel("Lecturer Name");
  42. lblPhone = new JLabel("Phone Number");
  43. lblLab = new JLabel("Laboratory");
  44. lblDep = new JLabel("Department");
  45.  
  46. JLabel lblISTime = new JLabel();
  47. lblISTime.setIcon(new ImageIcon("stime.PNG"));
  48. JLabel lblIETime = new JLabel();
  49. lblIETime.setIcon(new ImageIcon("etime.PNG"));
  50. JLabel lblIName = new JLabel();
  51. lblIName.setIcon(new ImageIcon("name.PNG"));
  52. JLabel lblIPhone = new JLabel();
  53. lblIPhone.setIcon(new ImageIcon("phone.PNG"));
  54. JLabel lblILab = new JLabel();
  55. lblILab.setIcon(new ImageIcon("lab.PNG"));
  56. JLabel lblIDep = new JLabel();
  57. lblIDep.setIcon(new ImageIcon("dep.PNG"));
  58.  
  59. txtTT = new JTextField(10);
  60. txtTR = new JTextField(10);
  61. txtName = new JTextField(15);
  62. txtPhone = new JTextField(15);
  63.  
  64. String[] chooseLab = {"MHM", "MIT1", "MIT2", "MIT3", "MPA", "MKR", "MSKD"};
  65. cboLab = new JComboBox(chooseLab);
  66. cboLab.setSelectedIndex(0);
  67.  
  68. rbDJTMK = new JRadioButton("JTMK");
  69. rbDJPA = new JRadioButton("JPA");
  70. bGroup = new ButtonGroup();
  71. bGroup.add(rbDJTMK);
  72. bGroup.add(rbDJPA);
  73.  
  74. btnSave = new JButton("Save");
  75. btnDisplay = new JButton("Display");
  76. btnClose = new JButton("Close");
  77.  
  78. lblGTT = new JLabel("");
  79. lblGTR = new JLabel("");
  80. lblGName = new JLabel("");
  81. lblGPhone = new JLabel("");
  82. lblGLab = new JLabel("");
  83. lblGDep = new JLabel("");
  84.  
  85. lblGTT.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  86. lblGTR.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  87. lblGName.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  88. lblGPhone.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  89. lblGLab.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  90. lblGDep.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  91.  
  92. add(new JLabel(new ImageIcon("key.PNG")));
  93. con.add(lblISTime);
  94. con.add(lblTT);
  95. con.add(txtTT);
  96. con.add(lblIETime);
  97. con.add(lblTR);
  98. con.add(txtTR);
  99. con.add(lblIName);
  100. con.add(lblName);
  101. con.add(txtName);
  102. con.add(lblIPhone);
  103. con.add(lblPhone);
  104. con.add(txtPhone);
  105. con.add(lblILab);
  106. con.add(lblLab);
  107. con.add(cboLab);
  108. con.add(lblIDep);
  109. con.add(lblDep);
  110. con.add(rbDJTMK);
  111. con.add(rbDJPA);
  112. con.add(btnSave);
  113. con.add(btnDisplay);
  114. con.add(btnClose);
  115. con.add(lblGTT);
  116. con.add(lblGTR);
  117. con.add(lblGName);
  118. con.add(lblGPhone);
  119. con.add(lblGLab);
  120. con.add(lblGDep);
  121. pack();
  122.  
  123. btnSave.addActionListener(this);
  124. btnDisplay.addActionListener(this);
  125. btnClose.addActionListener(this);
  126. }
  127.  
  128. public static void main(String[] args) {
  129. labKeys lKeys = new labKeys();
  130. }
  131.  
  132. @Override
  133. public void actionPerformed(ActionEvent e) {
  134.  
  135. if (e.getSource() == btnSave) {
  136.  
  137. try {
  138. int gTT = Integer.parseInt(txtTT.getText());
  139. int gTR = Integer.parseInt(txtTR.getText());
  140. String gName = txtName.getText();
  141. int gPhone = Integer.parseInt(txtPhone.getText());
  142. String gLab = cboLab.getSelectedItem().toString();
  143.  
  144. String gRB = "";
  145. if (rbDJTMK.isSelected()) {
  146. gRB = rbDJTMK.getActionCommand();
  147. } else if (rbDJPA.isSelected()) {
  148. gRB = rbDJPA.getActionCommand();
  149. }
  150.  
  151. JOptionPane.showMessageDialog(null, "Time taken : " + gTT + "\nTime returned : " + gTR
  152. + "\nLecturer name : " + gName + "\nPhone number : " + gPhone
  153. + "\nLaboratory : " + gLab + "\nDepartment : " + gRB);
  154.  
  155. Class.forName("com.mysql.jdbc.Driver");
  156. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/semester4", "root", "");
  157. stmt = conn.createStatement();
  158. stmt.executeUpdate("insert into laboratory values(" + gTT + "," + gTR + ",'" + gName + "'," + gPhone + ",'" + gLab + "','" + gRB + "')");
  159.  
  160. } catch (Exception exc) {
  161. System.err.println("Got an exception! ");
  162. System.err.println(exc.getMessage());
  163. }
  164.  
  165. }
  166.  
  167. else if (e.getSource() == btnDisplay) {
  168.  
  169. try {
  170.  
  171. JOptionPane.showMessageDialog(null, "Load data from database ...");
  172.  
  173. ResultSet rs = stmt.executeQuery("select * from laboratory");
  174. while (rs.next());
  175.  
  176. int gdTT = rs.getInt("TIME_TAKEN");
  177. int gdTR = rs.getInt("TIME_RETURNED");
  178. String gdName = rs.getString("LECTURER_NAME");
  179. int gdPhone = rs.getInt("PHONE_NUMBER");
  180. String gdLab = rs.getString("LABORATORY");
  181. String gdDep = rs.getString("DEPARTMENT");
  182.  
  183. DefaultTableModel model = new DefaultTableModel();
  184. JTable table = new JTable(model);
  185. JScrollPane scrollPane = new JScrollPane();
  186.  
  187. model.addRow(new Object [] {gdTT, gdTR, gdName, gdPhone, gdLab, gdDep});
  188. table.setModel(model);
  189. scrollPane = new JScrollPane(table);
  190. scrollPane.setBounds(130,120,400,150);
  191. add(scrollPane);
  192.  
  193. } catch (Exception exc2) {
  194. exc2.printStackTrace();
  195. }
  196. }
  197.  
  198. else if (e.getSource() == btnClose) {
  199. int close = JOptionPane.showConfirmDialog(null, "Do you want to close this program ?", "Exit", JOptionPane.YES_NO_OPTION);
  200. if (close == JOptionPane.YES_OPTION) {
  201. System.exit(0);
  202. } else if (close == JOptionPane.NO_OPTION) {
  203. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  204. }
  205. }
  206.  
  207. }
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement