Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaresturentdesktopclient; import Entity.BillingTableEntity; import Entity.FoodTableEntity; import Entity.MealEntity; import TableModel.RestaurantTableModel; import Tools.Constant; import Tools.HttpClientUtil; import Tools.ServletRequest; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import java.awt.Dimension; import java.util.ArrayList; import java.util.List; import javax.swing.JOptionPane; import javafx.scene.control.ScrollPane; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JTable; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; /** * * @author siamzz */ public class PreviousBillInfo extends javax.swing.JFrame { final static int day[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }; final static Integer MonthId[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; final static Integer year[] = { 2015, 2014 }; Integer Month = 1; Integer Year = 2015; Integer Day = 1; /** * Creates new form PreviousBillInfo */ public BillingOptionSelect add; public PreviousBillInfo(BillingOptionSelect var) { initComponents(); add = var; } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jButton2 = new javax.swing.JButton(); jComboBox1 = new javax.swing.JComboBox(); jComboBox3 = new javax.swing.JComboBox(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jComboBox2 = new javax.swing.JComboBox(); jScrollPane2 = new javax.swing.JScrollPane(); jTableMealOrder = new javax.swing.JTable(); jLabel6 = new javax.swing.JLabel(); jTextFieldBillingId = new javax.swing.JTextField(); jButtonDetails = new javax.swing.JButton(); jLabel5 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setMinimumSize(new java.awt.Dimension(715, 560)); getContentPane().setLayout(null); jLabel1.setFont(new java.awt.Font("Viner Hand ITC", 1, 14)); // NOI18N jLabel1.setForeground(new java.awt.Color(153, 0, 0)); jLabel1.setText("YEAR"); getContentPane().add(jLabel1); jLabel1.setBounds(440, 130, 110, 40); jLabel2.setFont(new java.awt.Font("Viner Hand ITC", 1, 14)); // NOI18N jLabel2.setForeground(new java.awt.Color(153, 0, 0)); jLabel2.setText("MONTH"); getContentPane().add(jLabel2); jLabel2.setBounds(290, 130, 90, 30); jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BUTTON IMAGE/ADD BILL/DONE.png"))); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); getContentPane().add(jButton1); jButton1.setBounds(550, 180, 70, 30); jLabel3.setFont(new java.awt.Font("Viner Hand ITC", 1, 14)); // NOI18N jLabel3.setForeground(new java.awt.Color(153, 0, 0)); jLabel3.setText("DAY"); getContentPane().add(jLabel3); jLabel3.setBounds(160, 120, 80, 50); jLabel4.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N jLabel4.setForeground(new java.awt.Color(153, 0, 0)); jLabel4.setText("PLEASE ENTER DATE "); getContentPane().add(jLabel4); jLabel4.setBounds(230, 70, 240, 30); jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BUTTON IMAGE/left.png"))); // NOI18N jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); getContentPane().add(jButton2); jButton2.setBounds(510, 80, 50, 50); jComboBox1.setFont(new java.awt.Font("Viner Hand ITC", 1, 14)); // NOI18N jComboBox1.setForeground(new java.awt.Color(153, 0, 0)); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" })); jComboBox1.setToolTipText("SELECT THE MONTH YOU WANT TO KNOW"); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); getContentPane().add(jComboBox1); jComboBox1.setBounds(260, 180, 130, 30); jComboBox3.setFont(new java.awt.Font("Andalus", 1, 12)); // NOI18N jComboBox3.setForeground(new java.awt.Color(153, 0, 0)); jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2015", "2014" })); jComboBox3.setToolTipText("SELECT THE DAY YOU WANT TO KNOW"); jComboBox3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox3ActionPerformed(evt); } }); getContentPane().add(jComboBox3); jComboBox3.setBounds(410, 180, 120, 30); jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "BillingId", "Payment Date", "Payment Time", "Total" }) { boolean[] canEdit = new boolean[] { true, true, true, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jScrollPane1.setViewportView(jTable1); getContentPane().add(jScrollPane1); jScrollPane1.setBounds(110, 220, 470, 140); jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" })); jComboBox2.setToolTipText("SELECT THE DAY YOU WANT TO KNOW"); jComboBox2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox2ActionPerformed(evt); } }); getContentPane().add(jComboBox2); jComboBox2.setBounds(140, 180, 80, 30); jTableMealOrder.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Meal number", "Food number", "Quantity", "Price" })); jScrollPane2.setViewportView(jTableMealOrder); getContentPane().add(jScrollPane2); jScrollPane2.setBounds(110, 410, 470, 120); jLabel6.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N jLabel6.setForeground(new java.awt.Color(153, 0, 0)); jLabel6.setText("Billing Id"); getContentPane().add(jLabel6); jLabel6.setBounds(110, 370, 100, 30); jTextFieldBillingId.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextFieldBillingIdActionPerformed(evt); } }); getContentPane().add(jTextFieldBillingId); jTextFieldBillingId.setBounds(230, 370, 110, 20); jButtonDetails.setText("Details"); jButtonDetails.setToolTipText(""); jButtonDetails.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonDetailsActionPerformed(evt); } }); getContentPane().add(jButtonDetails); jButtonDetails.setBounds(370, 370, 100, 23); jLabel5.setIcon( new javax.swing.ImageIcon(getClass().getResource("/BUTTON IMAGE/background-1024x808 - Copy.jpg"))); // NOI18N jLabel5.setMaximumSize(new java.awt.Dimension(715, 560)); jLabel5.setMinimumSize(new java.awt.Dimension(715, 560)); jLabel5.setPreferredSize(new java.awt.Dimension(715, 560)); getContentPane().add(jLabel5); jLabel5.setBounds(0, -10, 700, 550); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String day; if (Day < 10) { day = "0" + Day.toString(); } else { day = Day.toString(); } String month; String year; String date; if (Month < 10) { date = Year.toString() + "/0" + Month.toString() + "/" + day; } else { date = Year.toString() + "/" + Month.toString() + "/" + day; } System.out.println("the date is " + date); int tabbedPanelID = Constant.PREV_BILLING_PANELID; /*Showing previous data*/ ServletRequest.setServletRequest(Constant.BILLING_SERVLET); List<NameValuePair> paramList = new ArrayList<>(); paramList.add(new BasicNameValuePair(Constant.KEY_COMMAND, Constant.KEY_PREV_BILLING)); paramList.add(new BasicNameValuePair(Constant.KEY_DATE, date)); // paramList.add(new BasicNameValuePair(Constant.KEY_MONTH, month)); String response = HttpClientUtil.postRequest(paramList); Gson gson = new GsonBuilder().create(); Object[][] objectArray = null; java.lang.reflect.Type collectionType = new TypeToken<List<BillingTableEntity>>() { }.getType(); List<BillingTableEntity> BillList = gson.fromJson(response, collectionType); objectArray = new Object[BillList.size()][]; int count = 0; for (BillingTableEntity prob : BillList) { Object[] obj = new Object[4]; obj[0] = prob.getTransaction_id(); obj[1] = prob.getPayment_date(); obj[2] = prob.getPayment_time(); obj[3] = prob.getTotal(); objectArray[count] = obj; count++; } RestaurantTableModel restaurantModel1 = new RestaurantTableModel(tabbedPanelID, objectArray); jTable1 = new JTable(restaurantModel1); jTable1.setPreferredScrollableViewportSize(new Dimension(600, 400)); jTable1.setFillsViewportHeight(true); jScrollPane1.setViewportView(jTable1); // TODO add your handling code here: }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: add.show(); this.dispose(); }//GEN-LAST:event_jButton2ActionPerformed private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed // TODO add your handling code here: int index; Integer index; index = ((JComboBox) evt.getSource()).getSelectedIndex(); Month = MonthId[index]; }//GEN-LAST:event_jComboBox1ActionPerformed private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed // TODO add your handling code here: Integer index; index = ((JComboBox) evt.getSource()).getSelectedIndex(); Year = year[index]; }//GEN-LAST:event_jComboBox3ActionPerformed private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed // TODO add your handling code here: Integer index; index = ((JComboBox) evt.getSource()).getSelectedIndex(); Day = day[index]; }//GEN-LAST:event_jComboBox2ActionPerformed private void jButtonDetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDetailsActionPerformed // TODO add your handling code here: String Billing = jTextFieldBillingId.getText(); System.out.println(Billing); ShowTable(Billing); }//GEN-LAST:event_jButtonDetailsActionPerformed private void jTextFieldBillingIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldBillingIdActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextFieldBillingIdActionPerformed public void ShowTable(String BillingId) { String billingId = BillingId; int tabbedPanelID = Constant.SHOW_MEAL_DETAILS_PANELID; //send request to servlet ServletRequest.setServletRequest(Constant.BILLING_SERVLET); List<NameValuePair> paramList = new ArrayList<>(); paramList.add(new BasicNameValuePair(Constant.KEY_COMMAND, Constant.KEY_MEAL_DETAILS)); paramList.add(new BasicNameValuePair(Constant.KEY_TRANSACTIONID, billingId)); System.out.println(billingId); String response = HttpClientUtil.postRequest(paramList); Gson gson = new GsonBuilder().create(); Object[][] objectArray = null; java.lang.reflect.Type collectionType = new TypeToken<List<MealEntity>>() { }.getType(); List<MealEntity> MealList = gson.fromJson(response, collectionType); objectArray = new Object[MealList.size()][]; int count = 0; for (MealEntity prob : MealList) { Object[] obj = new Object[7]; obj[0] = prob.getMealId(); obj[1] = prob.getFoodNo(); obj[2] = prob.getFoodName(); obj[3] = prob.getQuantity(); obj[4] = prob.getPrice(); objectArray[count] = obj; count++; } RestaurantTableModel restaurantModel1 = new RestaurantTableModel(tabbedPanelID, objectArray); jTableMealOrder = new JTable(restaurantModel1); jTableMealOrder.setPreferredScrollableViewportSize(new Dimension(600, 400)); jTableMealOrder.setFillsViewportHeight(true); jScrollPane2.setViewportView(jTableMealOrder); } /** * @param args the command line arguments */ // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButtonDetails; private javax.swing.JComboBox jComboBox1; private javax.swing.JComboBox jComboBox2; private javax.swing.JComboBox jComboBox3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable jTable1; private javax.swing.JTable jTableMealOrder; private javax.swing.JTextField jTextFieldBillingId; // End of variables declaration//GEN-END:variables }