Results 1 to 16 of 16

Thread: GUI printing

  1. #1
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    68
    Rep Power
    0

    Default GUI printing

    Hi , i created small gui frame and wanted to take a print out of that frame with all the contents. it was running successfully without any errors in my machine (xp service pack 3), but when i tried to run it another machine (xp service pack 2) with jre environment i couldn't take a hard copy , sheet is processing without any marks (blank paper). Can any one explain why this is happening..

    Thanks,
    Uthpala

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    How did you run your application in the second machine, with the source or did you build the application?

  3. #3
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    68
    Rep Power
    0

    Default

    yes, i built the application, a * .jar file is running on second machine.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    It looks rather like you might be making calls to Swing methods off of the EDT. To get better help sooner, post a SSCCE *

    * SSCCE : Java Glossary

    db

  5. #5
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    68
    Rep Power
    0

    Default

    what is this SSCCE ? i have no idea.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Means this.

    Short Self-Contained Compilable Example
    Did you go through the link that Darryl post in his last post? Click on the second result on that page and read it.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by uthpalaw View Post
    yes, i built the application, a * .jar file is running on second machine.
    How did you run it and did you get any error messages ?

  8. #8
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    68
    Rep Power
    0

    Default

    no, i didn't get any error messages..

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    68
    Rep Power
    0

    Default

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * SimplePrint.java
     *
     * Created on Jan 11, 2010, 4:29:16 PM
     */
    
    package home.tess.ukw;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionListener;
    import java.awt.print.*;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author uthpalaw
     */
    public class SimplePrint extends javax.swing.JFrame implements Printable {
    
        JFrame frameToPrint;
    
        static JFrame SP = new SimplePrint();
        /** Creates new form SimplePrint */
        public SimplePrint() {
            initComponents();
        }
    
        public SimplePrint(JFrame f){
            frameToPrint = f;
    
        }
    
        /** 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();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Simple Print Form");
    
            jLabel1.setText("Hi Diluka");
    
            jButton1.setText("Print");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(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()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1))
                    .addContainerGap(361, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(51, 51, 51)
                    .addComponent(jLabel1)
                    .addGap(65, 65, 65)
                    .addComponent(jButton1)
                    .addContainerGap(132, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>//GEN-END:initComponents
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            // TODO add your handling code here:
            
    //        JOptionPane.showMessageDialog(null, "hello");
            PrinterJob PJ   =   PrinterJob.getPrinterJob();
            //PageFormat PF   =   PJ.pageDialog(PJ.defaultPage());
            PJ.setPrintable((Printable) this);
            boolean doPrint = PJ.printDialog();
    
            //JOptionPane.showMessageDialog(null, doPrint);
            if(doPrint){
                try {
                    PJ.print();
                } catch (PrinterException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
               }
            }
        }//GEN-LAST:event_jButton1ActionPerformed
    
        public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
            if (pageIndex > 0) {
              return(NO_SUCH_PAGE);
            } else {
              Graphics2D g2d = (Graphics2D)g;
              g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
              // Turn off double buffering
              //componentToBePrinted.paint(g2d);
              //frameToPrint.print(g);
              SP.print(g);
              // Turn double buffering back on
              return(PAGE_EXISTS);
            }
          }
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    
                    SP.setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        // End of variables declaration//GEN-END:variables
    
    
    }
    here is the code i tried..
    Last edited by Eranga; 05-27-2010 at 05:56 AM. Reason: code tags added

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags when you posting again here in the forum. Unformated codes are really hard to read. If you don't know how to do it, please check on my forum signature. You can find the link to the relevant page.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Is this initialized properly?

    Java Code:
    Graphics2D g2d = (Graphics2D)g;
              g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

  13. #13
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    68
    Rep Power
    0

    Default

    no idea.... what do u suggest ?

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What I'm ask is that following values are set properly?

    Java Code:
    pageFormat.getImageableX(), pageFormat.getImageableY()

  15. #15
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    68
    Rep Power
    0

    Default

    yes....... it's.......

  16. #16
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    68
    Rep Power
    0

    Unhappy Please help me .. This is urgent.....

    someone PLEASE HELP ME..... this is little bit urgent....

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 03:40 PM
  2. Printing
    By zzpprk in forum AWT / Swing
    Replies: 0
    Last Post: 01-20-2010, 11:25 AM
  3. Printing Help...
    By chiragkini in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2009, 06:07 AM
  4. Printing issues...
    By chango77745 in forum New To Java
    Replies: 15
    Last Post: 02-14-2009, 02:01 AM
  5. Printing Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •