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 fitnessmanagersystem; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.PageSize; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfWriter; import static fitnessmanagersystem.SearchClient.jTable1_clients; import static fitnessmanagersystem.SearchProduct.jTable12dfv; import java.awt.FileDialog; import java.awt.Frame; import java.awt.Graphics2D; import java.awt.Shape; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; /** * * @author DISHLIEV */ public class ExportPanelProducts extends javax.swing.JPanel { /** * Creates new form ExportPanelProducts */ public ExportPanelProducts() { 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() { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton1.setIcon(new javax.swing.ImageIcon( "C:\\Users\\DISHLIEV\\Documents\\1\\FitnessManagerSystem\\icons\\Excel-icon.png")); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setIcon(new javax.swing.ImageIcon( "C:\\Users\\DISHLIEV\\Documents\\1\\FitnessManagerSystem\\icons\\pdf.png")); // NOI18N jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setIcon(new javax.swing.ImageIcon( "C:\\Users\\DISHLIEV\\Documents\\1\\FitnessManagerSystem\\icons\\cancel.png")); // NOI18N jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(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().addContainerGap().addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton3) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(jButton2).addComponent(jButton3)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); }// </editor-fold>//GEN-END:initComponents private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed Document document = new Document(PageSize.A2.rotate()); try { String FileDialog = null; // PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\DISHLIEV\\Desktop\\jTsdfb11wablwe.pdf")); JFileChooser chooser = new JFileChooser("."); FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF", "pdf"); chooser.setFileFilter(filter); chooser.showSaveDialog(this); //chooser.setFileFilter(filter); // chooser.addChoosableFileFilter(filter); File file = chooser.getSelectedFile(); String fileName = chooser.getSelectedFile().getPath(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); // PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FileDialog+"C:\\Users\\DISHLIEV\\Desktop\\jTsdfb11wablwe.pdf")); /* FileDialog saveFileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE); saveFileDialog.setFile(""); saveFileDialog.setVisible(true); saveFileDialog.getDirectory(); try { dexceleporte exp = new dexceleporte(); exp.fillData(jTable1, new File(saveFileDialog.getDirectory()+saveFileDialog.getFile()+".xls")); } catch (Exception ex) { ex.printStackTrace(); } */ document.open(); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); Graphics2D g2 = cb.createGraphicsShapes(1500, 500); Shape oldClip = g2.getClip(); g2.clipRect(0, 0, 1500, 500); jTable12dfv.print(g2); g2.setClip(oldClip); g2.dispose(); cb.restoreState(); //} } catch (FileNotFoundException | DocumentException e) { System.err.println(e.getMessage()); } document.close(); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed FileDialog saveFileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE); FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF", "pdf"); saveFileDialog.setFile(""); saveFileDialog.setVisible(true); saveFileDialog.getDirectory(); try { dexceleporteProducts exp = new dexceleporteProducts(); exp.fillData(jTable12dfv, new File(saveFileDialog.getDirectory() + saveFileDialog.getFile() + ".xls")); } catch (Exception ex) { ex.printStackTrace(); } }//GEN-LAST:event_jButton1ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed this.setVisible(false); }//GEN-LAST:event_jButton3ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; // End of variables declaration//GEN-END:variables }