Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.event.ActionEvent.*;
- import java.util.Date.*;
- import javax.swing.*;
- import javax.swing.ImageIcon.*;
- import javax.swing.table.*;
- import java.sql.*;
- public class labKeys extends JFrame implements ActionListener {
- Container con;
- JLabel lblTT, lblTR, lblName, lblPhone, lblLab, lblDep, lblGTT, lblGTR, lblGName, lblGPhone, lblGLab, lblGDep;
- JTextField txtTT, txtTR, txtName, txtPhone;
- JComboBox cboLab;
- JRadioButton rbDJTMK, rbDJPA;
- ButtonGroup bGroup;
- JButton btnSave, btnDisplay, btnClose;
- ImageIcon icon;
- Connection conn = null;
- Statement stmt = null;
- public labKeys() {
- setTitle("Tracking Lab Keys");
- setVisible(true);
- setLayout(new FlowLayout());
- setSize(400, 500);
- setBackground(Color.LIGHT_GRAY);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- con = getContentPane();
- icon = new ImageIcon("icon.PNG");
- Image Image = icon.getImage();
- this.setIconImage(Image);
- lblTT = new JLabel("Time Taken");
- lblTR = new JLabel("Time Returned");
- lblName = new JLabel("Lecturer Name");
- lblPhone = new JLabel("Phone Number");
- lblLab = new JLabel("Laboratory");
- lblDep = new JLabel("Department");
- JLabel lblISTime = new JLabel();
- lblISTime.setIcon(new ImageIcon("stime.PNG"));
- JLabel lblIETime = new JLabel();
- lblIETime.setIcon(new ImageIcon("etime.PNG"));
- JLabel lblIName = new JLabel();
- lblIName.setIcon(new ImageIcon("name.PNG"));
- JLabel lblIPhone = new JLabel();
- lblIPhone.setIcon(new ImageIcon("phone.PNG"));
- JLabel lblILab = new JLabel();
- lblILab.setIcon(new ImageIcon("lab.PNG"));
- JLabel lblIDep = new JLabel();
- lblIDep.setIcon(new ImageIcon("dep.PNG"));
- txtTT = new JTextField(10);
- txtTR = new JTextField(10);
- txtName = new JTextField(15);
- txtPhone = new JTextField(15);
- String[] chooseLab = {"MHM", "MIT1", "MIT2", "MIT3", "MPA", "MKR", "MSKD"};
- cboLab = new JComboBox(chooseLab);
- cboLab.setSelectedIndex(0);
- rbDJTMK = new JRadioButton("JTMK");
- rbDJPA = new JRadioButton("JPA");
- bGroup = new ButtonGroup();
- bGroup.add(rbDJTMK);
- bGroup.add(rbDJPA);
- btnSave = new JButton("Save");
- btnDisplay = new JButton("Display");
- btnClose = new JButton("Close");
- lblGTT = new JLabel("");
- lblGTR = new JLabel("");
- lblGName = new JLabel("");
- lblGPhone = new JLabel("");
- lblGLab = new JLabel("");
- lblGDep = new JLabel("");
- lblGTT.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
- lblGTR.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
- lblGName.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
- lblGPhone.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
- lblGLab.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
- lblGDep.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
- add(new JLabel(new ImageIcon("key.PNG")));
- con.add(lblISTime);
- con.add(lblTT);
- con.add(txtTT);
- con.add(lblIETime);
- con.add(lblTR);
- con.add(txtTR);
- con.add(lblIName);
- con.add(lblName);
- con.add(txtName);
- con.add(lblIPhone);
- con.add(lblPhone);
- con.add(txtPhone);
- con.add(lblILab);
- con.add(lblLab);
- con.add(cboLab);
- con.add(lblIDep);
- con.add(lblDep);
- con.add(rbDJTMK);
- con.add(rbDJPA);
- con.add(btnSave);
- con.add(btnDisplay);
- con.add(btnClose);
- con.add(lblGTT);
- con.add(lblGTR);
- con.add(lblGName);
- con.add(lblGPhone);
- con.add(lblGLab);
- con.add(lblGDep);
- pack();
- btnSave.addActionListener(this);
- btnDisplay.addActionListener(this);
- btnClose.addActionListener(this);
- }
- public static void main(String[] args) {
- labKeys lKeys = new labKeys();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnSave) {
- try {
- int gTT = Integer.parseInt(txtTT.getText());
- int gTR = Integer.parseInt(txtTR.getText());
- String gName = txtName.getText();
- int gPhone = Integer.parseInt(txtPhone.getText());
- String gLab = cboLab.getSelectedItem().toString();
- String gRB = "";
- if (rbDJTMK.isSelected()) {
- gRB = rbDJTMK.getActionCommand();
- } else if (rbDJPA.isSelected()) {
- gRB = rbDJPA.getActionCommand();
- }
- JOptionPane.showMessageDialog(null, "Time taken : " + gTT + "\nTime returned : " + gTR
- + "\nLecturer name : " + gName + "\nPhone number : " + gPhone
- + "\nLaboratory : " + gLab + "\nDepartment : " + gRB);
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/semester4", "root", "");
- stmt = conn.createStatement();
- stmt.executeUpdate("insert into laboratory values(" + gTT + "," + gTR + ",'" + gName + "'," + gPhone + ",'" + gLab + "','" + gRB + "')");
- } catch (Exception exc) {
- System.err.println("Got an exception! ");
- System.err.println(exc.getMessage());
- }
- }
- else if (e.getSource() == btnDisplay) {
- try {
- JOptionPane.showMessageDialog(null, "Load data from database ...");
- ResultSet rs = stmt.executeQuery("select * from laboratory");
- while (rs.next());
- int gdTT = rs.getInt("TIME_TAKEN");
- int gdTR = rs.getInt("TIME_RETURNED");
- String gdName = rs.getString("LECTURER_NAME");
- int gdPhone = rs.getInt("PHONE_NUMBER");
- String gdLab = rs.getString("LABORATORY");
- String gdDep = rs.getString("DEPARTMENT");
- DefaultTableModel model = new DefaultTableModel();
- JTable table = new JTable(model);
- JScrollPane scrollPane = new JScrollPane();
- model.addRow(new Object [] {gdTT, gdTR, gdName, gdPhone, gdLab, gdDep});
- table.setModel(model);
- scrollPane = new JScrollPane(table);
- scrollPane.setBounds(130,120,400,150);
- add(scrollPane);
- } catch (Exception exc2) {
- exc2.printStackTrace();
- }
- }
- else if (e.getSource() == btnClose) {
- int close = JOptionPane.showConfirmDialog(null, "Do you want to close this program ?", "Exit", JOptionPane.YES_NO_OPTION);
- if (close == JOptionPane.YES_OPTION) {
- System.exit(0);
- } else if (close == JOptionPane.NO_OPTION) {
- setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement