Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. // This class works to call the frame (loginFrame)
- 2. package travelmanagement;
- 3.
- 4. public class TravelManagement {
- 5. public static void main(String[] args) {
- 6. loginFrame frame = new loginFrame();
- 7. }
- 8. }
- 9.
- 10. package travelmanagement;
- 11. import java.sql.*;
- 12. import javax.swing.*;
- 13.
- 14.
- 15. public class mySqlConnect {
- 16.
- 17. Connection con = null;
- 18. public static Connection ConnectDC() {
- 19. try {
- 20. Class.forName("com.mysql.jdbc.Driver");
- 21. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/travelmanagement", "root", "0786");
- 22. return con;
- 23. } catch (Exception e) {
- 24. return null;
- 25. }
- 26. }
- 27. }
- 28.
- 29.
- 30. package travelmanagement;
- 31.
- 32. import java.awt.Color;
- 33. import java.awt.event.ActionEvent;
- 34. import java.awt.event.ActionListener;
- 35. import javax.swing.JButton;
- 36. import javax.swing.JFrame;
- 37. import javax.swing.JLabel;
- 38. import javax.swing.JPasswordField;
- 39. import javax.swing.JTextField;
- 40. import java.sql.*;
- 41. import javax.swing.ImageIcon;
- 42. import javax.swing.JOptionPane;
- 43.
- 44. public class loginFrame extends JFrame implements ActionListener {
- 45.
- 46. Connection con = mySqlConnect.ConnectDC();
- 47. PreparedStatement pst = null;
- 48. ResultSet rs = null;
- 49. JLabel heading, usernameLabel, passwordLabel, errorMsg;
- 50. JTextField usernameField;
- 51. JPasswordField passwordField;
- 52. JButton loginButton, exitButton;
- 53.
- 54. loginFrame() {
- 55. this.setSize(350, 225);
- 56. this.setLocationRelativeTo(null);
- 57. this.setUndecorated(true);
- 58. this.setTitle("Login");
- 59. getContentPane().setBackground(Color.WHITE);
- 60.
- 61. heading = new JLabel("Login");
- 62. usernameLabel = new JLabel("Username:");
- 63. usernameField = new JTextField(12);
- 64. passwordLabel = new JLabel("Password:");
- 65. passwordField = new JPasswordField(12);
- 66. loginButton = new JButton("Login");
- 67. loginButton.addActionListener(this);
- 68. exitButton = new JButton("Exit");
- 69. exitButton.addActionListener(this);
- 70. errorMsg = new JLabel("Incorrect Username or Password!");
- 71. errorMsg.setVisible(false);
- 72.
- 73. // 1st x value, 2nd y value, 3rd width, 4th height
- 74. this.setLayout(null);
- 75. heading.setBounds(125, 5, 125, 25);
- 76. heading.setFont(heading.getFont().deriveFont(20f));
- 77. usernameLabel.setBounds(25, 50, 100, 50);
- 78. usernameField.setBounds(150, 60, 150, 30);
- 79. passwordLabel.setBounds(25, 100, 100, 50);
- 80. passwordField.setBounds(150, 105, 150, 30);
- 81. errorMsg.setBounds(75, 147, 200, 30);
- 82. errorMsg.setForeground(Color.red);
- 83. loginButton.setBounds(85, 185, 75, 30);
- 84. exitButton.setBounds(165, 185, 75, 30);
- 85.
- 86. this.add(heading);
- 87. this.add(usernameLabel);
- 88. this.add(usernameField);
- 89. this.add(passwordLabel);
- 90. this.add(passwordField);
- 91. this.add(loginButton);
- 92. this.add(exitButton);
- 93. this.add(errorMsg);
- 94. this.setVisible(true);
- 95. }
- 96.
- 97. public void clearFields() {
- 98. usernameField.setText("");
- 99. passwordField.setText("");
- 100. }
- 101.
- 102. public void checkLogin() {
- 103. String Sql = "SELECT * from login where Username=? and Password=?";
- 104. try {
- 105. pst = con.prepareStatement(Sql);
- 106. pst.setString(1, usernameField.getText());
- 107. pst.setString(2, passwordField.getText());
- 108. rs = pst.executeQuery();
- 109. if (rs.next()) {
- 110. mainFrame frame = new mainFrame();
- 111. this.dispose();
- 112. } else {
- 113. errorMsg.setVisible(true);
- 114. clearFields();
- 115. }
- 116. } catch (Exception e) {
- 117.
- 118. }
- 119. }
- 120.
- 121. @Override
- 122. public void actionPerformed(ActionEvent ae) {
- 123. if (ae.getSource() == loginButton) {
- 124. checkLogin();
- 125. } else if (ae.getSource() == exitButton) {
- 126. System.exit(0);
- 127. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- 128. }
- 129. }
- 130.
- 131. }
- 132.
- 133.
- 134. package travelmanagement;
- 135.
- 136. import java.awt.Color;
- 137. import java.awt.event.ActionEvent;
- 138. import java.awt.event.ActionListener;
- 139. import javax.swing.ImageIcon;
- 140. import javax.swing.JButton;
- 141. import javax.swing.JFrame;
- 142.
- 143. public class mainFrame extends JFrame implements ActionListener {
- 144.
- 145. JButton passport, group;
- 146.
- 147. mainFrame() {
- 148. this.setSize(700, 450);
- 149. this.setLocationRelativeTo(null);
- 150. getContentPane().setBackground(Color.WHITE);
- 151. this.setTitle("Bukhari Travel Ltd");
- 152. ImageIcon icon = new ImageIcon(getClass().getResource("logo.jpg"));
- 153. this.setIconImage(icon.getImage());
- 154. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- 155.
- 156. passport = new JButton(new ImageIcon(getClass().getResource("passport.png")));
- 157. group = new JButton(new ImageIcon(getClass().getResource("group.jpg")));
- 158.
- 159. // 1st x value, 2nd y value, 3rd width, 4th height
- 160. this.setLayout(null);
- 161. passport.setBounds(25, 75, 300, 300);
- 162. passport.addActionListener(this);
- 163. group.setBounds(355, 75, 300, 300);
- 164. group.addActionListener(this);
- 165.
- 166. this.add(passport);
- 167. this.add(group);
- 168. this.setVisible(true);
- 169. }
- 170.
- 171. @Override
- 172. public void actionPerformed(ActionEvent ae) {
- 173. if(ae.getSource() == passport) {
- 174. passportFrame frame = new passportFrame();
- 175. this.dispose();
- 176. } else if(ae.getSource() == group) {
- 177. groupFrame frame = new groupFrame();
- 178. this.dispose();
- 179. }
- 180. }
- 181.
- 182. }
- 183.
- 184.
- 185. package travelmanagement;
- 186.
- 187. import com.itextpdf.text.Document;
- 188. import com.itextpdf.text.PageSize;
- 189. import com.itextpdf.text.pdf.PdfPTable;
- 190. import com.itextpdf.text.pdf.PdfWriter;
- 191. import java.awt.Color;
- 192. import java.awt.Desktop;
- 193. import java.awt.Dimension;
- 194. import java.awt.event.ActionEvent;
- 195. import java.awt.event.ActionListener;
- 196. import java.io.File;
- 197. import java.io.FileOutputStream;
- 198. import java.sql.Connection;
- 199. import java.sql.PreparedStatement;
- 200. import java.sql.ResultSet;
- 201. import javax.swing.ImageIcon;
- 202. import javax.swing.JButton;
- 203. import javax.swing.JFileChooser;
- 204. import javax.swing.JFrame;
- 205. import javax.swing.JLabel;
- 206. import javax.swing.JOptionPane;
- 207. import javax.swing.JPanel;
- 208. import javax.swing.JScrollPane;
- 209. import javax.swing.JTable;
- 210. import javax.swing.JTextField;
- 211. import javax.swing.table.DefaultTableCellRenderer;
- 212. import javax.swing.table.DefaultTableModel;
- 213. import net.proteanit.sql.DbUtils;
- 214.
- 215.
- 216. public class passportFrame extends JFrame implements ActionListener {
- 217.
- 218. JLabel heading, agentL, groupNoL, noPassportL, sendingDateL, postageBarcodeL, receivingDateL,
- 219. returnedL, commentsL, insertDataL, PPsummaryL, passportInHandL, passportInHandN, passportInLondonL,
- 220. passportInLondonN;
- 221. JTextField agentTF, groupNoTF, noPassportTF, sendingDateTF, postageBarcodeTF, receivingDateTF, returnedTF,
- 222. commentsTF;
- 223. JButton backButton, addRow, insertButton, deleteButton, editButton, printButton;
- 224. JPanel mainTablePanel, insertDataPanel, summaryPanel;
- 225. JTable mainTable;
- 226. JScrollPane scrollPane;
- 227. Connection con = mySqlConnect.ConnectDC();;
- 228. PreparedStatement pst = null;
- 229. ResultSet rs = null;
- 230.
- 231. passportFrame() {
- 232. this.setLocationRelativeTo(null);
- 233. this.getContentPane().setBackground(Color.WHITE);
- 234. this.setTitle("Bukhari Travel Ltd");
- 235. ImageIcon icon = new ImageIcon(getClass().getResource("logo.jpg"));
- 236. this.setIconImage(icon.getImage());
- 237. this.setExtendedState(JFrame.MAXIMIZED_BOTH);
- 238. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- 239.
- 240.
- 241. heading = new JLabel("Manage Your Passports");
- 242. insertDataL = new JLabel("Add New Group");
- 243. agentL = new JLabel("Agent:");
- 244. groupNoL = new JLabel("Group No.:");
- 245. noPassportL = new JLabel("No. of Passports:");
- 246. sendingDateL = new JLabel("Sending Date:");
- 247. postageBarcodeL = new JLabel("Postage Barcode:");
- 248. receivingDateL = new JLabel("Receiving Date:");
- 249. returnedL = new JLabel("Returned:");
- 250. commentsL = new JLabel("Travelling Date:");
- 251. PPsummaryL = new JLabel("Passports Summary");
- 252. passportInHandL = new JLabel("Passports in Hand");
- 253. passportInHandN = new JLabel();
- 254. passportInLondonL = new JLabel("Passports in London");
- 255. passportInLondonN = new JLabel();
- 256.
- 257. agentTF = new JTextField();
- 258. groupNoTF = new JTextField();
- 259. noPassportTF = new JTextField();
- 260. sendingDateTF = new JTextField();
- 261. postageBarcodeTF = new JTextField();
- 262. receivingDateTF = new JTextField();
- 263. returnedTF = new JTextField();
- 264. commentsTF = new JTextField();
- 265.
- 266. insertButton = new JButton("Add");
- 267. insertButton.addActionListener(this);
- 268. editButton = new JButton("Edit");
- 269. editButton.addActionListener(this);
- 270. deleteButton = new JButton("Delete");
- 271. deleteButton.addActionListener(this);
- 272. printButton = new JButton("Print PDF");
- 273. printButton.addActionListener(this);
- 274. backButton = new JButton("Go Back");
- 275. backButton.addActionListener(this);
- 276.
- 277. DefaultTableModel mainTableModel = new DefaultTableModel();
- 278. mainTable = new JTable(mainTableModel);
- 279. scrollPane = new JScrollPane(mainTable);
- 280.
- 281. insertDataPanel = new JPanel();
- 282. mainTablePanel = new JPanel();
- 283. summaryPanel = new JPanel();
- 284.
- 285. insertDataPanel.add(insertDataL);
- 286. insertDataPanel.add(agentL);
- 287. insertDataPanel.add(groupNoL);
- 288. insertDataPanel.add(noPassportL);
- 289. insertDataPanel.add(sendingDateL);
- 290. insertDataPanel.add(postageBarcodeL);
- 291. insertDataPanel.add(receivingDateL);
- 292. insertDataPanel.add(returnedL);
- 293. insertDataPanel.add(commentsL);
- 294. insertDataPanel.add(agentTF);
- 295. insertDataPanel.add(groupNoTF);
- 296. insertDataPanel.add(noPassportTF);
- 297. insertDataPanel.add(sendingDateTF);
- 298. insertDataPanel.add(postageBarcodeTF);
- 299. insertDataPanel.add(receivingDateTF);
- 300. insertDataPanel.add(returnedTF);
- 301. insertDataPanel.add(commentsTF);
- 302. insertDataPanel.add(insertButton);
- 303. insertDataPanel.add(editButton);
- 304. insertDataPanel.add(deleteButton);
- 305.
- 306. mainTableModel.addColumn("Agent");
- 307. mainTableModel.addColumn("Group No.");
- 308. mainTableModel.addColumn("No. of Passports");
- 309. mainTableModel.addColumn("Sending Date");
- 310. mainTableModel.addColumn("Postage Barcode");
- 311. mainTableModel.addColumn("Receiving Date");
- 312. mainTableModel.addColumn("Returned");
- 313. mainTableModel.addColumn("Comments");
- 314.
- 315. summaryPanel.add(PPsummaryL);
- 316. summaryPanel.add(passportInHandL);
- 317. summaryPanel.add(passportInHandN);
- 318. summaryPanel.add(passportInLondonL);
- 319. summaryPanel.add(passportInLondonN);
- 320.
- 321. // 1st x value, 2nd y value, 3rd width, 4th height
- 322. heading.setBounds(575, 10, 300, 30);
- 323. heading.setFont(heading.getFont().deriveFont(25f));
- 324.
- 325. insertDataPanel.setBounds(20, 50, 375, 375);
- 326. insertDataL.setBounds(120, 10, 200, 30);
- 327. insertDataL.setFont(insertDataL.getFont().deriveFont(20f));
- 328. agentL.setBounds(20, 62, 100, 15);
- 329. groupNoL.setBounds(20, 92, 100, 15);
- 330. noPassportL.setBounds(20, 122, 100, 15);
- 331. sendingDateL.setBounds(20, 152, 100, 15);
- 332. postageBarcodeL.setBounds(20, 182, 150, 15);
- 333. receivingDateL.setBounds(20, 212, 100, 15);
- 334. returnedL.setBounds(20, 242, 100, 15);
- 335. commentsL.setBounds(20, 272, 100, 15);
- 336.
- 337. agentTF.setBounds(180, 55, 150, 25);
- 338. groupNoTF.setBounds(180, 85, 150, 25);
- 339. noPassportTF.setBounds(180, 115, 150, 25);
- 340. sendingDateTF.setBounds(180, 145, 150, 25);
- 341. postageBarcodeTF.setBounds(180, 175, 150, 25);
- 342. receivingDateTF.setBounds(180, 205, 150, 25);
- 343. returnedTF.setBounds(180, 235, 150, 25);
- 344. commentsTF.setBounds(180, 265, 150, 25);
- 345.
- 346. insertButton.setBounds(55, 320, 75, 30);
- 347. editButton.setBounds(155, 320, 75, 30);
- 348. deleteButton.setBounds(255, 320, 75, 30);
- 349.
- 350. mainTablePanel.setBounds(425, 50, 900, 500);
- 351. mainTable.setLayout(null);
- 352. mainTable.setBounds(475, 300, 1000, 500);
- 353. mainTable.setRowHeight(25);
- 354. mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
- 355.
- 356. summaryPanel.setBounds(20, 435, 375, 250);
- 357. summaryPanel.setLayout(null);
- 358. PPsummaryL.setFont(PPsummaryL.getFont().deriveFont(20f));
- 359. PPsummaryL.setBounds(85, 10, 200, 30);
- 360. passportInHandL.setBounds(20, 60, 150, 30);
- 361. passportInHandN.setBounds(200, 60, 150, 30);
- 362. passportInLondonL.setBounds(20, 90, 150, 30);
- 363. passportInLondonN.setBounds(200, 90, 150, 30);
- 364.
- 365. backButton.setBounds(675, 650, 100, 35);
- 366. printButton.setBounds(560, 650, 100, 35);
- 367.
- 368. mainTablePanel.add(scrollPane);
- 369. scrollPane.setPreferredSize(new Dimension(900, 500));
- 370.
- 371. this.add(heading);
- 372. this.add(insertDataPanel);
- 373. this.add(mainTablePanel);
- 374. this.add(summaryPanel);
- 375. this.add(printButton);
- 376. this.add(backButton);
- 377. insertDataPanel.setLayout(null);
- 378. summaryPanel.setLayout(null);
- 379. this.setLayout(null);
- 380. retrieveData();
- 381. this.setVisible(true);
- 382. }
- 383.
- 384. private void retrieveData() {
- 385. try {
- 386. String sqlShowData = "SELECT * FROM `passports`";
- 387. pst = con.prepareStatement(sqlShowData);
- 388. rs = pst.executeQuery();
- 389. mainTable.setModel(DbUtils.resultSetToTableModel(rs));
- 390. } catch (Exception ex) {
- 391. System.out.println(ex);
- 392. }
- 393. }
- 394.
- 395. private void clearField() {
- 396. agentTF.setText("");
- 397. groupNoTF.setText("");
- 398. noPassportTF.setText("");
- 399. sendingDateTF.setText("");
- 400. postageBarcodeTF.setText("");
- 401. receivingDateTF.setText("");
- 402. returnedTF.setText("");
- 403. commentsTF.setText("");
- 404. }
- 405.
- 406. private void addGroup() {
- 407. try {
- 408. String sqlInsert = "INSERT INTO `passports`(`Agent`, `GroupNo`, `NoPassports`, `SendingDate`, `Postage`, `ReceivingDate`, `Returned`, `Travelling Date`) VALUES (?,?,?,?,?,?,?,?)";
- 409. pst = con.prepareStatement(sqlInsert);
- 410. pst.setString(1, agentTF.getText());
- 411. pst.setString(2, groupNoTF.getText());
- 412. pst.setString(3, noPassportTF.getText());
- 413. pst.setString(4, sendingDateTF.getText());
- 414. pst.setString(5, postageBarcodeTF.getText());
- 415. pst.setString(6, receivingDateTF.getText());
- 416. pst.setString(7, returnedTF.getText());
- 417. pst.setString(8, commentsTF.getText());
- 418. pst.executeUpdate();
- 419. JOptionPane.showMessageDialog(null, "Successfully Inserted!");
- 420. retrieveData();
- 421. clearField();
- 422. } catch (Exception ex) {
- 423. JOptionPane.showMessageDialog(null, ex);
- 424. }
- 425. }
- 426.
- 427. private void deleteGroup() {
- 428. try {
- 429. String sqlDelete = "DELETE FROM `passports` WHERE `GroupNo` = ?";
- 430. pst = con.prepareStatement(sqlDelete);
- 431. pst.setString(1, groupNoTF.getText());
- 432. pst.executeUpdate();
- 433. JOptionPane.showMessageDialog(null, "Successfully Deleted!");
- 434. retrieveData();
- 435. clearField();
- 436. } catch (Exception ex) {
- 437. JOptionPane.showMessageDialog(null, ex);
- 438. }
- 439. }
- 440.
- 441. private void updateGroup() {
- 442. try {
- 443. String sqlUpdate = "UPDATE `passports` SET `Agent`=?,`NoPassports`=?,`SendingDate`=?,`Postage`=?,`ReceivingDate`=?,`Returned`=?,`Travelling Date`=? WHERE `GroupNo`=?";
- 444. pst = con.prepareStatement(sqlUpdate);
- 445. pst.setString(8, groupNoTF.getText());
- 446. pst.setString(1, agentTF.getText());
- 447. pst.setString(2, noPassportTF.getText());
- 448. pst.setString(3, sendingDateTF.getText());
- 449. pst.setString(4, postageBarcodeTF.getText());
- 450. pst.setString(5, receivingDateTF.getText());
- 451. pst.setString(6, returnedTF.getText());
- 452. pst.setString(7, commentsTF.getText());
- 453. pst.executeUpdate();
- 454. JOptionPane.showMessageDialog(null, "Successfully Updated!");
- 455. retrieveData();
- 456. clearField();
- 457. } catch (Exception ex) {
- 458. JOptionPane.showMessageDialog(null, ex);
- 459. }
- 460. }
- 461. private void createPDF() {
- 462. String path = "";
- 463. JFileChooser j = new JFileChooser();
- 464. j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- 465. int x = j.showSaveDialog(this);
- 466. if (x == JFileChooser.APPROVE_OPTION) {
- 467. path = j.getSelectedFile().getPath();
- 468. }
- 469.
- 470. Document doc = new Document(PageSize.A4.rotate());
- 471. try {
- 472. PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(path +".pdf"));
- 473. doc.open();
- 474. PdfPTable tbl = new PdfPTable(8);
- 475.
- 476. tbl.addCell("Agent");
- 477. tbl.addCell("Group No.");
- 478. tbl.addCell("No. of Passports");
- 479. tbl.addCell("Sending Out Date");
- 480. tbl.addCell("Postage Barcode");
- 481. tbl.addCell("Receiving Back Date");
- 482. tbl.addCell("Returned");
- 483. tbl.addCell("Comments");
- 484.
- 485. for (int i = 0; i < mainTable.getRowCount(); i++) {
- 486. String agent = mainTable.getValueAt(i, 0).toString();
- 487. String groupNo = mainTable.getValueAt(i, 1).toString();
- 488. String noPassport = mainTable.getValueAt(i, 2).toString();
- 489. String sendingDate = mainTable.getValueAt(i, 3).toString();
- 490. String postageBarcode = mainTable.getValueAt(i, 3).toString();
- 491. String receivingDate = mainTable.getValueAt(i, 4).toString();
- 492. String returned = mainTable.getValueAt(i, 5).toString();
- 493. String comments = mainTable.getValueAt(i, 6).toString();
- 494.
- 495. tbl.addCell(agent);
- 496. tbl.addCell(groupNo);
- 497. tbl.addCell(noPassport);
- 498. tbl.addCell(sendingDate);
- 499. tbl.addCell(postageBarcode);
- 500. tbl.addCell(receivingDate);
- 501. tbl.addCell(returned);
- 502. tbl.addCell(comments);
- 503. }
- 504. doc.add(tbl);
- 505. doc.close();
- 506. writer.close();
- 507. } catch (Exception ex) {
- 508. System.out.println(ex);
- 509. }
- 510. }
- 511.
- 512. @Override
- 513. public void actionPerformed(ActionEvent ae) {
- 514. if (ae.getSource() == backButton) {
- 515. mainFrame frame = new mainFrame();
- 516. this.dispose();
- 517. } else if (ae.getSource() == insertButton) {
- 518. addGroup();
- 519. } else if (ae.getSource() == editButton) {
- 520. updateGroup();
- 521. } else if (ae.getSource() == deleteButton) {
- 522. deleteGroup();
- 523. } else if (ae.getSource() == printButton) {
- 524. createPDF();
- 525. }
- 526. }
- 527. }
- 528.
- 529. package travelmanagement;
- 530.
- 531. import com.itextpdf.text.Document;
- 532. import com.itextpdf.text.PageSize;
- 533. import com.itextpdf.text.pdf.PdfPTable;
- 534. import com.itextpdf.text.pdf.PdfWriter;
- 535. import java.awt.Color;
- 536. import java.awt.Dimension;
- 537. import java.awt.event.ActionEvent;
- 538. import java.awt.event.ActionListener;
- 539. import java.io.FileOutputStream;
- 540. import java.sql.Connection;
- 541. import java.sql.PreparedStatement;
- 542. import java.sql.ResultSet;
- 543. import javafx.scene.transform.Rotate;
- 544. import javax.swing.ImageIcon;
- 545. import javax.swing.JButton;
- 546. import javax.swing.JFileChooser;
- 547. import javax.swing.JFrame;
- 548. import javax.swing.JLabel;
- 549. import javax.swing.JOptionPane;
- 550. import javax.swing.JPanel;
- 551. import javax.swing.JScrollPane;
- 552. import javax.swing.JTable;
- 553. import javax.swing.JTextField;
- 554. import javax.swing.table.DefaultTableModel;
- 555. import javax.swing.table.TableColumnModel;
- 556. import javax.swing.table.TableModel;
- 557. import net.proteanit.sql.DbUtils;
- 558.
- 559. public class groupFrame extends JFrame implements ActionListener {
- 560.
- 561. Connection con = mySqlConnect.ConnectDC();
- 562. PreparedStatement pst = null;
- 563. PreparedStatement pst2 = null;
- 564. ResultSet rs = null;
- 565. JLabel heading, indexL, ageL, surnameL, forenameL, titleL, passportNoL, DOBL, expiryDateL, contactNoL, invoiceNoL, referenceL;
- 566. JTextField indexTF, ageTF, surnameTF, forenameTF, titleTF, passportNoTF, DOBTF, expiryDateTF, contactNoTF, invoiceNoTF, referenceTF;
- 567. JButton back, addPilgrim, deletePilgrim, updatePilgrim, savePDF;
- 568. JPanel mainTablePanel, insertDataPanel;
- 569. JTable mainTable;
- 570. DefaultTableModel mainTableModel;
- 571. JScrollPane scrollPane;
- 572.
- 573. groupFrame() {
- 574. this.setLocationRelativeTo(null);
- 575. getContentPane().setBackground(Color.WHITE);
- 576. this.setTitle("Bukhari Travel Ltd");
- 577. this.setExtendedState(JFrame.MAXIMIZED_BOTH);
- 578. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- 579. ImageIcon icon = new ImageIcon(getClass().getResource("logo.jpg"));
- 580. this.setIconImage(icon.getImage());
- 581.
- 582.
- 583. //All the labels
- 584. heading = new JLabel("Manage Your Groups");
- 585. indexL = new JLabel("Index");
- 586. ageL = new JLabel("Age");
- 587. surnameL = new JLabel("Surname");
- 588. forenameL = new JLabel("Forename(s)");
- 589. titleL = new JLabel("Title");
- 590. passportNoL = new JLabel("Passport No.");
- 591. DOBL = new JLabel("D.O.B.");
- 592. expiryDateL = new JLabel("Expiry Date");
- 593. contactNoL = new JLabel("Contact No.");
- 594. invoiceNoL = new JLabel("Invoice No.");
- 595. referenceL = new JLabel("Reference");
- 596. indexTF = new JTextField();
- 597.
- 598. //All the Text Fields
- 599. ageTF = new JTextField();
- 600. surnameTF = new JTextField();
- 601. forenameTF = new JTextField();
- 602. titleTF = new JTextField();
- 603. passportNoTF = new JTextField();
- 604. DOBTF = new JTextField();
- 605. expiryDateTF = new JTextField();
- 606. contactNoTF = new JTextField();
- 607. invoiceNoTF = new JTextField();
- 608. referenceTF = new JTextField();
- 609.
- 610. //Buttons
- 611. back = new JButton("Go Back");
- 612. back.addActionListener(this);
- 613. addPilgrim = new JButton("Add Pilgrim");
- 614. addPilgrim.addActionListener(this);
- 615. updatePilgrim = new JButton("Update Pilgrim");
- 616. updatePilgrim.addActionListener(this);
- 617. deletePilgrim = new JButton("Delete Pilgrim");
- 618. deletePilgrim.addActionListener(this);
- 619. savePDF = new JButton("Save PDF");
- 620. savePDF.addActionListener(this);
- 621.
- 622. //Jpanels
- 623. mainTablePanel = new JPanel();
- 624. insertDataPanel = new JPanel();
- 625.
- 626. //Insert Data Panel additions
- 627. insertDataPanel.add(indexL);
- 628. insertDataPanel.add(indexTF);
- 629. insertDataPanel.add(ageL);
- 630. insertDataPanel.add(ageTF);
- 631. insertDataPanel.add(surnameL);
- 632. insertDataPanel.add(surnameTF);
- 633. insertDataPanel.add(forenameL);
- 634. insertDataPanel.add(forenameTF);
- 635. insertDataPanel.add(titleL);
- 636. insertDataPanel.add(titleTF);
- 637. insertDataPanel.add(passportNoL);
- 638. insertDataPanel.add(passportNoTF);
- 639. insertDataPanel.add(DOBL);
- 640. insertDataPanel.add(DOBTF);
- 641. insertDataPanel.add(expiryDateL);
- 642. insertDataPanel.add(expiryDateTF);
- 643. insertDataPanel.add(contactNoL);
- 644. insertDataPanel.add(contactNoTF);
- 645. insertDataPanel.add(invoiceNoL);
- 646. insertDataPanel.add(invoiceNoTF);
- 647. insertDataPanel.add(referenceL);
- 648. insertDataPanel.add(referenceTF);
- 649. insertDataPanel.add(addPilgrim);
- 650. insertDataPanel.add(updatePilgrim);
- 651. insertDataPanel.add(deletePilgrim);
- 652.
- 653. //maintable
- 654. mainTableModel = new DefaultTableModel();
- 655. mainTable = new JTable(mainTableModel);
- 656. scrollPane = new JScrollPane(mainTable);
- 657.
- 658. mainTableModel.addColumn("Index");
- 659. mainTableModel.addColumn("Age");
- 660. mainTableModel.addColumn("Surname");
- 661. mainTableModel.addColumn("Forename(s)");
- 662. mainTableModel.addColumn("Title");
- 663. mainTableModel.addColumn("Passport No.");
- 664. mainTableModel.addColumn("D.O.B.");
- 665. mainTableModel.addColumn("Exiry Date");
- 666. mainTableModel.addColumn("Contact No.");
- 667. mainTableModel.addColumn("Invoice No.");
- 668. mainTableModel.addColumn("Reference");
- 669. mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
- 670.
- 671. heading.setBounds(575, 10, 300, 25);
- 672. heading.setFont(heading.getFont().deriveFont(20f));
- 673. mainTablePanel.setBounds(0, 50, 1366, 400);
- 674. insertDataPanel.setBounds(20, 480, 1325, 150);
- 675. savePDF.setBounds(570, 640, 100, 35);
- 676. back.setBounds(700, 640, 100, 35);
- 677.
- 678. indexL.setBounds(30, 10, 40, 25);
- 679. ageL.setBounds(90, 10, 40, 25);
- 680. surnameL.setBounds(170, 10, 80, 25);
- 681. forenameL.setBounds(330, 10, 80, 25);
- 682. titleL.setBounds(490, 10, 40, 25);
- 683. passportNoL.setBounds(560, 10, 80, 25);
- 684. DOBL.setBounds(680, 10, 80, 25);
- 685. expiryDateL.setBounds(800, 10, 80, 25);
- 686. contactNoL.setBounds(920, 10, 80, 25);
- 687. invoiceNoL.setBounds(1050, 10, 80, 25);
- 688. referenceL.setBounds(1170, 10, 80, 25);
- 689. indexTF.setBounds(20, 50, 40, 30);
- 690. ageTF.setBounds(80, 50, 40, 30);
- 691. surnameTF.setBounds(140, 50, 150, 30);
- 692. forenameTF.setBounds(310, 50, 150, 30);
- 693. titleTF.setBounds(480, 50, 40, 30);
- 694. passportNoTF.setBounds(540, 50, 100, 30);
- 695. DOBTF.setBounds(660, 50, 100, 30);
- 696. expiryDateTF.setBounds(780, 50, 100, 30);
- 697. contactNoTF.setBounds(900, 50, 110, 30);
- 698. invoiceNoTF.setBounds(1030, 50, 100, 30);
- 699. referenceTF.setBounds(1150, 50, 150, 30);
- 700. mainTable.setRowHeight(25);
- 701. addPilgrim.setBounds(460, 100, 125, 35);
- 702. updatePilgrim.setBounds(600, 100, 125, 35);
- 703. deletePilgrim.setBounds(740, 100, 125, 35);
- 704.
- 705. mainTablePanel.add(scrollPane);
- 706. scrollPane.setPreferredSize(new Dimension(1356, 500));
- 707. this.add(heading);
- 708. this.add(mainTablePanel);
- 709. this.add(insertDataPanel);
- 710. this.add(savePDF);
- 711. this.add(back);
- 712. insertDataPanel.setLayout(null);
- 713. this.setLayout(null);
- 714. retrieveData();
- 715. scrollPane.setVisible(true);
- 716. this.setVisible(true);
- 717. }
- 718.
- 719. private void retrieveData() {
- 720. try {
- 721. String sqlShowData = "SELECT * FROM `grouptemplate`";
- 722. pst = con.prepareStatement(sqlShowData);
- 723. rs = pst.executeQuery();
- 724. mainTable.setModel(DbUtils.resultSetToTableModel(rs));
- 725. } catch (Exception e) {
- 726. }
- 727. }
- 728.
- 729. private void clearField() {
- 730. indexTF.setText("");
- 731. ageTF.setText("");
- 732. surnameTF.setText("");
- 733. forenameTF.setText("");
- 734. titleTF.setText("");
- 735. passportNoTF.setText("");
- 736. DOBTF.setText("");
- 737. expiryDateTF.setText("");
- 738. contactNoTF.setText("");
- 739. invoiceNoTF.setText("");
- 740. referenceTF.setText("");
- 741. }
- 742.
- 743. private void addPilgrim() {
- 744. try {
- 745. String sqlInsert = "INSERT INTO `grouptemplate`(`Index`, `Age`, `Surname`, `Forename(s)`, `Title`, `Passport Number`, `DOB`, `Expiry Date`, `Contact Number`, `Invoice Number`, `Reference`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
- 746. pst = con.prepareStatement(sqlInsert);
- 747. pst.setString(1, indexTF.getText());
- 748. pst.setString(2, ageTF.getText());
- 749. pst.setString(3, surnameTF.getText());
- 750. pst.setString(4, forenameTF.getText());
- 751. pst.setString(5, titleTF.getText());
- 752. pst.setString(6, passportNoTF.getText());
- 753. pst.setString(7, DOBTF.getText());
- 754. pst.setString(8, expiryDateTF.getText());
- 755. pst.setString(9, contactNoTF.getText());
- 756. pst.setString(10, invoiceNoTF.getText());
- 757. pst.setString(11, referenceTF.getText());
- 758. pst.executeUpdate();
- 759. JOptionPane.showMessageDialog(null, "Successfully Inserted!");
- 760. retrieveData();
- 761. clearField();
- 762. } catch (Exception ex) {
- 763. JOptionPane.showMessageDialog(null, ex);
- 764. }
- 765. }
- 766.
- 767. private void deletePilgrim() {
- 768. try {
- 769. String sqlDelete = "DELETE FROM `grouptemplate` WHERE `Index` = ?";
- 770. pst = con.prepareStatement(sqlDelete);
- 771. pst.setString(1, indexTF.getText());
- 772. pst.executeUpdate();
- 773. JOptionPane.showMessageDialog(null, "Successfully Deleted!");
- 774. retrieveData();
- 775. clearField();
- 776. } catch (Exception ex) {
- 777. JOptionPane.showMessageDialog(null, ex);
- 778. }
- 779. }
- 780.
- 781. private void updatePilgrim() {
- 782. try {
- 783. String sqlUpdate = "UPDATE `grouptemplate` SET `Age`=?,`Surname`=?,`Forename(s)`=?,`Title`=?,`Passport Number`=?,`DOB`=?,`Expiry Date`=?,`Contact Number`=?,`Invoice Number`=?,`Reference`=? WHERE ?";
- 784. pst = con.prepareStatement(sqlUpdate);
- 785. pst.setString(11, indexTF.getText());
- 786. pst.setString(1, ageTF.getText());
- 787. pst.setString(2, surnameTF.getText());
- 788. pst.setString(3, forenameTF.getText());
- 789. pst.setString(4, titleTF.getText());
- 790. pst.setString(5, passportNoTF.getText());
- 791. pst.setString(6, DOBTF.getText());
- 792. pst.setString(7, expiryDateTF.getText());
- 793. pst.setString(8, contactNoTF.getText());
- 794. pst.setString(9, invoiceNoTF.getText());
- 795. pst.setString(10, referenceTF.getText());
- 796. pst.executeUpdate();
- 797. JOptionPane.showMessageDialog(null, "Successfully Updated!");
- 798. retrieveData();
- 799. clearField();
- 800. } catch (Exception ex) {
- 801. JOptionPane.showMessageDialog(null, ex);
- 802. }
- 803. }
- 804.
- 805. private void createPDF() {
- 806. String path = "";
- 807. JFileChooser j = new JFileChooser();
- 808. j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- 809. int x = j.showSaveDialog(this);
- 810. if (x == JFileChooser.APPROVE_OPTION) {
- 811. path = j.getSelectedFile().getPath();
- 812. }
- 813.
- 814. Document doc = new Document(PageSize.A4.rotate());
- 815. try {
- 816. PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(path +".pdf"));
- 817. doc.open();
- 818. PdfPTable tbl = new PdfPTable(11);
- 819.
- 820. tbl.addCell("Index");
- 821. tbl.addCell("Age");
- 822. tbl.addCell("Surname");
- 823. tbl.addCell("Forename (s)");
- 824. tbl.addCell("Title");
- 825. tbl.addCell("Passport No.");
- 826. tbl.addCell("DOB");
- 827. tbl.addCell("Expiry Date");
- 828. tbl.addCell("Contact No.");
- 829. tbl.addCell("Inovice No.");
- 830. tbl.addCell("Reference");
- 831.
- 832. for (int i = 0; i < mainTable.getRowCount(); i++) {
- 833. String Index = mainTable.getValueAt(i, 0).toString();
- 834. String Age = mainTable.getValueAt(i, 1).toString();
- 835. String Surname = mainTable.getValueAt(i, 2).toString();
- 836. String Forename = mainTable.getValueAt(i, 3).toString();
- 837. String Title = mainTable.getValueAt(i, 4).toString();
- 838. String PassportNo = mainTable.getValueAt(i, 5).toString();
- 839. String DOB = mainTable.getValueAt(i, 6).toString();
- 840. String ExpiryDate = mainTable.getValueAt(i, 7).toString();
- 841. String ContactNo = mainTable.getValueAt(i, 8).toString();
- 842. String InvoiceNo = mainTable.getValueAt(i, 9).toString();
- 843. String Reference = mainTable.getValueAt(i, 10).toString();
- 844.
- 845. tbl.addCell(Index);
- 846. tbl.addCell(Age);
- 847. tbl.addCell(Surname);
- 848. tbl.addCell(Forename);
- 849. tbl.addCell(Title);
- 850. tbl.addCell(PassportNo);
- 851. tbl.addCell(DOB);
- 852. tbl.addCell(ExpiryDate);
- 853. tbl.addCell(ContactNo);
- 854. tbl.addCell(InvoiceNo);
- 855. tbl.addCell(Reference);
- 856. }
- 857. doc.add(tbl);
- 858. doc.close();
- 859. writer.close();
- 860. } catch (Exception ex) {
- 861. JOptionPane.showMessageDialog(null, ex);
- 862. }
- 863. }
- 864.
- 865. @Override
- 866. public void actionPerformed(ActionEvent ae) {
- 867. if (ae.getSource() == back) {
- 868. mainFrame frame = new mainFrame();
- 869. this.dispose();
- 870. } else if (ae.getSource() == addPilgrim) {
- 871. addPilgrim();
- 872. } else if (ae.getSource() == deletePilgrim) {
- 873. deletePilgrim();
- 874. } else if (ae.getSource() == updatePilgrim) {
- 875. updatePilgrim();
- 876. } else if (ae.getSource() == savePDF) {
- 877. createPDF();
- 878. }
- 879. }
- 880. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement