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.awt.event.ActionEvent; import java.awt.event.ActionListener; 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 SourceCodeFrame extends javax.swing.JFrame { /** * Creates new form SourceCodeFrame */ private String nameField, profField, progField, progNumField, dateField, langField; public SourceCodeFrame() { initComponents(); this.setVisible(true); System.out.println("XD"); } public SourceCodeFrame(String comboValue, String nameField, String profField, String progField, String progNumField, String dateField, String langField) { initComponents(); this.setVisible(true); System.out.println(comboValue); 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() { jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); codeTextArea = new javax.swing.JTextArea(); doneButton = new javax.swing.JButton(); closeButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Source Code"); codeTextArea.setColumns(20); codeTextArea.setRows(5); jScrollPane1.setViewportView(codeTextArea); 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(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(doneButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(closeButton).addGap(19, 19, 19)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent( jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 535, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup( layout.createSequentialGroup().addGap(240, 240, 240).addComponent(jLabel1))) .addContainerGap(18, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(doneButton).addComponent(closeButton)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed // TODO add your handling code here: XWPFDocument document = new XWPFDocument(); //write document try { FileOutputStream out = new FileOutputStream(new File("sourcecode.docx")); //making of title XWPFParagraph para = document.createParagraph(); para.setAlignment(ParagraphAlignment.CENTER); para.setSpacingAfter(500); XWPFRun run = para.createRun(); run.setText("Source Code Listing"); run.setBold(true); run.setFontSize(16); //end making of title para = document.createParagraph(); run = para.createRun(); //adding name etc to document XWPFTable details = document.createTable(3, 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: " + dateField); row = details.getRow(1); row.getCell(0).setText("Program: " + progField); row.getCell(1).setText("Program#: " + progNumField); row = details.getRow(2); row.getCell(0).setText("Professor: " + profField); row.getCell(1).setText("Language: " + langField); //end adding name to document para = document.createParagraph(); run = para.createRun(); run.addBreak(); run.addBreak(); String sourceCode = codeTextArea.getText(); if (sourceCode.contains("\n")) { String[] lines = sourceCode.split("\n"); run.setText(lines[0], 0); for (int i = 1; i < lines.length; i++) { run.addBreak(); if (lines[i].contains("\t")) { int tabSpace = lines[i].length() - lines[i].replaceAll("\t", "").length(); for (int j = 0; j < tabSpace; j++) { run.addTab(); } } run.setText(lines[i]); } } else run.setText(sourceCode); //sourceCode.replaceAll("\\n", "
"); document.write(out); out.close(); } catch (Exception e) { } }//GEN-LAST:event_doneButtonActionPerformed 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 /** * @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(SourceCodeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(SourceCodeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(SourceCodeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(SourceCodeFrame.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 SourceCodeFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton closeButton; private javax.swing.JTextArea codeTextArea; private javax.swing.JButton doneButton; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables }