Java tutorial
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.UnknownHostException; import javax.swing.JFrame; import javax.swing.JOptionPane; import com.mongodb.*; /* * 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 yogesh */ @SuppressWarnings("serial") public class Search_trainer extends javax.swing.JFrame implements ActionListener { /** * Creates new form Search_trainer */ public Search_trainer() { initComponents(); } /** * 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", "rawtypes" }) // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton2.setBackground(new java.awt.Color(51, 0, 51)); jButton2.setFont(new java.awt.Font("SansSerif", 3, 12)); // NOI18N jButton2.setForeground(new java.awt.Color(255, 255, 255)); jButton2.setText("Back"); jButton2.setBounds(20, 20, 80, 40); add(jButton2); jButton2.addActionListener(this); jLabel1.setFont(new java.awt.Font("SansSerif", 3, 24)); // NOI18N jLabel1.setForeground(new java.awt.Color(204, 255, 204)); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("Search Receptionist"); jLabel1.setBounds(126, 40, 314, 85); add(jLabel1); jLabel2.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel2.setForeground(new java.awt.Color(204, 255, 204)); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel2.setText("Enter Field : "); jComboBox1.setFont(new java.awt.Font("SansSerif", 3, 13)); // NOI18N jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "First_Name", "Last_Name", "Batch", "All_Trainers", "Phone_Number", "Age", "Username" })); jComboBox1.addActionListener(this); jButton1.addActionListener(this); getContentPane().setBackground(new java.awt.Color(88, 0, 0)); jLabel2.setBounds(130, 160, 130, 40); jComboBox1.setBounds(280, 160, 130, 40); add(jLabel2); add(jComboBox1); setSize(610, 500); setLayout(null); }// </editor-fold> /** * @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(Search_trainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Search_trainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Search_trainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Search_trainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // </editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new Search_trainer().setVisible(true); } }); } // Variables declaration - do not modify @SuppressWarnings("rawtypes") private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JTextField jTextField1; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; // End of variables declaration @Override public void actionPerformed(ActionEvent ae) { // TODO Auto-generated method stub add(jLabel3); add(jTextField1); add(jButton1); if (ae.getSource() == jComboBox1) { String Variable = jComboBox1.getSelectedItem().toString(); jLabel3.setText(""); jTextField1.setText(""); if (Variable == "All_Trainers") { try { MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("Gym"); DBCollection Gym_Collection = db.getCollection("trainer"); DBCursor cursor1 = Gym_Collection.find(); int i = 0; String[][] s = new String[20][10]; while (cursor1.hasNext()) { cursor1.next(); s[i][0] = cursor1.curr().get("First_Name").toString(); s[i][1] = cursor1.curr().get("Last_Name").toString(); s[i][2] = cursor1.curr().get("Batch").toString(); s[i][3] = cursor1.curr().get("Age").toString(); s[i][4] = cursor1.curr().get("Username").toString(); s[i][5] = cursor1.curr().get("Password").toString(); s[i][6] = cursor1.curr().get("Phone_Number").toString(); s[i][7] = cursor1.curr().get("Address").toString(); i++; } JFrame frame = new JFrame("Resulted Trainers"); frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); Trainer_Table ne = new Trainer_Table(s); ne.setOpaque(true); frame.setContentPane(ne); frame.pack(); frame.setVisible(true); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { jLabel3.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel3.setForeground(new java.awt.Color(204, 255, 204)); jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel3.setText("Enter " + Variable + ": "); jLabel3.setBounds(55, 230, 200, 40); jTextField1.setBounds(267, 230, 200, 40); jButton1.setBackground(new java.awt.Color(51, 0, 51)); jButton1.setFont(new java.awt.Font("SansSerif", 3, 12)); // NOI18N jButton1.setForeground(new java.awt.Color(255, 255, 255)); jButton1.setText("Search Trainer"); jButton1.setBounds(160, 300, 300, 40); setVisible(true); } } else if (ae.getSource() == jButton1) { try { String Variable = jComboBox1.getSelectedItem().toString(); MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("Gym"); DBCollection Gym_Collection = db.getCollection("trainer"); BasicDBObject fields; if (Variable == "First_Name" || Variable == "Last_Name" || Variable == "Username" || Variable == "Batch") { fields = new BasicDBObject(Variable, jTextField1.getText().toString()); } else { fields = new BasicDBObject(Variable, Integer.parseInt(jTextField1.getText().toString())); } DBCursor cursor1 = Gym_Collection.find(fields); int i = 0; String[][] s = new String[20][10]; while (cursor1.hasNext()) { cursor1.next(); s[i][0] = cursor1.curr().get("First_Name").toString(); s[i][1] = cursor1.curr().get("Last_Name").toString(); s[i][2] = cursor1.curr().get("Batch").toString(); s[i][3] = cursor1.curr().get("Age").toString(); s[i][4] = cursor1.curr().get("Username").toString(); s[i][5] = cursor1.curr().get("Password").toString(); s[i][6] = cursor1.curr().get("Phone_Number").toString(); s[i][7] = cursor1.curr().get("Address").toString(); i++; } JFrame frame = new JFrame("Resulted Trainers"); frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); Trainer_Table ne = new Trainer_Table(s); ne.setOpaque(true); frame.setContentPane(ne); frame.pack(); frame.setVisible(true); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NumberFormatException e) {// To validate for data types JOptionPane.showMessageDialog(this, "Please Check the Data types"); jTextField1.setText(""); } } else if (ae.getSource() == jButton2) { this.setVisible(false); new Admin().setVisible(true); } } }