Advertisement
Guest User

Multiple JPanels in JTabbedPane

a guest
Oct 13th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 40.98 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.table.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.event.ActionListener;
  6. import java.sql.*;
  7. import java.util.Vector;
  8.  
  9. public class Project_SalesDatabase extends JPanel {
  10.  
  11.     JTabbedPane tabbedPane = new JTabbedPane();
  12.     ImageIcon icon = createImageIcon("images/middle.gif");
  13.     JButton view = new JButton(" View ");
  14.     JButton save = new JButton(" Save ");
  15.     JButton addNew = new JButton(" Add New ");
  16.     JButton exit = new JButton(" Exit Application ");
  17.     JPanel displayBiscuitsPanel = new JPanel();
  18.     JPanel displayCookingPanel = new JPanel();
  19.     JPanel displayCustomersPanel = new JPanel();
  20.     JPanel displayEmployeesPanel = new JPanel();
  21.     JPanel displayProvisionsPanel = new JPanel();
  22.     JPanel displayButton = new JPanel();
  23.     JPanel displayContent = new JPanel(new GridLayout(0, 2));
  24.     JPanel displayTable = new JPanel(new GridLayout(1, 1));
  25.     JTextField biscuitName = new JTextField();
  26.     JTextField biscuitPrice = new JTextField();
  27.     JTextField biscuitCompany = new JTextField();
  28.     JTextField quantityOfBiscuitsBought = new JTextField();
  29.     JTextField quantityOfBiscuitsSold = new JTextField();
  30.     JTextField quantityInStock = new JTextField();
  31.     JTextField itemName = new JTextField();
  32.     JTextField itemPrice = new JTextField();
  33.     JTextField itemType = new JTextField();
  34.     JTextField quantityOfitemsBought = new JTextField();
  35.     JTextField quantityOfitemsSold = new JTextField();
  36.     JTextField firstName = new JTextField();
  37.     JTextField lastName = new JTextField();
  38.     JTextField customerAttendant = new JTextField();
  39.     JTextField customerAttendantPosition = new JTextField();
  40.     JTextField isCustomerADebtor = new JTextField();
  41.     JTextField orderNumber = new JTextField();
  42.     JTextField debtAmount = new JTextField();
  43.     JTextField address = new JTextField();
  44.     JTextField phoneNumber = new JTextField();
  45.     JTextField position = new JTextField();
  46.     JTextField age = new JTextField();
  47.     JTextField salary = new JTextField();
  48.     JTextField nextOfKin = new JTextField();
  49.     JTextField relationshipWithNextOfKin = new JTextField();
  50.     JTextField nextOfKinPhoneNumber = new JTextField();
  51.     JTextField itemCompany = new JTextField();
  52.  
  53.     public static void main(String[] args) {
  54.  
  55.         //Schedule a job for the event dispatch thread:
  56.         //creating and showing this application's GUI.
  57.         SwingUtilities.invokeLater(new Runnable() {
  58.             @Override
  59.             public void run() {
  60.                 //Turn off metal's use of bold fonts
  61.                 UIManager.put("swing.boldMetal", Boolean.FALSE);
  62.                 //Create and set up the window.
  63.                 JFrame frame = new JFrame("The Royal Ventures International®");
  64.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65.  
  66.                 //Add content to the window.
  67.                 frame.add(new Project_SalesDatabase(), BorderLayout.CENTER);
  68.  
  69.                 //Display the window.
  70.                 frame.setSize(1000, 500);
  71.                 frame.setLocationRelativeTo(null);
  72.                 frame.setVisible(true);
  73.             }
  74.         });
  75.     }
  76.  
  77.     public Project_SalesDatabase() {
  78.         super(new GridLayout(1, 1));
  79.         // Create the tab pages
  80.         biscuitsTable();
  81.         cookingIngredientsTable();
  82.         customersTable();
  83.         employeesTable();
  84.         provisionsTable();
  85.  
  86.         // Create tabs in tabbedPane
  87.         int index = tabbedPane.getTabCount();
  88.         System.out.println(index);
  89.         tabbedPane.addTab("Biscuits Database", icon, displayBiscuitsPanel,
  90.                 "Allows you to view or Enter Data into the Biscuits Database");
  91.         tabbedPane.addTab("Cooking Ingredients Database", icon, displayCookingPanel,
  92.                 "Allows you to view or Enter Data into the Cooking Ingredients Database");
  93.  
  94.         tabbedPane.addTab("Customers Database", icon, displayCustomersPanel,
  95.                 "Allows you to view or Enter Data into the Customers Database");
  96.  
  97.         tabbedPane.addTab("Employees Database", icon, displayEmployeesPanel,
  98.                 "Allows you to view or Enter Data into the Employees Database");
  99.  
  100.         tabbedPane.addTab("Provisions Database", icon, displayProvisionsPanel,
  101.                 "Allows you to view or Enter Data into the Provisions Database");
  102.         add(tabbedPane);
  103.         //Enable scrolling in tabs.
  104.         tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  105.     }
  106.  
  107.     public static ImageIcon createImageIcon(String path) {
  108.         java.net.URL imgURL = Project_SalesDatabase.class.getResource(path);
  109.         if (imgURL != null) {
  110.             return new ImageIcon(imgURL);
  111.         } else {
  112.             System.err.println("Couldn't find file: " + path);
  113.             return null;
  114.         }
  115.     }
  116.  
  117.     public final void biscuitsTable() {
  118.  
  119.         displayBiscuitsPanel.setLayout(new BorderLayout());
  120.  
  121.         displayBiscuitsPanel.add(displayContent, BorderLayout.CENTER);
  122.         displayBiscuitsPanel.add(displayButton, BorderLayout.SOUTH);
  123.         displayBiscuitsPanel.add(displayTable, BorderLayout.NORTH);
  124.  
  125.         displayContent.add(new JLabel("Biscuit Name"));
  126.         displayContent.add(biscuitName);
  127.         displayContent.add(new JLabel("Biscuit Price"));
  128.         displayContent.add(biscuitPrice);
  129.         displayContent.add(new JLabel("Biscuit Company"));
  130.         displayContent.add(biscuitCompany);
  131.         displayContent.add(new JLabel("Quantity Of Biscuits Bought"));
  132.         displayContent.add(quantityOfBiscuitsBought);
  133.         displayContent.add(new JLabel("Quantity Of Biscuits Sold"));
  134.         displayContent.add(quantityOfBiscuitsSold);
  135.         displayContent.add(new JLabel("Quantity In Stock"));
  136.         displayContent.add(quantityInStock);
  137.  
  138.         displayButton.add(addNew);
  139.         displayButton.add(save);
  140.         displayButton.add(view);
  141.         displayButton.add(exit);
  142.  
  143.         view.addActionListener(new ActionListener() {
  144.             @Override
  145.             public void actionPerformed(ActionEvent e) {
  146.  
  147.                 String db_url = "jdbc:odbc:ProjectSalesDatabase";
  148.                 String username = "";
  149.                 String password = "";
  150.                 Connection con = null;
  151.  
  152.                 try {
  153.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  154.                     con = DriverManager.getConnection(db_url, username, password);
  155.                 } catch (ClassNotFoundException | SQLException f) {
  156.                     JOptionPane.showMessageDialog(null, f.getMessage());
  157.                 }
  158.  
  159.                 Statement state = null;
  160.                 ResultSet set = null;
  161.  
  162.                 try {
  163.  
  164.                     String Query = "SELECT * FROM Biscuits";
  165.                     state = con.createStatement();
  166.                     set = state.executeQuery(Query);
  167.  
  168.                     boolean nextrec = set.next();
  169.                     if (!nextrec) {
  170.                         JOptionPane.showMessageDialog(null, "No Record");
  171.                     } else {
  172.                         Vector col = new Vector();
  173.                         Vector row = new Vector();
  174.  
  175.                         ResultSetMetaData rsm = set.getMetaData();
  176.  
  177.                         for (int x = 1; x <= rsm.getColumnCount(); x++) {
  178.                             col.addElement(rsm.getColumnName(x));
  179.                         }
  180.                         do {
  181.                             row.addElement(getNextRow(set, rsm));
  182.                         } while (set.next());
  183.  
  184.                         JTable tab = new JTable(row, col);
  185.                         displayContent.removeAll();
  186.                         displayTable.removeAll();
  187.                         displayTable.add(new JScrollPane(tab), BorderLayout.CENTER);
  188.  
  189.                         validate();
  190.                     }
  191.                     state.close();
  192.                 } catch (SQLException sql) {
  193.                     JOptionPane.showMessageDialog(null, sql.getMessage());
  194.                 }
  195.             }
  196.         });
  197.  
  198.         save.addActionListener(new ActionListener() {
  199.             @Override
  200.             public void actionPerformed(ActionEvent e) {
  201.  
  202.                 try {
  203.                     String Query = "INSERT INTO Biscuits VALUES ('" + biscuitName.getText() + "',"
  204.                             + "          '" + biscuitPrice.getText() + "',"
  205.                             + "          '" + biscuitCompany.getText() + "',"
  206.                             + "          '" + quantityOfBiscuitsBought.getText() + "',"
  207.                             + "          '" + quantityOfBiscuitsSold.getText() + "',"
  208.                             + "          '" + quantityInStock.getText() + "')";
  209.  
  210.                     String db_url = "jdbc:odbc:ProjectSalesDatabase";
  211.                     String username = "";
  212.                     String password = "";
  213.                     Connection con = null;
  214.                     try {
  215.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  216.                         con = DriverManager.getConnection(db_url, username, password);
  217.                     } catch (ClassNotFoundException | SQLException f) {
  218.  
  219.                         JOptionPane.showMessageDialog(null, f.getMessage());
  220.                     }
  221.                     Statement state = con.createStatement();
  222.                     int rep = state.executeUpdate(Query);
  223.                     if (rep == 0) {
  224.                         JOptionPane.showMessageDialog(null, "No Data Saved");
  225.                     } else {
  226.                         JOptionPane.showMessageDialog(null, "Data Saved");
  227.                     }
  228.                 } catch (SQLException sqle) {
  229.                     sqle.getMessage();
  230.                 }
  231.             }
  232.         });
  233.  
  234.         addNew.addActionListener(new ActionListener() {
  235.             @Override
  236.             public void actionPerformed(ActionEvent e) {
  237.  
  238.                 try {
  239.  
  240.                     biscuitName.setText("");
  241.                     biscuitPrice.setText("");
  242.                     biscuitCompany.setText("");
  243.                     quantityOfBiscuitsBought.setText("");
  244.                     quantityOfBiscuitsSold.setText("");
  245.                     quantityInStock.setText("");
  246.  
  247.                     displayTable.removeAll();
  248.                     displayContent.removeAll();
  249.  
  250.                     displayContent.add(new JLabel("Biscuit Name"));
  251.                     displayContent.add(biscuitName);
  252.                     displayContent.add(new JLabel("Biscuit Price"));
  253.                     displayContent.add(biscuitPrice);
  254.                     displayContent.add(new JLabel("Biscuit Company"));
  255.                     displayContent.add(biscuitCompany);
  256.                     displayContent.add(new JLabel("Quantity Of Biscuits Bought"));
  257.                     displayContent.add(quantityOfBiscuitsBought);
  258.                     displayContent.add(new JLabel("Quantity Of Biscuits Sold"));
  259.                     displayContent.add(quantityOfBiscuitsSold);
  260.                     displayContent.add(new JLabel("Quantity In Stock"));
  261.                     displayContent.add(quantityInStock);
  262.  
  263.                     validate();
  264.  
  265.                 } catch (Exception f) {
  266.                     f.getMessage();
  267.                 }
  268.             }
  269.         });
  270.  
  271.         exit.addActionListener(new ActionListener() {
  272.             @Override
  273.             public void actionPerformed(ActionEvent e) {
  274.                 System.exit(0);
  275.             }
  276.         });
  277.     }
  278.  
  279.     public final void cookingIngredientsTable() {
  280.  
  281.         displayCookingPanel.setLayout(new BorderLayout());
  282.  
  283.         displayCookingPanel.add(displayContent, BorderLayout.CENTER);
  284.         displayCookingPanel.add(displayButton, BorderLayout.SOUTH);
  285.         displayCookingPanel.add(displayTable, BorderLayout.NORTH);
  286.  
  287.         displayContent.add(new JLabel("Item Name"));
  288.         displayContent.add(itemName);
  289.         displayContent.add(new JLabel("Item Price"));
  290.         displayContent.add(itemPrice);
  291.         displayContent.add(new JLabel("Item Type"));
  292.         displayContent.add(itemType);
  293.         displayContent.add(new JLabel("Quantity Of Items Bought"));
  294.         displayContent.add(quantityOfitemsBought);
  295.         displayContent.add(new JLabel("Quantity Of Items Sold"));
  296.         displayContent.add(quantityOfitemsSold);
  297.         displayContent.add(new JLabel("Quantity In Stock"));
  298.         displayContent.add(quantityInStock);
  299.  
  300.         displayButton.add(addNew);
  301.         displayButton.add(save);
  302.         displayButton.add(view);
  303.         displayButton.add(exit);
  304.  
  305.         view.addActionListener(new ActionListener() {
  306.             @Override
  307.             public void actionPerformed(ActionEvent e) {
  308.                 String db_url = "jdbc:odbc:ProjectSalesDatabase";
  309.                 String username = "";
  310.                 String password = "";
  311.                 Connection con = null;
  312.                 try {
  313.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  314.                     con = DriverManager.getConnection(db_url, username, password);
  315.                 } catch (ClassNotFoundException | SQLException f) {
  316.                     JOptionPane.showMessageDialog(null, f.getMessage());
  317.                 }
  318.  
  319.                 Statement state = null;
  320.                 ResultSet set = null;
  321.  
  322.                 try {
  323.  
  324.                     String Query = "SELECT * FROM CookingIngredients";
  325.                     state = con.createStatement();
  326.                     set = state.executeQuery(Query);
  327.  
  328.                     boolean nextrec = set.next();
  329.                     if (!nextrec) {
  330.                         JOptionPane.showMessageDialog(null, "No Record");
  331.                     } else {
  332.                         Vector col = new Vector();
  333.                         Vector row = new Vector();
  334.  
  335.                         ResultSetMetaData rsm = set.getMetaData();
  336.  
  337.                         for (int x = 1; x <= rsm.getColumnCount(); x++) {
  338.                             col.addElement(rsm.getColumnName(x));
  339.                         }
  340.                         do {
  341.                             row.addElement(getNextRow(set, rsm));
  342.                         } while (set.next());
  343.                         JTable tab = new JTable(row, col);
  344.                         displayContent.removeAll();
  345.                         displayTable.removeAll();
  346.                         displayTable.add(new JScrollPane(tab), BorderLayout.CENTER);
  347.                         validate();
  348.                     }
  349.                     state.close();
  350.  
  351.                 } catch (SQLException sql) {
  352.                     JOptionPane.showMessageDialog(null, sql.getMessage());
  353.                 }
  354.             }
  355.         });
  356.  
  357.         save.addActionListener(new ActionListener() {
  358.             @Override
  359.             public void actionPerformed(ActionEvent e) {
  360.  
  361.                 try {
  362.                     String Query = "INSERT INTO CookingIngredients VALUES ('" + itemName.getText() + "',"
  363.                             + "          '" + itemPrice.getText() + "',"
  364.                             + "          '" + itemType.getText() + "',"
  365.                             + "          '" + quantityOfitemsBought.getText() + "',"
  366.                             + "          '" + quantityOfitemsSold.getText() + "',"
  367.                             + "          '" + quantityInStock.getText() + "')";
  368.  
  369.                     String db_url = "jdbc:odbc:ProjectSalesDatabase";
  370.                     String username = "";
  371.                     String password = "";
  372.                     Connection con = null;
  373.                     try {
  374.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  375.                         con = DriverManager.getConnection(db_url, username, password);
  376.                     } catch (ClassNotFoundException | SQLException f) {
  377.                         JOptionPane.showMessageDialog(null, f.getMessage());
  378.                     }
  379.                     Statement state = con.createStatement();
  380.                     int rep = state.executeUpdate(Query);
  381.                     if (rep == 0) {
  382.                         JOptionPane.showMessageDialog(null, "No Data Saved");
  383.                     } else {
  384.                         JOptionPane.showMessageDialog(null, "Data Saved");
  385.                     }
  386.                 } catch (SQLException sqle) {
  387.                     sqle.getMessage();
  388.                 }
  389.             }
  390.         });
  391.  
  392.         addNew.addActionListener(new ActionListener() {
  393.             @Override
  394.             public void actionPerformed(ActionEvent e) {
  395.  
  396.                 try {
  397.  
  398.                     itemName.setText("");
  399.                     itemPrice.setText("");
  400.                     itemType.setText("");
  401.                     quantityOfitemsBought.setText("");
  402.                     quantityOfBiscuitsSold.setText("");
  403.                     quantityOfitemsSold.setText("");
  404.                     quantityInStock.setText("");
  405.  
  406.                     displayTable.removeAll();
  407.                     displayContent.removeAll();
  408.  
  409.                     displayContent.add(new JLabel("Item Name"));
  410.                     displayContent.add(itemName);
  411.                     displayContent.add(new JLabel("Item Price"));
  412.                     displayContent.add(itemPrice);
  413.                     displayContent.add(new JLabel("Item Type"));
  414.                     displayContent.add(itemType);
  415.                     displayContent.add(new JLabel("Quantity Of Items Bought"));
  416.                     displayContent.add(quantityOfitemsBought);
  417.                     displayContent.add(new JLabel("Quantity Of Items Sold"));
  418.                     displayContent.add(quantityOfitemsSold);
  419.                     displayContent.add(new JLabel("Quantity In Stock"));
  420.                     displayContent.add(quantityInStock);
  421.  
  422.                     validate();
  423.  
  424.                 } catch (Exception f) {
  425.                     f.getMessage();
  426.                 }
  427.             }
  428.         });
  429.  
  430.         exit.addActionListener(new ActionListener() {
  431.             @Override
  432.             public void actionPerformed(ActionEvent e) {
  433.                 System.exit(0);
  434.             }
  435.         });
  436.     }
  437.  
  438.     public final void customersTable() {
  439.  
  440.         displayCustomersPanel.setLayout(new BorderLayout());
  441.  
  442.         displayCustomersPanel.add(displayContent, BorderLayout.CENTER);
  443.         displayCustomersPanel.add(displayButton, BorderLayout.SOUTH);
  444.         displayCustomersPanel.add(displayTable, BorderLayout.NORTH);
  445.  
  446.         displayContent.add(new JLabel("First Name"));
  447.         displayContent.add(firstName);
  448.         displayContent.add(new JLabel("Last Name"));
  449.         displayContent.add(lastName);
  450.         displayContent.add(new JLabel("Customer Attendant"));
  451.         displayContent.add(customerAttendant);
  452.         displayContent.add(new JLabel("Customer Attendant's Position"));
  453.         displayContent.add(customerAttendantPosition);
  454.         displayContent.add(new JLabel("Is This Customer A Debtor?"));
  455.         displayContent.add(isCustomerADebtor);
  456.         displayContent.add(new JLabel("Order Number"));
  457.         displayContent.add(orderNumber);
  458.         displayContent.add(new JLabel("Debt Amount"));
  459.         displayContent.add(debtAmount);
  460.         displayContent.add(new JLabel("Customer's Address"));
  461.         displayContent.add(address);
  462.         displayContent.add(new JLabel("Customer's Phone Number"));
  463.         displayContent.add(phoneNumber);
  464.  
  465.         displayButton.add(addNew);
  466.         displayButton.add(save);
  467.         displayButton.add(view);
  468.         displayButton.add(exit);
  469.  
  470.         view.addActionListener(new ActionListener() {
  471.             @Override
  472.             public void actionPerformed(ActionEvent e) {
  473.                 String db_url = "jdbc:odbc:ProjectSalesDatabase";
  474.                 String username = "";
  475.                 String password = "";
  476.                 Connection con = null;
  477.                 try {
  478.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  479.                     con = DriverManager.getConnection(db_url, username, password);
  480.                 } catch (ClassNotFoundException | SQLException f) {
  481.                     JOptionPane.showMessageDialog(null, f.getMessage());
  482.                 }
  483.  
  484.                 Statement state = null;
  485.                 ResultSet set = null;
  486.  
  487.                 try {
  488.  
  489.                     String Query = "SELECT * FROM CustomersNames";
  490.                     state = con.createStatement();
  491.                     set = state.executeQuery(Query);
  492.  
  493.                     boolean nextrec = set.next();
  494.                     if (!nextrec) {
  495.                         JOptionPane.showMessageDialog(null, "No Record");
  496.                     } else {
  497.                         Vector col = new Vector();
  498.                         Vector row = new Vector();
  499.  
  500.                         ResultSetMetaData rsm = set.getMetaData();
  501.  
  502.                         for (int x = 1; x <= rsm.getColumnCount(); x++) {
  503.                             col.addElement(rsm.getColumnName(x));
  504.                         }
  505.                         do {
  506.                             row.addElement(getNextRow(set, rsm));
  507.                         } while (set.next());
  508.  
  509.                         JTable tab = new JTable(row, col);
  510.                         displayContent.removeAll();
  511.                         displayTable.removeAll();
  512.                         displayTable.add(new JScrollPane(tab), BorderLayout.CENTER);
  513.  
  514.                         validate();
  515.                     }
  516.                     state.close();
  517.                 } catch (SQLException sql) {
  518.                     JOptionPane.showMessageDialog(null, sql.getMessage());
  519.                 }
  520.             }
  521.         });
  522.  
  523.         save.addActionListener(new ActionListener() {
  524.             @Override
  525.             public void actionPerformed(ActionEvent e) {
  526.  
  527.                 try {
  528.                     String Query = "INSERT INTO CustomersNames VALUES ('" + firstName.getText() + "',"
  529.                             + "          '" + lastName.getText() + "',"
  530.                             + "          '" + customerAttendant.getText() + "')"
  531.                             + "          '" + customerAttendantPosition.getText() + "')"
  532.                             + "          '" + isCustomerADebtor.getText() + "')"
  533.                             + "          '" + orderNumber.getText() + "')"
  534.                             + "          '" + debtAmount.getText() + "')"
  535.                             + "          '" + address.getText() + "')"
  536.                             + "          '" + phoneNumber.getText() + "')";
  537.  
  538.                     String db_url = "jdbc:odbc:ProjectSalesDatabase";
  539.                     String username = "";
  540.                     String password = "";
  541.                     Connection con = null;
  542.                     try {
  543.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  544.                         con = DriverManager.getConnection(db_url, username, password);
  545.                     } catch (ClassNotFoundException | SQLException f) {
  546.                         JOptionPane.showMessageDialog(null, f.getMessage());
  547.                     }
  548.                     Statement state = con.createStatement();
  549.                     int rep = state.executeUpdate(Query);
  550.                     if (rep == 0) {
  551.                         JOptionPane.showMessageDialog(null, "No Data Saved");
  552.                     } else {
  553.                         JOptionPane.showMessageDialog(null, "Data Saved");
  554.                     }
  555.                 } catch (SQLException sqle) {
  556.                     sqle.getMessage();
  557.                 }
  558.             }
  559.         });
  560.  
  561.         addNew.addActionListener(new ActionListener() {
  562.             @Override
  563.             public void actionPerformed(ActionEvent e) {
  564.  
  565.                 try {
  566.  
  567.                     firstName.setText("");
  568.                     lastName.setText("");
  569.                     customerAttendant.setText("");
  570.                     customerAttendantPosition.setText("");
  571.                     isCustomerADebtor.setText("");
  572.                     orderNumber.setText("");
  573.                     debtAmount.setText("");
  574.                     address.setText("");
  575.                     phoneNumber.setText("");
  576.  
  577.                     displayTable.removeAll();
  578.                     displayContent.removeAll();
  579.  
  580.                     displayContent.add(new JLabel("First Name"));
  581.                     displayContent.add(firstName);
  582.                     displayContent.add(new JLabel("Last Name"));
  583.                     displayContent.add(lastName);
  584.                     displayContent.add(new JLabel("Customer Attendant"));
  585.                     displayContent.add(customerAttendant);
  586.                     displayContent.add(new JLabel("Customer Attendant's Position"));
  587.                     displayContent.add(customerAttendantPosition);
  588.                     displayContent.add(new JLabel("Is This Customer A Debtor?"));
  589.                     displayContent.add(isCustomerADebtor);
  590.                     displayContent.add(new JLabel("Order Number"));
  591.                     displayContent.add(orderNumber);
  592.                     displayContent.add(new JLabel("Debt Amount"));
  593.                     displayContent.add(debtAmount);
  594.                     displayContent.add(new JLabel("Customer's Address"));
  595.                     displayContent.add(address);
  596.                     displayContent.add(new JLabel("Customer's Phone Number"));
  597.                     displayContent.add(phoneNumber);
  598.  
  599.                     validate();
  600.  
  601.                 } catch (Exception f) {
  602.                     f.getMessage();
  603.                 }
  604.             }
  605.         });
  606.  
  607.         exit.addActionListener(new ActionListener() {
  608.             @Override
  609.             public void actionPerformed(ActionEvent e) {
  610.                 System.exit(0);
  611.             }
  612.         });
  613.     }
  614.  
  615.     public final void employeesTable() {
  616.  
  617.         displayEmployeesPanel.setLayout(new BorderLayout());
  618.  
  619.         displayEmployeesPanel.add(displayContent, BorderLayout.CENTER);
  620.         displayEmployeesPanel.add(displayButton, BorderLayout.SOUTH);
  621.         displayEmployeesPanel.add(displayTable, BorderLayout.NORTH);
  622.  
  623.         displayContent.add(new JLabel("First Name"));
  624.         displayContent.add(firstName);
  625.         displayContent.add(new JLabel("Last Name"));
  626.         displayContent.add(lastName);
  627.         displayContent.add(new JLabel("Position"));
  628.         displayContent.add(position);
  629.         displayContent.add(new JLabel("Age"));
  630.         displayContent.add(age);
  631.         displayContent.add(new JLabel("Salary"));
  632.         displayContent.add(salary);
  633.         displayContent.add(new JLabel("Employees's Address"));
  634.         displayContent.add(address);
  635.         displayContent.add(new JLabel("Employees's Phone Number(s)"));
  636.         displayContent.add(phoneNumber);
  637.         displayContent.add(new JLabel("Next of Kin"));
  638.         displayContent.add(nextOfKin);
  639.         displayContent.add(new JLabel("Relationship With Next of Kin"));
  640.         displayContent.add(relationshipWithNextOfKin);
  641.         displayContent.add(new JLabel("Next of Kin's Phone Number(s)"));
  642.         displayContent.add(nextOfKinPhoneNumber);
  643.  
  644.         displayButton.add(addNew);
  645.         displayButton.add(save);
  646.         displayButton.add(view);
  647.         displayButton.add(exit);
  648.  
  649.         view.addActionListener(new ActionListener() {
  650.             @Override
  651.             public void actionPerformed(ActionEvent e) {
  652.                 String db_url = "jdbc:odbc:ProjectSalesDatabase";
  653.                 String username = "Employer";
  654.                 String password = "employer";
  655.                 Connection con = null;
  656.                 try {
  657.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  658.                     con = DriverManager.getConnection(db_url, username, password);
  659.                 } catch (ClassNotFoundException | SQLException f) {
  660.                     JOptionPane.showMessageDialog(null, f.getMessage());
  661.                 }
  662.  
  663.                 Statement state = null;
  664.                 ResultSet set = null;
  665.  
  666.                 try {
  667.  
  668.                     String Query = "SELECT * FROM Employees";
  669.                     state = con.createStatement();
  670.                     set = state.executeQuery(Query);
  671.  
  672.                     boolean nextrec = set.next();
  673.                     if (!nextrec) {
  674.                         JOptionPane.showMessageDialog(null, "No Record");
  675.                     } else {
  676.                         Vector col = new Vector();
  677.                         Vector row = new Vector();
  678.  
  679.                         ResultSetMetaData rsm = set.getMetaData();
  680.  
  681.                         for (int x = 1; x <= rsm.getColumnCount(); x++) {
  682.                             col.addElement(rsm.getColumnName(x));
  683.                         }
  684.                         do {
  685.                             row.addElement(getNextRow(set, rsm));
  686.                         } while (set.next());
  687.  
  688.                         JTable tab = new JTable(row, col);
  689.                         displayContent.removeAll();
  690.                         displayTable.removeAll();
  691.                         displayTable.add(new JScrollPane(tab), BorderLayout.CENTER);
  692.                         for (int i = 0; i < 10; i++) {
  693.                             TableColumn column = null;
  694.     column = tab.getColumnModel().getColumn(i);
  695.     if (i == 5) {
  696.         column.setPreferredWidth(300); //third column is bigger
  697.     } else {
  698.         column.setPreferredWidth(50);
  699.     }
  700. }
  701.  
  702.                         validate();
  703.                     }
  704.                     state.close();
  705.                 } catch (SQLException sql) {
  706.                     JOptionPane.showMessageDialog(null, sql.getMessage());
  707.                 }
  708.             }
  709.         });
  710.  
  711.         save.addActionListener(new ActionListener() {
  712.             @Override
  713.             public void actionPerformed(ActionEvent e) {
  714.  
  715.                 try {
  716.                     String Query = "INSERT INTO Employees VALUES ('" + firstName.getText() + "',"
  717.                             + "          '" + lastName.getText() + "',"
  718.                             + "          '" + position.getText() + "',"
  719.                             + "          '" + age.getText() + "',"
  720.                             + "          '" + salary.getText() + "',"
  721.                             + "          '" + address.getText() + "',"
  722.                             + "          '" + phoneNumber.getText() + "',"
  723.                             + "          '" + nextOfKin.getText() + "',"
  724.                             + "          '" + relationshipWithNextOfKin.getText() + "',"
  725.                             + "          '" + nextOfKinPhoneNumber.getText() + "')";
  726.  
  727.                     String db_url = "jdbc:odbc:ProjectSalesDatabase";
  728.                     String username = "";
  729.                     String password = "";
  730.                     Connection con = null;
  731.  
  732.                     try {
  733.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  734.                         con = DriverManager.getConnection(db_url, username, password);
  735.                     } catch (ClassNotFoundException | SQLException f) {
  736.                         JOptionPane.showMessageDialog(null, f.getMessage());
  737.                     }
  738.                     Statement state = con.createStatement();
  739.                     int rep = state.executeUpdate(Query);
  740.                     if (rep == 0) {
  741.                         JOptionPane.showMessageDialog(null, "No Data Saved");
  742.                     } else {
  743.                         JOptionPane.showMessageDialog(null, "Data Saved");
  744.                     }
  745.                 } catch (SQLException sqle) {
  746.                     sqle.getMessage();
  747.                 }
  748.             }
  749.         });
  750.  
  751.         addNew.addActionListener(new ActionListener() {
  752.             @Override
  753.             public void actionPerformed(ActionEvent e) {
  754.  
  755.                 try {
  756.  
  757.                     firstName.setText("");
  758.                     lastName.setText("");
  759.                     position.setText("");
  760.                     age.setText("");
  761.                     salary.setText("");
  762.                     address.setText("");
  763.                     phoneNumber.setText("");
  764.                     nextOfKin.setText("");
  765.                     relationshipWithNextOfKin.setText("");
  766.                     nextOfKinPhoneNumber.setText("");
  767.  
  768.                     displayTable.removeAll();
  769.                     displayContent.removeAll();
  770.  
  771.                     displayContent.add(new JLabel("First Name"));
  772.                     displayContent.add(firstName);
  773.                     displayContent.add(new JLabel("Last Name"));
  774.                     displayContent.add(lastName);
  775.                     displayContent.add(new JLabel("Position"));
  776.                     displayContent.add(position);
  777.                     displayContent.add(new JLabel("Age"));
  778.                     displayContent.add(age);
  779.                     displayContent.add(new JLabel("Salary"));
  780.                     displayContent.add(salary);
  781.                     displayContent.add(new JLabel("Employees's Address"));
  782.                     displayContent.add(address);
  783.                     displayContent.add(new JLabel("Employees's Phone Number(s)"));
  784.                     displayContent.add(phoneNumber);
  785.                     displayContent.add(new JLabel("Next of Kin"));
  786.                     displayContent.add(nextOfKin);
  787.                     displayContent.add(new JLabel("Relationship With Next of Kin"));
  788.                     displayContent.add(relationshipWithNextOfKin);
  789.                     displayContent.add(new JLabel("Next of Kin's Phone Number(s)"));
  790.                     displayContent.add(nextOfKinPhoneNumber);
  791.  
  792.                     validate();
  793.  
  794.                 } catch (Exception f) {
  795.                     f.getMessage();
  796.                 }
  797.             }
  798.         });
  799.  
  800.         exit.addActionListener(new ActionListener() {
  801.             @Override
  802.             public void actionPerformed(ActionEvent e) {
  803.                 System.exit(0);
  804.             }
  805.         });
  806.     }
  807.  
  808.     public final void provisionsTable() {
  809.  
  810.         displayProvisionsPanel.setLayout(new BorderLayout());
  811.  
  812.         displayProvisionsPanel.add(displayContent, BorderLayout.CENTER);
  813.         displayProvisionsPanel.add(displayButton, BorderLayout.SOUTH);
  814.         displayProvisionsPanel.add(displayTable, BorderLayout.NORTH);
  815.  
  816.         displayContent.add(new JLabel("Item Name"));
  817.         displayContent.add(itemName);
  818.         displayContent.add(new JLabel("Item Price"));
  819.         displayContent.add(itemPrice);
  820.         displayContent.add(new JLabel("Item Company"));
  821.         displayContent.add(itemCompany);
  822.         displayContent.add(new JLabel("Quantity Of Items Bought"));
  823.         displayContent.add(quantityOfitemsBought);
  824.         displayContent.add(new JLabel("Quantity Of Items Sold"));
  825.         displayContent.add(quantityOfitemsSold);
  826.         displayContent.add(new JLabel("Quantity In Stock"));
  827.         displayContent.add(quantityInStock);
  828.  
  829.         displayButton.add(addNew);
  830.         displayButton.add(save);
  831.         displayButton.add(view);
  832.         displayButton.add(exit);
  833.  
  834.         view.addActionListener(new ActionListener() {
  835.             @Override
  836.             public void actionPerformed(ActionEvent e) {
  837.                 String db_url = "jdbc:odbc:ProjectSalesDatabase";
  838.                 String username = "";
  839.                 String password = "";
  840.                 Connection con = null;
  841.                 try {
  842.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  843.                     con = DriverManager.getConnection(db_url, username, password);
  844.                 } catch (ClassNotFoundException | SQLException f) {
  845.                     JOptionPane.showMessageDialog(null, f.getMessage());
  846.                 }
  847.  
  848.                 Statement state = null;
  849.                 ResultSet set = null;
  850.  
  851.                 try {
  852.  
  853.                     String Query = "SELECT * FROM Provisions";
  854.                     state = con.createStatement();
  855.                     set = state.executeQuery(Query);
  856.  
  857.                     boolean nextrec = set.next();
  858.                     if (!nextrec) {
  859.                         JOptionPane.showMessageDialog(null, "No Record");
  860.                     } else {
  861.                         Vector col = new Vector();
  862.                         Vector row = new Vector();
  863.  
  864.                         ResultSetMetaData rsm = set.getMetaData();
  865.  
  866.                         for (int x = 1; x <= rsm.getColumnCount(); x++) {
  867.                             col.addElement(rsm.getColumnName(x));
  868.                         }
  869.                         do {
  870.                             row.addElement(getNextRow(set, rsm));
  871.                         } while (set.next());
  872.  
  873.                         JTable tab = new JTable(row, col);
  874.                         displayContent.removeAll();
  875.                         displayTable.removeAll();
  876.                         displayTable.add(new JScrollPane(tab), BorderLayout.CENTER);
  877.  
  878.                         validate();
  879.                     }
  880.                     state.close();
  881.                 } catch (SQLException sql) {
  882.                     JOptionPane.showMessageDialog(null, sql.getMessage());
  883.                 }
  884.             }
  885.         });
  886.  
  887.         save.addActionListener(new ActionListener() {
  888.             @Override
  889.             public void actionPerformed(ActionEvent e) {
  890.  
  891.                 try {
  892.                     String Query = "INSERT INTO Provisions VALUES ('" + itemName.getText() + "',"
  893.                             + "          '" + itemPrice.getText() + "',"
  894.                             + "          '" + itemCompany.getText() + "',"
  895.                             + "          '" + quantityOfitemsBought.getText() + "',"
  896.                             + "          '" + quantityOfitemsSold.getText() + "',"
  897.                             + "          '" + quantityInStock.getText() + "')";
  898.  
  899.                     String db_url = "jdbc:odbc:ProjectSalesDatabase";
  900.                     String username = "";
  901.                     String password = "";
  902.                     Connection con = null;
  903.                     try {
  904.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  905.                         con = DriverManager.getConnection(db_url, username, password);
  906.                     } catch (ClassNotFoundException | SQLException f) {
  907.                         JOptionPane.showMessageDialog(null, f.getMessage());
  908.                     }
  909.                     Statement state = con.createStatement();
  910.                     int rep = state.executeUpdate(Query);
  911.                     if (rep == 0) {
  912.                         JOptionPane.showMessageDialog(null, "No Data Saved");
  913.                     } else {
  914.                         JOptionPane.showMessageDialog(null, "Data Saved");
  915.                     }
  916.                 } catch (SQLException sqle) {
  917.                     sqle.getMessage();
  918.                 }
  919.             }
  920.         });
  921.  
  922.         addNew.addActionListener(new ActionListener() {
  923.             @Override
  924.             public void actionPerformed(ActionEvent e) {
  925.  
  926.                 try {
  927.  
  928.                     itemName.setText("");
  929.                     itemPrice.setText("");
  930.                     itemCompany.setText("");
  931.                     quantityOfitemsBought.setText("");
  932.                     quantityOfitemsSold.setText("");
  933.                     quantityInStock.setText("");
  934.  
  935.                     displayTable.removeAll();
  936.                     displayContent.removeAll();
  937.  
  938.                     displayContent.add(new JLabel("Item Name"));
  939.                     displayContent.add(itemName);
  940.                     displayContent.add(new JLabel("Item Price"));
  941.                     displayContent.add(itemPrice);
  942.                     displayContent.add(new JLabel("Item Company"));
  943.                     displayContent.add(itemCompany);
  944.                     displayContent.add(new JLabel("Quantity Of Items Bought"));
  945.                     displayContent.add(quantityOfitemsBought);
  946.                     displayContent.add(new JLabel("Quantity Of Items Sold"));
  947.                     displayContent.add(quantityOfitemsSold);
  948.                     displayContent.add(new JLabel("Quantity In Stock"));
  949.                     displayContent.add(quantityInStock);
  950.  
  951.                     validate();
  952.  
  953.                 } catch (Exception f) {
  954.                     f.getMessage();
  955.                 }
  956.             }
  957.         });
  958.  
  959.         exit.addActionListener(new ActionListener() {
  960.             @Override
  961.             public void actionPerformed(ActionEvent e) {
  962.                 System.exit(0);
  963.             }
  964.         });
  965.     }
  966.  
  967.     Vector getNextRow(ResultSet set, ResultSetMetaData rsm) {
  968.         Vector currentRow = new Vector();
  969.         try {
  970.             for (int x = 1; x <= rsm.getColumnCount(); x++) {
  971.                 switch (rsm.getColumnType(x)) {
  972.                     case Types.VARCHAR:
  973.                         currentRow.addElement(set.getString(x));
  974.                         break;
  975.                     case Types.INTEGER:
  976.                         currentRow.addElement(new Long(set.getLong(x)));
  977.                         break;
  978.                     default:
  979.                         System.out.println("No column type known");
  980.                         break;
  981.                 }
  982.             }
  983.         } catch (SQLException sqle) {
  984.             sqle.getMessage();
  985.         }
  986.         return currentRow;
  987.     }
  988. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement