Results 1 to 8 of 8

Thread: Print jFrame

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

    Angry Print jFrame

    Hi - I'm designing a small jFrame with some labels, fields and a table. I want to send this frame to a printer so that i can take a hard copy of the the frame with all the contents. I'm using netbeans as the ide. Highly appreciate any help on this.

    Thanks in advanced.
    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

    I don't think there is a simple/direct way to do is.

    What you can do is, take screen shot of your UI and print it. Read more about java.awt.print for printing a document.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    i've already seen code that can print components, for example here Swing Tutorial: High-Quality Java Printing but i never check it out. if you try it out could you please give a short information if it works?

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

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

    Thumbs up

    hey guys... i could find a code which is printing a gui form with all components.. it's working fine..
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.print.*;
    
     class PrintUIWindow implements Printable, ActionListener {
    
        JFrame frameToPrint;
    
        public int print(Graphics g, PageFormat pf, int page) throws
                                                            PrinterException {
    
            if (page > 0) {
                return NO_SUCH_PAGE;
            }
    
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pf.getImageableX(), pf.getImageableY()-55);
    
            frameToPrint.print(g);
    
            return PAGE_EXISTS;
        }
    
        public void actionPerformed(ActionEvent e) {
             PrinterJob job = PrinterJob.getPrinterJob();
             job.setPrintable(this);
             boolean ok = job.printDialog();
             if (ok) {
                 try {
                      job.print();
                 } catch (PrinterException ex) {
    
                 }
             }
        }
    
        public PrintUIWindow(JFrame f) {
            frameToPrint = f;
        }
    
        public static void main(String args[]) {
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            JFrame f = new JFrame("Print UI Example");
            f.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {System.exit(0);}
            });
    
            JLabel label1=new JLabel("Selling Bill",JLabel.CENTER);
            JLabel label2=new JLabel("Customer Name :Mahmoud Saleh       ",JLabel.LEFT);
            JLabel label3=new JLabel("Buying Date :29/8/2008             ",JLabel.LEFT);
            JLabel label4=new JLabel("Book Buyed :Java Printing          ",JLabel.LEFT);
            JLabel label5=new JLabel("Number : 6 Copies                  ",JLabel.LEFT);
            JLabel label6=new JLabel("Total Price :600 $                 ",JLabel.LEFT);
    
            label1.setFont(new Font("Courier New", Font.BOLD, 13));
            label2.setFont(new Font("Courier New", Font.BOLD, 13));
            label3.setFont(new Font("Courier New", Font.BOLD, 13));
            label4.setFont(new Font("Courier New", Font.BOLD, 13));
            label5.setFont(new Font("Courier New", Font.BOLD, 13));
            label6.setFont(new Font("Courier New", Font.BOLD, 13));
    
            JButton printButton = new JButton("Print This Window");
            printButton.addActionListener(new PrintUIWindow(f));
            JPanel panel=new JPanel();
            panel.setLayout(new GridLayout(6,1));
            panel.add(label1);
            panel.add(label2);
            panel.add(label3);
            panel.add(label4);
            panel.add(label5);
            panel.add(label6);
            f.setSize(300,300);
            f.setLocationRelativeTo(null);
            f.add(panel,BorderLayout.CENTER);
            f.add(printButton,BorderLayout.SOUTH);
            panel.setBackground(Color.WHITE);
            f.setResizable(false);
            f.setVisible(true);
        }
    }
    
    --------------------------------------------------------------------
    
    
    and i also created a small frame using netbeans and it's also working..  
    hope this will help u guys too....  
    
    /*
     * 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">
        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("Hello...");
    
            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>
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // 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());
               }
            }
        }                                        
    
        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
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        // End of variables declaration
    
    
    }
    Last edited by Eranga; 03-31-2010 at 01:36 PM. Reason: added code tags

  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

    Fine, if you are satisfied with what you have mean you found the solution. If so please mark the thread solved. And also please don't forget to use code tags next time. Good luck! :)

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

    Default

    Could you please show me how to make this thread as solved. ?

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

Similar Threads

  1. how to print
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 03-18-2010, 02:23 PM
  2. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  3. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  5. Print A Pdf
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 05:40 AM

Posting Permissions

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