Java tutorial
package govt_import_export; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; import com.mongodb.MongoException; import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.Int; import java.net.UnknownHostException; import java.util.ArrayList; import javax.swing.JLabel; import javax.swing.table.DefaultTableModel; /* * 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. */ /** * * @author Nitesh */ public class display_2 extends javax.swing.JFrame { /** * Creates new form display */ int total; DefaultTableModel model; public display_2() { initComponents(); jComboBox1.setEnabled(false); jComboBox2.setEnabled(false); jComboBox3.setEnabled(false); model = new DefaultTableModel(); jTable1.setModel(model); model.addColumn("Manufacture"); model.addColumn("Model"); model.addColumn("Units"); model.addColumn("Cost"); model.addColumn("Continent"); model.addColumn("Year"); simple_display(); String a = Integer.toString(total); jLabel5.setText(a); } /** * 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() { jPopupMenu1 = new javax.swing.JPopupMenu(); jPopupMenu2 = new javax.swing.JPopupMenu(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jCheckBox1 = new javax.swing.JCheckBox(); jComboBox1 = new javax.swing.JComboBox(); jCheckBox2 = new javax.swing.JCheckBox(); jCheckBox3 = new javax.swing.JCheckBox(); jComboBox2 = new javax.swing.JComboBox(); jComboBox3 = new javax.swing.JComboBox(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Back"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jCheckBox1.setText("Year"); jCheckBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCheckBox1ActionPerformed(evt); } }); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2012", "2013", "2014" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jCheckBox2.setText("Region"); jCheckBox2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCheckBox2ActionPerformed(evt); } }); jCheckBox3.setText("Manufacture"); jCheckBox3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCheckBox3ActionPerformed(evt); } }); jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Asia Pacific", "Australia", "Europe", "North-Central America", "South America", "Russia" })); jComboBox2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox2ActionPerformed(evt); } }); jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select a manufucture", "HYUNDAI", "MARUTISUZUKI", "VOLKSVAGEN", "NISSAN", "BAJAJ", "KTM" })); jComboBox3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox3ActionPerformed(evt); } }); jLabel3.setText("ORDER SUMMARY"); jLabel4.setText("Total Orders"); jTable1.setModel( new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); jScrollPane2.setViewportView(jTable1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(17, 17, 17).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jCheckBox3) .addGroup(layout.createSequentialGroup() .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(34, 34, 34) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(46, 46, 46).addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(855, 855, 855)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(25, 25, 25) .addComponent(jLabel2).addGap(180, 180, 180).addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(83, 83, 83).addComponent( jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 663, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(607, 607, 607))) .addGroup(layout.createSequentialGroup() .addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(33, 33, 33).addComponent(jCheckBox2).addGap(337, 337, 337) .addComponent(jLabel5).addGap(865, 865, 865))) .addComponent(jButton1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(26, 26, 26).addComponent(jLabel2))) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jCheckBox1).addComponent(jCheckBox2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel4) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel5)) .addGroup(layout.createSequentialGroup().addComponent(jCheckBox3) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(4, 4, 4))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(224, 224, 224) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1).addComponent(jLabel1)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: this.dispose(); autoexpo a = new autoexpo(); a.setVisible(true); }//GEN-LAST:event_jButton1ActionPerformed private void disp(String s) { MongoClient mongo = null; int x = 0; try { mongo = new MongoClient("localhost", 27017); } catch (UnknownHostException | MongoException e) { } if (jTable1.getRowCount() > 0) { for (int i = jTable1.getRowCount() - 1; i > -1; i--) { model.removeRow(i); } } DB db = mongo.getDB("AUTOMOBILEXPO"); String[] st = new String[10]; st[0] = "HYUNDAI"; st[1] = "MARUTISUZUKI"; st[2] = "NISSAN"; st[3] = "BAJAJ"; st[4] = "KTM"; st[5] = "VOLKSVAGEN"; String s1, s2, s3; s1 = jComboBox1.getSelectedItem().toString(); s2 = jComboBox2.getSelectedItem().toString(); s3 = jComboBox3.getSelectedItem().toString(); BasicDBObject search = new BasicDBObject(); BasicDBObject result = new BasicDBObject(); BasicDBObject sp = new BasicDBObject(); if (s.equals(s1)) { int i = Integer.parseInt(s); sp.put("Year", i); if (jCheckBox2.isSelected() && jCheckBox3.isSelected()) { sp.append("country", s2); sp.append("manufacturer", s3); } else if (jCheckBox2.isSelected()) { sp.append("country", s2); } else if (jCheckBox3.isSelected()) { sp.append("manufacturer", s3); } } else if (s.equals(s2)) { sp.put("country", s2); int i = Integer.parseInt(s1); if (jCheckBox1.isSelected() && jCheckBox3.isSelected()) { sp.append("Year", i); //sp.append("country",s2); sp.append("manufacturer", s3); } else if (jCheckBox1.isSelected()) { //sp.append("country",s2); sp.append("Year", i); } else if (jCheckBox3.isSelected()) { sp.append("manufacturer", s3); } } else if (s.equals(s3)) { sp.put("manufacturer", s3); int i = Integer.parseInt(s1); if (jCheckBox1.isSelected() && jCheckBox2.isSelected()) { sp.append("Year", i); sp.append("country", s2); //sp.append("manufacturer",s3); } else if (jCheckBox1.isSelected()) { //sp.append("country",s2); sp.append("Year", i); } else if (jCheckBox2.isSelected()) { sp.append("country", s2); } } ArrayList<String> arr = new ArrayList(); for (int j = 0; j <= 5; j++) { DBCollection table; table = db.getCollection(st[j]); if (table.findOne() != null) { DBCursor cr = table.find(sp); DBObject obj; while (cr.hasNext()) { obj = cr.next(); x++; //jLabel1.setText(jLabel1.getText()+"Model\n"+(obj.toString())+"\n"); //jTextArea1. // jTextArea1.setText(jTextArea1.getText()+"Manufacture: "+(obj.get("manufacturer").toString())+" Model: "+(obj.get("model").toString())+" Units : "+(obj.get("Units").toString())+"Cost : "+(obj.get("Cost").toString())+" Continent : "+(obj.get("country").toString())+" Year :"+(obj.get("Year").toString())+"\n"); model.addRow(new Object[] { obj.get("manufacturer").toString(), (obj.get("model").toString()), (obj.get("Units").toString()), (obj.get("Cost").toString()), (obj.get("country").toString()), (obj.get("Year").toString()) }); } } } total = x; String a = Integer.toString(total); jLabel5.setText(a); } private void simple_display() { MongoClient mongo = null; try { mongo = new MongoClient("localhost", 27017); } catch (UnknownHostException | MongoException e) { } // String batches=(String)jComboBox1.getSelectedItem( ); //get collections if (jTable1.getRowCount() > 0) { for (int i = jTable1.getRowCount() - 1; i > -1; i--) { model.removeRow(i); } } int x = 0; DB db = mongo.getDB("AUTOMOBILEXPO"); String[] st = new String[10]; st[0] = "HYUNDAI"; st[1] = "MARUTISUZUKI"; st[2] = "NISSAN"; st[3] = "BAJAJ"; st[4] = "KTM"; st[5] = "VOLKSVAGEN"; ArrayList<String> arr = new ArrayList(); for (int j = 0; j <= 5; j++) { DBCollection table; table = db.getCollection(st[j]); if (table.findOne() != null) { BasicDBObject search = new BasicDBObject(); BasicDBObject result = new BasicDBObject(); BasicDBObject sp = new BasicDBObject(); DBCursor cr = table.find(); //System.out.println(table.findOne()); String str; DBObject obj; while (cr.hasNext()) { x++; obj = cr.next(); //jTextArea1.setText(jTextArea1.getText()+" Manufacture : "+(obj.get("manufacturer").toString())+" Model : "+(obj.get("model").toString())+" Units : "+(obj.get("Units").toString())+" Cost : "+(obj.get("Cost").toString())+" Continent : "+(obj.get("country").toString())+" Year : "+(obj.get("Year").toString())+"\n"); model.addRow(new Object[] { obj.get("manufacturer").toString(), (obj.get("model").toString()), (obj.get("Units").toString()), (obj.get("Cost").toString()), (obj.get("country").toString()), (obj.get("Year").toString()) }); } } } total = x; String a = Integer.toString(total); jLabel5.setText(a); } private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed // TODO add your handling code here: //jComboBox1.setEnabled(true); if (jCheckBox1.isSelected()) { jComboBox1.setEnabled(true); } else { String s; jComboBox1.setEnabled(false); if ((!jCheckBox2.isSelected() && !jCheckBox3.isSelected())) { simple_display(); } if (jCheckBox2.isSelected()) { s = jComboBox2.getSelectedItem().toString(); disp(s); } if (jCheckBox3.isSelected()) { s = jComboBox3.getSelectedItem().toString(); disp(s); } } }//GEN-LAST:event_jCheckBox1ActionPerformed private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed // TODO add your handling code here: if (jCheckBox2.isSelected()) { jComboBox2.setEnabled(true); } else { String s; jComboBox2.setEnabled(false); if ((!jCheckBox1.isSelected() && !jCheckBox3.isSelected())) { simple_display(); } if (jCheckBox1.isSelected()) { s = jComboBox1.getSelectedItem().toString(); disp(s); } if (jCheckBox3.isSelected()) { s = jComboBox3.getSelectedItem().toString(); disp(s); } } }//GEN-LAST:event_jCheckBox2ActionPerformed private void jCheckBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox3ActionPerformed // TODO add your handling code here: if (jCheckBox3.isSelected()) jComboBox3.setEnabled(true); else { String s; jComboBox3.setEnabled(false); if ((!jCheckBox2.isSelected() && !jCheckBox1.isSelected())) { simple_display(); } if (jCheckBox2.isSelected()) { s = jComboBox2.getSelectedItem().toString(); disp(s); } if (jCheckBox1.isSelected()) { s = jComboBox1.getSelectedItem().toString(); disp(s); } } }//GEN-LAST:event_jCheckBox3ActionPerformed private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed // TODO add your handling code here: String s; int x = 0; s = jComboBox1.getSelectedItem().toString(); if (jCheckBox2.isSelected() && jCheckBox3.isSelected()) { x = 2; } disp(s); }//GEN-LAST:event_jComboBox1ActionPerformed private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed // TODO add your handling code here: String s; s = jComboBox2.getSelectedItem().toString(); disp(s); }//GEN-LAST:event_jComboBox2ActionPerformed private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed // TODO add your handling code here: String s; s = jComboBox3.getSelectedItem().toString(); disp(s); }//GEN-LAST:event_jComboBox3ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new display_2().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JCheckBox jCheckBox1; private javax.swing.JCheckBox jCheckBox2; private javax.swing.JCheckBox jCheckBox3; 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.JPopupMenu jPopupMenu1; private javax.swing.JPopupMenu jPopupMenu2; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable jTable1; // End of variables declaration//GEN-END:variables }