title: Print an opened Document using JMenuItem
body: <p>I want to print an opened File when I click print menuitem.I Wrote some code but It's not working properly.It shows print Dialog box then I click print button it was not taken print .Please help me,Thank you.
  My code:</p>
  
  <pre><code>public class PrintAction extends javax.swing.JFrame {
  
  JScrollPane scrollPane;
  JTextPane textPane;
  int i=0;
  public PrintAction() {
      initComponents();
  }
  @SuppressWarnings("unchecked")
  // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
  private void initComponents() {
  
      tabbedPane = new javax.swing.JTabbedPane();
      jMenuBar1 = new javax.swing.JMenuBar();
      jMenu1 = new javax.swing.JMenu();
      open = new javax.swing.JMenuItem();
      print = new javax.swing.JMenuItem();
  
      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
      jMenu1.setText("File");
  
      open.setText("Open");
      open.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              openActionPerformed(evt);
          }
      });
      jMenu1.add(open);
  
      print.setText("Print");
      print.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              printActionPerformed(evt);
          }
      });
      jMenu1.add(print);
  
      jMenuBar1.add(jMenu1);
  
      setJMenuBar(jMenuBar1);
  
      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
      );
      layout.setVerticalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
      );
  
      pack();
  }// &lt;/editor-fold&gt;                        
  
  private void openActionPerformed(java.awt.event.ActionEvent evt) {                                     
       FileDialog fd = new FileDialog(PrintAction.this, "Select File", FileDialog.LOAD);
       fd.show();
       String title;
       String sts;
       if (fd.getFile() != null) {
         sts = fd.getDirectory() + fd.getFile();
         title=fd.getFile();
         BufferedReader br = null;
         StringBuffer str = new StringBuffer("");
         try {
             br = new BufferedReader(new FileReader(sts));
             String line;
             try {
                  while ((line = br.readLine()) != null) {
                  str.append(line + "\n");
                  }
             } catch (IOException ex) {
                  Logger.getLogger(PrintAction.class.getName()).log(Level.SEVERE, null, ex);
               }
         } catch (FileNotFoundException ex) {
              Logger.getLogger(PrintAction.class.getName()).log(Level.SEVERE, null, ex);
           }
         String t = str.toString();
         final JInternalFrame internalFrame = new JInternalFrame("",true,true);  
         textPane = new JTextPane();
         textPane.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
         internalFrame.add(textPane);
         i+=1;
         internalFrame.setName("Document"+i);
         scrollPane=new JScrollPane(textPane);
         internalFrame.add(scrollPane); 
         internalFrame.setTitle(title);
         tabbedPane.add(internalFrame);
         internalFrame.setVisible(true);   
         textPane.setText(t);
         try {
              br.close();
              } 
           catch (IOException ex) {
              Logger.getLogger(PrintAction.class.getName()).log(Level.SEVERE, null, ex);
          }
  </code></pre>
  
  <p>}
      }                                    </p>
  
  <pre><code>private void printActionPerformed(java.awt.event.ActionEvent evt) {                                      
        PrinterJob pj = PrinterJob.getPrinterJob();
        HashPrintRequestAttributeSet printAttr = new HashPrintRequestAttributeSet();
        if(pj.printDialog(printAttr))     // Display print dialog
        {            // If true is returned...
        try
        {
        pj.print(printAttr);    // then print
        }
        catch(PrinterException e)
        {
        JOptionPane.showMessageDialog(this,"Failed to print the file: "+e,"Error",JOptionPane.ERROR_MESSAGE);
        }
        }
  }                                     
  public static void main(String args[]) {
      /* Set the Nimbus look and feel */
      //&lt;editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "&gt;
      /* 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(PrintAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (InstantiationException ex) {
          java.util.logging.Logger.getLogger(PrintAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (IllegalAccessException ex) {
          java.util.logging.Logger.getLogger(PrintAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (javax.swing.UnsupportedLookAndFeelException ex) {
          java.util.logging.Logger.getLogger(PrintAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      }
      //&lt;/editor-fold&gt;
  
      /* Create and display the form */
      java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
              new PrintAction().setVisible(true);
          }
      });
  }
  // Variables declaration - do not modify                     
  private javax.swing.JMenu jMenu1;
  private javax.swing.JMenuBar jMenuBar1;
  private javax.swing.JMenuItem open;
  private javax.swing.JMenuItem print;
  private javax.swing.JTabbedPane tabbedPane;
  // End of variables declaration                   
  </code></pre>
  
  <p>}</p>