Results 1 to 2 of 2
  1. #1
    slamdon is offline Member
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Question print out jpanel

    hi i am a new comer of java, i had been facing a little problem through printout all component of a JPanel including JLabel,JTextField etc.But as a output it only shows the first element (lblName) which is not i want so.I want to print the whole JPanel & all of it's component, Please help me. Here is my code:



    package package1;

    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.RepaintManager;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.print.PageFormat;
    import java.awt.print.Printable;
    import java.awt.print.PrinterJob;

    public class test {

    private JFrame frm;
    private JPanel panel;
    private JTextField textField;
    private JTextField textField_1;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    test window = new test();
    window.frm.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the application.
    */
    public test() {
    initialize();
    }

    /**
    * Initialize the contents of the frame.
    */
    private void initialize() {
    frm = new JFrame();
    frm.setBounds(100, 100, 554, 405);
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    frm.setVisible(true);
    frm.setSize(554, 405);
    frm.getContentPane().setLayout(null);

    panel = new JPanel();
    panel.setBounds(206, 79, 232, 152);
    frm.getContentPane().add(panel);
    panel.setLayout(null);
    panel.setSize(232, 152);
    panel.setVisible(true);

    JLabel lblName = new JLabel("NAME");
    lblName.setBounds(27, 43, 46, 14);
    panel.add(lblName);

    JLabel lblRoll = new JLabel("ROLL");
    lblRoll.setBounds(27, 97, 46, 14);
    panel.add(lblRoll);

    textField = new JTextField();
    textField.setBounds(123, 40, 86, 20);
    panel.add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setBounds(123, 94, 86, 20);
    panel.add(textField_1);
    textField_1.setColumns(10);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat pf = pj.pageDialog(pj.defaultPage());
    pj.setJobName(" Print Component ");

    pj.setPrintable (new Printable() {
    @Override
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
    return(NO_SUCH_PAGE);
    }

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

    double xScale = 1.63;
    double yScale = 1.54;

    g2d.scale(xScale, yScale);

    disableDoubleBuffering(panel);

    enableDoubleBuffering(panel);
    panel.printAll(g);

    return(PAGE_EXISTS);



    }

    private void disableDoubleBuffering(JPanel panel) {
    RepaintManager currentManager = RepaintManager.currentManager(panel);
    currentManager.setDoubleBufferingEnabled(false);

    }

    private void enableDoubleBuffering(JPanel panel) {
    RepaintManager currentManager = RepaintManager.currentManager(panel);

    currentManager.setDoubleBufferingEnabled(true);

    }


    });
    if (pj.printDialog() == false)
    return;

    try {
    pj.print();
    } catch ( java.awt.print.PrinterException ex) {
    // handle exception
    }
    }
    });
    btnNewButton.setBounds(334, 278, 89, 23);
    frm.getContentPane().add(btnNewButton);
    }}

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: print out jpanel

    Post moved to its own thread.
    Please edit the post and wrap the code in code tags:
    BB Code List - Java Programming Forum - Learn Java Programming
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. help making class print in jpanel
    By Grid_iso in forum New To Java
    Replies: 1
    Last Post: 05-19-2011, 02:39 PM
  2. Replies: 11
    Last Post: 03-09-2011, 08:18 AM
  3. I would like to print this JPanel class. How?
    By AcousticBruce in forum New To Java
    Replies: 8
    Last Post: 02-26-2011, 06:25 PM
  4. print multiple jpanel
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 02-05-2010, 03:28 PM
  5. print jpanel
    By anilkumar_vist in forum New To Java
    Replies: 3
    Last Post: 01-08-2010, 12:08 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
  •