How do u print a JPanel with all of its contents??

I have produced a java application and want to add printing facilities to the application.
I want the user to be able to a print a page (JPanel) which consists of JLabel's, JTextarea's and JTable's. How can this be done????
Thank you,
Yuvraj.

Here is an example using the printer class I sent you the other day:
package com.cpm.printertest.applet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
* Applet
* <P>
* @author *
public class CPrinterTest extends JApplet {
   boolean isStandalone = false;
   JPanel jPanel1 = new JPanel();
   JCheckBox jCheckBox1 = new JCheckBox();
   JEditorPane jEditorPane1 = new JEditorPane();
   JList jList1 = new JList();
   JLabel jLabel1 = new JLabel();
   JButton jButton1 = new JButton();
    * Constructs a new instance.
    * getParameter
    * @param key
    * @param def
    * @return java.lang.String
   public String getParameter(String key, String def) {
      if (isStandalone) {
         return System.getProperty(key, def);
      if (getParameter(key) != null) {
         return getParameter(key);
      return def;
   public CPrinterTest() {
    * Initializes the state of this instance.
    * init
   public void init() {
      try  {
         jbInit();
      catch (Exception e) {
         e.printStackTrace();
   private void jbInit() throws Exception {
      this.setSize(new Dimension(400, 400));
      jCheckBox1.setText("jCheckBox1");
      jCheckBox1.setBounds(new Rectangle(205, 34, 90, 25));
      jEditorPane1.setText("jEditorPane1");
      jEditorPane1.setBounds(new Rectangle(123, 104, 152, 151));
      jList1.setBounds(new Rectangle(55, 25, 79, 37));
      jLabel1.setText("jLabel1");
      jLabel1.setBounds(new Rectangle(161, 285, 98, 17));
      jButton1.setText("jButton1");
      jButton1.setBounds(new Rectangle(160, 330, 79, 27));
      jButton1.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
            jButton1_actionPerformed(e);
      jPanel1.setLayout(null);
      this.getContentPane().add(jPanel1, BorderLayout.CENTER);
      jPanel1.add(jCheckBox1, null);
      jPanel1.add(jEditorPane1, null);
      jPanel1.add(jList1, null);
      jPanel1.add(jLabel1, null);
      jPanel1.add(jButton1, null);
    * start
   public void start() {
    * stop
   public void stop() {
    * destroy
   public void destroy() {
    * getAppletInfo
    * @return java.lang.String
   public String getAppletInfo() {
      return "Applet Information";
    * getParameterInfo
    * @return java.lang.String[][]
   public String[][] getParameterInfo() {
      return null;
    * main
    * @param args
   public static void main(String[] args) {
      CPrinterTest applet = new CPrinterTest();
      applet.isStandalone = true;
      JFrame frame = new JFrame();
      frame.setTitle("Applet Frame");
      frame.getContentPane().add(applet, BorderLayout.CENTER);
      applet.init();
      applet.start();
      frame.setSize(400, 420);
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
      frame.setVisible(true);
      frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
   static {
      try {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      catch(Exception e) {
         e.printStackTrace();
   void jButton1_actionPerformed(ActionEvent e) {
      CPrintUtilities p = new CPrintUtilities();
      p.AddPage(jPanel1);
      p.print();
}

Similar Messages

  • How can I Print a JPanel including all added Components?

    Hello dear saviours,
    I have a JPanel object which is responsible for displaying a Graph.
    I need to Print the Components of this JPanel as close to what they look like to the user as possible.
    I thought about casting the JPanel into an Image of some kind, but couldn't find anything but how to add am Image to a JPanel (God damned search engines :-).
    But wait, this gets more interesting!
    I need to have control over the scale of the Printed matterial.
    I want the option to choose between a single page and dividing the drawing the JPanel displays into Multiple-Pages.
    In both cases I need full details of all the data (Nodes, Arcs, Text describing the various Nodes and Arcs names or type, etc.).Keeping the sizes of all these components is also important (that means, I don't want the nodes to be tinny-winny and the Fonts to be twice as large as the nodes and so on...).
    Attached is the code of the PrintUtillity class I created as an API for printing the JPanel data:
    package ild;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.print.*;
    /** A simple utility class that lets you very simply print
    * an arbitrary component. Just pass the component to the
    * PrintUtilities.printComponent. The component you want to
    * print doesn't need a print method and doesn't have to
    * implement any interface or do anything special at all.
    * <P>
    * If you are going to be printing many times, it is marginally more
    * efficient to first do the following:
    * <PRE>
    * PrintUtilities printHelper = new PrintUtilities(theComponent);
    * </PRE>
    * then later do printHelper.print(). But this is a very tiny
    * difference, so in most cases just do the simpler
    * PrintUtilities.printComponent(componentToBePrinted).
    * 7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
    * May be freely used or adapted.
    public class PrintUtilities implements Printable {
    private Component componentToBePrinted;
    public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
    public static void printComponent(Component c) {
    new PrintUtilities(c).print();
    public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    if (printJob.printDialog())
    try {
    printJob.print();
    } catch(PrinterException pe) {
    System.out.println("Error printing: " + pe);
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
    return(NO_SUCH_PAGE);
    } else {
    Graphics2D g2d = (Graphics2D)g;
    // double scale = 2;
    // g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    double scaleFactor = java.lang.Math.min((double)this.componentToBePrinted.getSize().width/(double)(pageFormat.getImageableWidth()),
    (double)this.componentToBePrinted.getSize().height/(double)(pageFormat.getImageableHeight()));
    g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    g2d.scale(1.0/scaleFactor,1.0/scaleFactor);
    // disableDoubleBuffering(componentToBePrinted);
    componentToBePrinted.paint(g2d);
    enableDoubleBuffering(componentToBePrinted);
    return(PAGE_EXISTS);
    /** The speed and quality of printing suffers dramatically if
    * any of the containers have double buffering turned on.
    * So this turns if off globally.
    * @see enableDoubleBuffering
    public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
    /** Re-enables double buffering globally. */
    public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);

    That's a nice utility, but my JPanel gets truncated when printed. Is there a way to print a JPanel in full.

  • How to keep the repeating table and all of its contents on the same page

    Hi All,
    I use 5.6 build version of xml publisher. My problem is about, repeating table in rtf is divided, so i want to keep the object and its contents on the same logical page.
    How it can be done?
    Thanks.

    I'm glad to hear someone who doesn't have this problem. I tried that to make a connection between my phone and PC, but then I am unable to accomplish that: it says (on my phone) how I'm not playing something... :@ I mean "WTF, it is a bluetooth...".
    I did disable the software from starting, but I is not all of the software - it is all around me: in main many, in control panel... And I can smell some other processes running!
    Also, the computer continues to restart it self for no reason, and it has to do something with installation of Toshiba BT...
    I will remove this thing from my PC even if that meant reinstalling operating system!
    No, I don't have Toshiba notebook... Maybe I had, if that was one part of the uninstalled components.
    I don't get it, it looks like Toshibas developers wanted to scr*w with us - why did they make it so hard to remove all of those components. The internet is full of peoples post who had the same problem as I do!

  • How do i print a pdf with markups made on an ipad?

    I make markups, comments and pop out notes on a pdf from an ipad.  How do I print that document with all the comments showing up??

    [topic moved to iOS subforum]

  • How can I print a JTable with varying sized rows?

    How can I print a JTable with varying sized rows?
    I am using java 1.5, and have made a cell renderer to display multiple lines in rows and it works well, however when I use the new print method it doesn't separate the cells onto the next page, it cuts them and will print the rest of the row on the next page. Does anyone know how I might go about making this happen?
    cheers

    I finally found out the reason, The column heights were being set through my renderer. If I hadn't viewed them in the scrollpane the row heights were set to the default height of 16 therefore when the print method was called it clipped all the rows wrong, what is weird though is that the row height printed out correctly but the clipping area was set wrong because it was getting the default value of 16 from rowHeight(row).

  • How can I print to hp photosmart all in one 7280 from ipad

    How can I print to hp photosmart all in one 7280 from ipad
    This question was solved.
    View Solution.

    Hi,
    Although the C7280 is not AirPrint enabled, you may still print through the local network using the HP ePrint mobile app.
    you may find the steps to get and use the app in the following document:
    http://h10025.www1.hp.com/ewfrf/wc/document?docname=c02775166&cc=us&dlc=en&lc=en&product=3204785&tmp...
    Say thanks by clicking the Kudos thumb up in the post.
    If my post resolve your problem please mark it as an Accepted Solution

  • How can I print a file with mixed page orientation in windows 8.1?

    I have a win 7 and a win 8.1 computer.  I have a file which contains both landscape and portrait pages.  The file prints correctly with the mixed orientation from the win 7 pc, but will only print with either landscape or portrait on the win 8.1 pc. 
    I am using Adobe reader XI on the win 7 pc and adobe touch on the win 8.1 pc

    ส่งจาก จดหมายของ Windows
    จาก: Pat Willener
    ส่งเมื่อ: จ. 5 มกราคม 2558 6:15
    ถึง: thang dinhvan
    How can I print a file with mixed page orientation in windows 8.1?
    reply from Pat Willener in Adobe Reader Touch for Windows 8 - View the full discussion 
    I have a win 7 and a win 8.1 computer.  I have a file which contains both landscape and portrait pages.  The file prints correctly with the mixed orientation from the win 7 pc, but will only print with either landscape or portrait on the win 8.1 pc. 
    I am using Adobe reader XI on the win 7 pc and adobe touch on the win 8.1 pc
    If the reply above answers your question, please take a moment to mark this answer as correct by visiting: https://forums.adobe.com/message/7064031#7064031 and clicking ‘Correct’ below the answer
    Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:
    Please note that the Adobe Forums do not accept email attachments. If you want to embed an image in your message please visit the thread in the forum and click the camera icon: https://forums.adobe.com/message/7064031#7064031
    To unsubscribe from this thread, please visit the message page at , click "Following" at the top right, & "Stop Following"
    Start a new discussion in Adobe Reader Touch for Windows 8 by email or at Adobe Community
    For more information about maintaining your forum email notifications please go to https://forums.adobe.com/thread/1516624.

  • How do I print a list of all my book marks

    How do I print a list of all my book marks ?
    Thanks,
    Earoseme

    Your realize many links to websites have extremely long links.  Do you just want the text of the link without the actual http:// address?

  • How to tack print out use with Query ?

    Hi
    I have to tack purchase order print out (print details- PO date, BP code, name, item name, Quantity and Amount . System Print Layout means from Purchase Order layout designer) use with Query
    How to tack print out use with Query (system print layout means from  Purchase Order layout designer) ?
    Regards
    Aravind M

    Hi
    I have to take purchase order print out and I created 3 templates for that purchase order in layout designer (Because as per the MIS requirement I created 3 templates, its cover more information). I have to take print out 3 templates at a time. So i try to take print use with Query.
    Regards
    Aravind M

  • How do I print .pdf documents with reader for windows 8?

    I have Reader for windows 8.  How can I print .pdf documents with it? 

    See FAQ: Printing from Adobe Reader for Windows 8 Tablets.

  • How to create full new user with all privileges

    how to create full new user with all privileges?
    and how to delete existing users?
    Thanks in advance..

    Common solution is probably to use sudo for privilege elevation, wiki should help

  • I downloaded garage band on my iPad ages ago but now it just says waiting and when I click on it it just says "retry" and does nothing. I've spent literally ages making songs (I've got like 20 ). How can I get it back with all my songs still there?

    I downloaded garage band on my iPad ages ago but now it just says waiting and when I click on it it just says "retry" and does nothing. I've spent literally ages making songs (I've got like 20 ). How can I get it back with all my songs still there?

    Thanks for that. Much more constructive than the last comment. It's only the restriction code I can't recall, not the access passcode. So I can currently access the device, just not age restricted content. Does that's make a difference? I also wondered if anyone knew how many attempts you get to try to get it right. Now tried 21 times and so far nothing bad has happened but I am concerned I'll eventually be completely locked out of the device. That doesn't seem in the spirit of things though. Surely it's foreseeable that a child could repeatedly try to guess the code so I can't see that it would be right to lock the device down completely in that circumstance, particularly if the access code is being typed in correctly every time.
    Thanks

  • How do you print a playlist with the new itunes?

    How do you print a playlist with the new itunes????

    You can click on the little triangle that's next to the little box at the top left of the new iTunes. It will give you a drop down box which gives you several selections. One of the selections is "Show Menu Bar". Click on that and you get the bar back that was on the previous version. Then in "file" you can print a CD jewel case list just like before!

  • How can I save a page with all the links for offline reading

    how can I save a page with all the links for offline reading

    Maybe '''Pocket''' is what you want?
    [https://addons.mozilla.org/en-US/firefox/addon/read-it-later/?src=search Pocket (Mozilla Addons)]

  • How can I print only the attachment without the email content

    How can I print only the attachment without the email content. Every time I send to print, It prints the email itself with the attachment- can I cancel that?

    Hello Riklama,
    When you first open the attachment (e.g. on your phone) than you can mail only that attachment. Works fine with my husbands blackberry.
    Elsy

Maybe you are looking for