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 sofmeth.mco3.gui; import java.io.File; import java.io.FileOutputStream; import java.math.BigInteger; import javax.swing.SwingConstants; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; /** * * @author owner */ public class TPTFrame extends javax.swing.JFrame { /** * Creates new form TPTFrame */ private float pValue, tHours = 0; private String nameField, profField, progField, progNumField, dateField, langField; public TPTFrame() { initComponents(); } public TPTFrame(String comboValue, String nameField, String profField, String progField, String progNumField, String dateField, String langField) { initComponents(); this.setVisible(true); this.nameField = nameField; this.profField = profField; this.progField = progField; this.progNumField = progNumField; this.dateField = dateField; this.langField = langField; } /** * 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(); tptTable = new javax.swing.JTable(); doneButton = new javax.swing.JButton(); closeButton = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); tptTable.setModel(new javax.swing.table.DefaultTableModel( new Object[][] { { "", null, null, null, null, null, null, null, null, null }, { "", null, null, null, null, null, null, null, null, null }, { "", null, null, null, null, null, null, null, null, null }, { "", null, null, null, null, null, null, null, null, null }, { "", null, null, null, null, null, null, null, null, null }, { "", null, null, null, null, null, null, null, null, null }, { "", null, null, null, null, null, null, null, null, null }, { "", null, null, null, null, null, null, null, null, null }, { "", null, null, null, null, null, null, null, null, null }, { "", null, null, null, null, null, null, null, null, null } }, new String[] { "Number", "Name", "Hours", "Planned Value", "Cumulative Hours", "Cumulative Planned Value", "Date(Plan)", "Date(Actual)", "Earned Value", "Cumulative Earned Value" })); tptTable.addPropertyChangeListener(new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { tptTablePropertyChange(evt); } }); tptTable.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { tptTableKeyPressed(evt); } public void keyTyped(java.awt.event.KeyEvent evt) { tptTableKeyTyped(evt); } }); jScrollPane1.setViewportView(tptTable); doneButton.setText("Done"); doneButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { doneButtonActionPerformed(evt); } }); closeButton.setText("Close"); closeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { closeButtonActionPerformed(evt); } }); jLabel1.setText("Task"); jLabel1.setVerticalAlignment(SwingConstants.CENTER); jLabel1.setHorizontalAlignment(SwingConstants.CENTER); jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel2.setText("Plan"); jLabel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel2.setVerticalAlignment(SwingConstants.CENTER); jLabel2.setHorizontalAlignment(SwingConstants.CENTER); jLabel3.setText("Actual"); jLabel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel3.setVerticalAlignment(SwingConstants.CENTER); jLabel3.setHorizontalAlignment(SwingConstants.CENTER); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(doneButton).addGap(3, 3, 3).addComponent(closeButton)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap(43, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jLabel2).addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(doneButton).addComponent(closeButton)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed // TODO add your handling code here: this.setVisible(false); }//GEN-LAST:event_closeButtonActionPerformed private void tptTablePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tptTablePropertyChange // TODO add your handling code here: }//GEN-LAST:event_tptTablePropertyChange private void tptTableKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tptTableKeyTyped // TODO add your handling code here: }//GEN-LAST:event_tptTableKeyTyped private void tptTableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tptTableKeyPressed // TODO add your handling code here: }//GEN-LAST:event_tptTableKeyPressed private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed // TODO add your handling code here: XWPFDocument document = new XWPFDocument(); try { FileOutputStream out = new FileOutputStream(new File("task planning template.docx")); //making of title XWPFParagraph para = document.createParagraph(); para.setAlignment(ParagraphAlignment.CENTER); para.setSpacingAfter(500); XWPFRun run = para.createRun(); run.setText("Defect Recording Log"); run.setBold(true); run.setFontSize(16); //end making of title //adding name etc to document XWPFTable details = document.createTable(2, 2); //setting cell width CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW(); width.setType(STTblWidth.DXA); width.setW(BigInteger.valueOf(9500)); //end setting cell width //computation for (int i = 0; i < 10; i++) { if (tptTable.getModel().getValueAt(i, 3) != null && !tptTable.getModel().getValueAt(i, 3).toString().isEmpty()) { pValue = pValue + Float.parseFloat(tptTable.getModel().getValueAt(i, 3).toString()); } if (tptTable.getModel().getValueAt(i, 2) != null && !tptTable.getModel().getValueAt(i, 2).toString().isEmpty()) { tHours = tHours + Float.parseFloat(tptTable.getModel().getValueAt(i, 2).toString()); } } // //details.getCTTbl().getTblPr().unsetTblBorders(); XWPFTableRow dtlRow = details.getRow(0); dtlRow.getCell(0).setText("Name: " + nameField); dtlRow.getCell(1).setText("Date: " + dateField); dtlRow = details.getRow(1); dtlRow.getCell(0).setText("Professor: " + profField); dtlRow.getCell(1).setText("Program#: " + progNumField); //end adding name to document para = document.createParagraph(); run = para.createRun(); run.addBreak(); run.addBreak(); //creating of TPT table XWPFTable table = document.createTable(5, 10); width = table.getCTTbl().addNewTblPr().addNewTblW(); width.setType(STTblWidth.DXA); width.setW(BigInteger.valueOf(9500)); /* TODO: make all loops like this while loops? */ for (int i = -1; i < 10; i++) { if (i == 9) break; if (i == -1) { //top row of the table XWPFTableRow row1 = table.getRow(i + 1); for (int j = 0; j < 10; j++) { switch (j) { //no formatting yet like bold etc case 0: row1.getCell(j).setText("Number"); break; case 1: row1.getCell(j).setText("Name"); break; case 2: row1.getCell(j).setText("Hours"); break; case 3: row1.getCell(j).setText("Planned Value"); break; case 4: row1.getCell(j).setText("Cumulative Hours"); break; case 5: row1.getCell(j).setText("Cumulative Planned Values"); break; case 6: row1.getCell(j).setText("Date(Plan)"); break; case 7: row1.getCell(j).setText("Date(Actual)"); break; case 8: row1.getCell(j).setText("Earned Value"); break; case 9: row1.getCell(j).setText("Cumulative Value"); break; } } } //although this code assumes that there are values inside //UPDATE1: ok i fixed it i think else { XWPFTableRow row = table.getRow(i + 1); for (int j = 0; j < 10; j++) { if (tptTable.getModel().getValueAt(i, 1) != null && !tptTable.getModel().getValueAt(i, j).toString().isEmpty()) { if (tptTable.getModel().getValueAt(i, j) != null && !tptTable.getModel().getValueAt(i, j).toString().isEmpty()) row.getCell(j).setText(tptTable.getModel().getValueAt(i, j).toString()); } else break; } } } table = document.createTable(2, 1); XWPFTableRow row = table.getRow(0); row.getCell(0).setText("Total Hours: " + tHours); row = table.getRow(1); row.getCell(0).setText("Total Planned Value: " + pValue); document.write(out); out.close(); } catch (Exception e) { } }//GEN-LAST:event_doneButtonActionPerformed /** * @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(TPTFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TPTFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TPTFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TPTFrame.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 TPTFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton closeButton; private javax.swing.JButton doneButton; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable tptTable; // End of variables declaration//GEN-END:variables }