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.

Similar Messages

  • 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 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();
    }

  • How can I print a list of all folders/directories on my MacBook Pro?

    I need to print a list of all directories on my MacBook Pro.  Is there a way to do this already on my Mac, or can anyone recommend the best software to do this?

    And if you pipe the result of Charlie's suggestion to a file, you have it printed out:
    in the Terminal you enter following command first:
    cd <enter>
    now you are in the root folder ... and then:
    sudo ls -R */ | grep '/$' | cat > ~/Desktop/folders.txt <enter>
    to explain:
    certain folders are only readable by the administrator. That means, we precede this command by sudo; you have to enter your password
    ls means list, the switch -R means recursive
    | means pipe the command to an other command and grep is taking only the entries finishing with a / (that means folders only, not the files
    then we | pipe everything to the command cat, which puts the found folders into a file on the desktop ...
    hope this helps

  • How can I print the worksheets included in an iBook?

    A book purchased from iBooks has worksheets available for use. I would like to print them. How can I do this?

    On the normal vCard in Contacts I wrote in the name of the foreign country, for example:
    Michael Meyer
    13 Kinsington Road
    VT Worthing
    Great Britain
    If I want to print this on an envelope there is no Country in the preview, how can I get in this Example Great Britain on the envelope?

  • In Contacts version 8, how can I print ALL information in each individual card? When I select the print command the only thing printed is the name and address. I need phone number(s) and all other information in the cards.

    In Contacts version 8, how can I print ALL information in each individual card? When I select the print command the only thing printed is the name and address. I need phone number(s) and all other information in the cards. We enter various pieces of data, other than the standard name address & phone numbers and we print all information on each card so it fits in a 5x7 inch loose binder. We have used InTouch software for many years and it has served us extremely welll, however, the publisher (The Prairie Group) has not, and apparently has no plans to update their software to be compatible with any Mac OSX OS beyond 10.6. Any help will be appreciated!

    You can select what you want included in a list format. In the Print command from Contacts, click the Show Details button. Then in the Style pulldown menu select "Lists" and there you'll be able to select what you want included. You can also select what you wish included if you select the Pocket Address Book style.
    If neither of those options will work for you, then you will need to look to third-party software. Here's one possibility that seems to get good reviews:
    https://www.macupdate.com/app/mac/15485/labels-&-addresses
    I haven't done more than try it to make sure that it works with OS X 10.9's Contacts, which it does, but you can download their demo and try it yourself.
    Regards.

  • How can i print all the contect of the code in sapscript window ? ?

    how can i print all the contect of the code in sapscript window ? ?

    Hi,
    Do you mean that you want to print the ABAP code to SAPscrip form ?
    Svetlin

  • How can i print all the tab pages not just the first page (tab)

    how can i print all the tab pages not just the first page (tab)

    You would need to do this programmatically. Here's one way:
    Attachments:
    Example_VI_BD6.png ‏3 KB

  • How can I print multiple copies of the same photo onto one sheet of paper?  Do I have to Duplicate the photo in iPhoto and then select them all?

    How can I print multiple copies of the same photo onto one sheet of paper?  Do I have to Duplicate the photo in iPhoto and then select them all?

    no - you simply have to select the option to print mucliples of a photo on a page
    select the photo and go to the file menu ==> print - select the printer, paper size and print size and click customize - in the tool bar click on the settings icon (the gear looking thingy) and select "multiple of the same photo per page" and the preview will reflect this option showing a full page of the selected size photos
    LN

  • I have a iphone 5c and a wireless computer hooked to a usb wireless printer Hp4215 officejet all-in-one how can I print from my phone to the printer

    How can I print to my wireless Hp officejet 4215 all-in-one printer that is usb in line with my computer that uses windows 7 from my Iphone 5c

    there are 2 ½ ways to print
    1. the printer support AirPrint and it works out of the box
    2. one get a printing app from app store which support the printer
    2-½ one get the free software from netgear called netgear genie which will make the computer display all printes it's know act as AirPrint printers
    no other things in the known universe will make printing possible

  • How can I print from my ipad2 to my hp6210 all in one using printopia...I can see the list on my ipad2, I select the printer and say print but nothing happens.

    How can I print from my ipad2 to my hp 6210 all in one using printopia? I see my printer on my iPad, I select it and say print and nothing happens. The printer is connected to an older Mac with OS X 10.4.11 but I do have an imac in another room. I have trouble printing from the iMac to this printer also even tho share is on but the printer works fine with the old Mac it's connected to. My iPad has the latest iOS.

    A few things to consider.
    The computer Printopia is loaded on must be powered on and must be able to print on the printer that is being shared.  If the iMac is having problems printing to the printer it could indicate a problem with the share setup.

  • How can i print logo in all the page

    i've created a form(SAP script) which have  five page.pls can anybody tell me how can i print logo in all the five page?

    hi,
    u can try this in
          windows
    change window type as CONST or MAIN.
    cheers,
    sam

  • How can I print to my HP OfficeJet 6500 Wireless Printer from my iPhone 4S which used to work before AirPrint printers were even available? (running Mac Lion OS, all software and HP sw updates are all performed)

    How can I print to my HP OfficeJet 6500 Wireless Printer from my iPhone 4S which used to work (on prior iPhones) before AirPrint printers were even available? (running Mac Lion OS, all software and HP sw updates are all performed)
    I don't get it, I read one post about why would we have to buy an AirPrint Printer when we have used the "print to a wifi printer" just fine in the past?  Yes I feel that's exactly what's happening to me now too. I want to print from my iPhone and iPad to my WiFi Printer which is also now connected to my Airport Express WiFi network just fine. No problems.

    Very glad you added that bit of information because I see different problems similar to this.  I want to share with them what you did and hopefully you not only fixed your specific problem but you can lend some more advice to others with problems like this. Thanks!
    Don't forgot to say thanks by giving "Kudos" if I helped solve your problem.
    When a solution is found please mark the post that solves your issue.
    Every problem has a solution!

  • I have a business and I use iCal for all my appointments, how can I print receipts from my MacBook to a receipt printer? I want my clients to have a receipt of the services that they have paid for, can anyone HELP ME PLEASE?

    I have a business and I use iCal for all my appointments, how can I print receipts from my MacBook to a receipt printer?
    I want my clients to have a receipt of the services that they have paid for, can anyone HELP ME PLEASE?

    Well...I went to the modem (Westell, WireSpeed), found the NAT settings, once again, I'm WAY over my head, I am assuming this is a TCP connection (as opposed to a UDP) and per Lorex my mobile devices will use port 1025.  So I gave it a "global port range" of 1-10 and I indicated that the "base host port" was 80, 1025, & 9000 (ports 1,2,3).  When I selected the 'enable' it asked for a "host devise" my choices are my IPhone, IMac and the IP address for the dvr, so I choose the dvr.  I still cannot connect and canyouseeme still can NOT find these open ports.  This is taking up my whole day! I don't know how people figure this stuff out.

  • HT2486 how can I print all my address book?

    How can I print all my address book?

    There is a bug with printing Contacts in ML. If you print from iCloud, I'm told it works.
    You can file feedback here.

Maybe you are looking for

  • [SOLVED]/etc/ssl/certs/ca-certificates.crt missing from fresh install?

    Hi! I was wondering if any of you could understand why I need to reinstall ca-certificates post-install, so as /etc/ssl/certs/ca-certificates.crt gets generated back? I'm installing from a netinstall x86_64 image with automatic AIF profile and from [

  • Unwanted text outline colour in highlighted button state

    Hi DVDSP geniuses and much thanks in advance! I'm getting unwanted colour at the edges of text in buttons in selected and activated states. I'm using a non-layered menu and a greyscale jpeg for background, a greyscale jpeg for overlay with simple col

  • LAN-LAN connections

    I have a dual router setup with comcast internet modem connected to E4200 (doing DHCP) this is connected (Wired) to an E3000 (setup as access point with cable from lan port to lanport) per the linksys instructions.  The IP seg is 10.0.1.x with the E4

  • Where can I buy the snow leopard software disc?

    I'm trying to upgrade my MacBook Pro but in order to do that I have to install the Snow Leopard version. Now when I asked a worker at the Apple store where do I get this from, he told me to look online for the installation disc.. I can't find a the i

  • Illustrator : Problème avec pathfinder

    Bonjour, Lorsque je "pathfinde" (toutes opérations confondues du pathfinder), le trait des deux formes (qui était en dessous d'1 pt d'épaisseur) se change de lui même en 1pt d'épaisseur. Si je décide de le changer en 0,5pt il repasse automatiquement