Java tutorial
import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.MongoClient; import java.util.ArrayList; import java.util.List; 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 mbp */ public class Parent extends javax.swing.JPanel { /** * Creates new form Parent */ public static MongoClient conn; public static DB db; public static String firstname; public static String lastname; public static Login login; public Parent(MongoClient connection, DB db1, String firstname, String lastname, Login t) { conn = connection; db = db1; this.firstname = firstname; this.lastname = lastname; login = t; 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") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); SonList = new javax.swing.JList<>(); jScrollPane2 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); LogoutBtn = new javax.swing.JButton(); addComponentListener(new java.awt.event.ComponentAdapter() { public void componentResized(java.awt.event.ComponentEvent evt) { formComponentResized(evt); } public void componentShown(java.awt.event.ComponentEvent evt) { formComponentShown(evt); } }); SonList.setModel(new javax.swing.AbstractListModel<String>() { String[] strings = { "Son1", "Son2", "Son3", "Son4", "Son5" }; public int getSize() { return strings.length; } public String getElementAt(int i) { return strings[i]; } }); SonList.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { SonListValueChanged(evt); } }); jScrollPane1.setViewportView(SonList); jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "course", "teacher", "mid_mark", "final_mark" })); jScrollPane2.setViewportView(jTable1); LogoutBtn.setText("log out"); LogoutBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { LogoutBtnActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap(293, Short.MAX_VALUE).addComponent(LogoutBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(63, 63, 63))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(LogoutBtn) .addGap(0, 7, Short.MAX_VALUE))); }// </editor-fold>//GEN-END:initComponents private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown // TODO add your handling code here: }//GEN-LAST:event_formComponentShown private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized // TODO add your handling code here: DBCollection coll = db.getCollection("parent"); BasicDBObject andQuery = new BasicDBObject(); List<BasicDBObject> obj = new ArrayList<BasicDBObject>(); obj.add(new BasicDBObject("firstname", firstname)); obj.add(new BasicDBObject("lastname", lastname)); andQuery.put("$and", obj); DBCursor cursor = coll.find(andQuery); while (cursor.hasNext()) { BasicDBList sons = (BasicDBList) cursor.next().get("sons"); String data[] = new String[sons.size()]; for (int j = 0; j < sons.size(); ++j) { BasicDBObject son = (BasicDBObject) sons.get(j); data[j] = son.get("name").toString(); } SonList.setListData(data); } }//GEN-LAST:event_formComponentResized private void SonListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_SonListValueChanged // TODO add your handling code here: if (!evt.getValueIsAdjusting()) { DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); model.setRowCount(0); String selected = SonList.getSelectedValue(); DBCollection coll = db.getCollection("student"); BasicDBObject andQuery = new BasicDBObject(); List<BasicDBObject> obj = new ArrayList<BasicDBObject>(); obj.add(new BasicDBObject("firstname", selected)); obj.add(new BasicDBObject("mid_name", firstname)); obj.add(new BasicDBObject("lastname", lastname)); andQuery.put("$and", obj); DBCursor cursor = coll.find(andQuery); while (cursor.hasNext()) { BasicDBList marks = (BasicDBList) cursor.next().get("marks"); for (int j = 0; j < marks.size(); ++j) { BasicDBObject mark = (BasicDBObject) marks.get(j); String material = mark.get("course").toString(); String teacher = mark.get("teacher").toString(); if (mark.get("mid") == null) { Object[] row = { material, teacher, "not readay", "not ready" }; model.addRow(row); } else if (mark.get("mid") != null && mark.get("final") == null) { Object[] row = { material, teacher, mark.get("mid"), "not ready" }; model.addRow(row); } else { Object[] row = { material, teacher, mark.get("mid"), mark.get("final") }; model.addRow(row); } } } } }//GEN-LAST:event_SonListValueChanged private void LogoutBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogoutBtnActionPerformed // TODO add your handling code here: this.setVisible(false); login.setVisible(true); }//GEN-LAST:event_LogoutBtnActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton LogoutBtn; private javax.swing.JList<String> SonList; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable jTable1; // End of variables declaration//GEN-END:variables }