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 org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; import java.math.BigInteger; /** * * @author owner */ public class TRTFrame extends javax.swing.JFrame { /** * Creates new form TSTFrame */ private String nameField, profField, progField, progNumField, dateField, langField; private int i = 0; public TRTFrame() { initComponents(); } public TRTFrame(String comboValue, String nameField, String profField, String progField, String progNumField, String dateField, String langField) { initComponents(); this.setVisible(true); System.out.println("YAY"); 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() { jPanel1 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); trtTable = new javax.swing.JTable(); doneButton = new javax.swing.JButton(); closeButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); trtTable.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 }, { "", null, null, null, null, null }, { "", null, null, null, null, null } }, new String[] { "Test Number", "Test Objective", "Test Description", "Test Condiitons", "Expected Results", "Actual Results" })); jScrollPane1.setViewportView(trtTable); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout .createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(20, Short.MAX_VALUE))); 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); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(doneButton).addGap(7, 7, 7).addComponent(closeButton).addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, 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) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .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 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("test report template.docx")); //making of title XWPFParagraph para = document.createParagraph(); para.setAlignment(ParagraphAlignment.CENTER); para.setSpacingAfter(500); XWPFRun run = para.createRun(); run.setText("Test Report Template"); run.setBold(true); run.setFontSize(16); //end making of title //start making name table XWPFTable details = document.createTable(2, 2); //setting cell width CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW(); width.setType(STTblWidth.DXA); width.setW(BigInteger.valueOf(9000)); //end setting cell width //details.getCTTbl().getTblPr().unsetTblBorders(); XWPFTableRow row = details.getRow(0); row.getCell(0).setText("Name: " + nameField); row.getCell(1).setText("Date: " + nameField); row = details.getRow(1); row.getCell(0).setText("Professor: " + profField); row.getCell(1).setText("Program#: " + progNumField); para = document.createParagraph(); run = para.createRun(); run.addBreak(); run.addBreak(); //end making name table while (true) { if (trtTable.getModel().getValueAt(i, 0) != null && !trtTable.getModel().getValueAt(i, 0).toString().isEmpty()) { XWPFTable table = document.createTable(6, 2); width = table.getCTTbl().addNewTblPr().addNewTblW(); width.setType(STTblWidth.DXA); width.setW(BigInteger.valueOf(9500)); for (int j = 0; j < 6; j++) { row = table.getRow(j); switch (j) { case 0: row.getCell(0).setText("Test Number"); row.getCell(1).setText(trtTable.getModel().getValueAt(i, 0).toString()); break; case 1: row.getCell(0).setText("Test Objective"); row.getCell(1).setText(trtTable.getModel().getValueAt(i, 1).toString()); break; case 2: row.getCell(0).setText("Test Description"); row.getCell(1).setText(trtTable.getModel().getValueAt(i, 2).toString()); break; case 3: row.getCell(0).setText("Test Conditions"); row.getCell(1).setText(trtTable.getModel().getValueAt(i, 3).toString()); break; case 4: row.getCell(0).setText("Expected Results"); row.getCell(1).setText(trtTable.getModel().getValueAt(i, 4).toString()); break; case 5: row.getCell(0).setText("Actual Results"); row.getCell(1).setText(trtTable.getModel().getValueAt(i, 5).toString()); break; } } para = document.createParagraph(); run = para.createRun(); run.addBreak(); run.addBreak(); i++; } else break; } 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(TRTFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TRTFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TRTFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TRTFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TRTFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton closeButton; private javax.swing.JButton doneButton; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable trtTable; // End of variables declaration//GEN-END:variables }