473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Print image from a JPanel

epots9
1,351 Expert 1GB
I have no clue how to print, but i have this:
Expand|Select|Wrap|Line Numbers
  1. PrinterJob print = PrinterJob.getPrinterJob();
  2.             boolean okToPrint = print.printDialog();
  3.             if(okToPrint)
  4.             {
  5.                 try
  6.                 {
  7.                     print.print();
  8.                 }
  9.                 catch (PrinterException pe)
  10.                 {
  11.                 }
  12.             }
  13.  
I'm trying to print an image that is on a JPanel, the image is loaded and everything but nothing with print...i think i might be missing something.

open to all suggestions,
thank you.
Sep 7 '07 #1
4 3088
epots9
1,351 Expert 1GB
An update, here is my actionlistener that is fired when the print menuitem is clicked:
Expand|Select|Wrap|Line Numbers
  1. private class PrintListener implements ActionListener
  2.     {
  3.         public void actionPerformed(ActionEvent e)
  4.         {
  5.             PrinterJob printJob = PrinterJob.getPrinterJob();
  6.             printJob.setPrintable(imageHolder);
  7.             boolean okToPrint = printJob.printDialog();
  8.             if(okToPrint)
  9.             {
  10.                 try
  11.                 {
  12.                     printJob.print();
  13.                 }
  14.                 catch (PrinterException pe)
  15.                 {
  16.                     System.out.println("nope: " + pe);
  17.                 }
  18.             }
  19.         }
  20.     }
  21.  
the variable imageHolder is a JPanel, it is a class that extends JPanel and implements Printable. The overwritten method is:
Expand|Select|Wrap|Line Numbers
  1. public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
  2.     {
  3.         return 0;
  4.     }
  5.  
I'm still trying to figure out what does in there, because now i am able to print and it prints 1000+ (yes thats crazy) blank pages.

any ideas what i'm missing?
Sep 10 '07 #2
Nepomuk
3,112 Expert 2GB
An update, here is my actionlistener that is fired when the print menuitem is clicked:
Expand|Select|Wrap|Line Numbers
  1. private class PrintListener implements ActionListener
  2.     {
  3.         public void actionPerformed(ActionEvent e)
  4.         {
  5.             PrinterJob printJob = PrinterJob.getPrinterJob();
  6.             printJob.setPrintable(imageHolder);
  7.             boolean okToPrint = printJob.printDialog();
  8.             if(okToPrint)
  9.             {
  10.                 try
  11.                 {
  12.                     printJob.print();
  13.                 }
  14.                 catch (PrinterException pe)
  15.                 {
  16.                     System.out.println("nope: " + pe);
  17.                 }
  18.             }
  19.         }
  20.     }
  21.  
the variable imageHolder is a JPanel, it is a class that extends JPanel and implements Printable. The overwritten method is:
Expand|Select|Wrap|Line Numbers
  1. public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
  2.     {
  3.         return 0;
  4.     }
  5.  
I'm still trying to figure out what does in there, because now i am able to print and it prints 1000+ (yes thats crazy) blank pages.

any ideas what i'm missing?
What should the line PrinterJob printJob = PrinterJob.getPrinterJob(); do? As far as I can say, it should be something like
Expand|Select|Wrap|Line Numbers
  1. Toolkit tools = Toolkit.getDefaultToolkit(); 
  2. PrintJob printJob = tools.getPrintJob( new Frame(), "", null ); 
  3.  
or similar. Could it be, that something (e.g. the line from above) is putting the Printer into an infinite loop? That would of course explain the 1000+ pages.

Greetings,
Nepomuk
Sep 10 '07 #3
epots9
1,351 Expert 1GB
What should the line PrinterJob printJob = PrinterJob.getPrinterJob(); do? As far as I can say, it should be something like
Expand|Select|Wrap|Line Numbers
  1. Toolkit tools = Toolkit.getDefaultToolkit(); 
  2. PrintJob printJob = tools.getPrintJob( new Frame(), "", null ); 
  3.  
or similar. Could it be, that something (e.g. the line from above) is putting the Printer into an infinite loop? That would of course explain the 1000+ pages.

Greetings,
Nepomuk
According to the api, that is how i should create the printerjob object and not call the constructor directly.
Sep 10 '07 #4
Nepomuk
3,112 Expert 2GB
According to the api, that is how i should create the printerjob object and not call the constructor directly.
You mean the description of the constructor? Hm, you're right, it says it that way in this tutorial too. Hm, according to that Tutorial, it should work just fine... I'll have a look at it later, when I have more time and a printer to check it with. In the meantime, could you post the code which regulates, what should be printed? Maybe the error is not in the code you posted but somewhere else?

Greetings,
Nepomuk
Sep 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Moth | last post by:
I have a PlotPanel class that extends JPanel which I have used previously to display charts in swing guis. I now want to display a graph in a jsp page so I was going to generate the graph and then...
1
by: dishal | last post by:
Hi, Im having a problem with typing on the JPanel. The thing is, that it works perfectly fine when this line " content.add(jp, BorderLayout.NORTH); " is taken out (its the buttons panel) but when its...
4
by: blackraven1425 | last post by:
I'm trying to draw an image to a panel, but am having problems with it not showing up. I know I'm creating/loading the image properly, but I have the feeling I'm not painting it right. public...
5
by: xirowei | last post by:
public class Result { private int countA = 0; private int countB = 0; private int statement; private boolean statusA = false; private boolean statusB = false; private int arrayA = new...
8
by: eddiewould | last post by:
Hi, I want to write a custom widget which will act similarly to a JPanel (i.e it can contain other Components), but semantically it's not a kind of JPanel so it shouldn't extend from it. Here is...
1
by: Akino877 | last post by:
Hello, I have a question regarding Java and Swing programming I wonder if I could ask the forum for some help. I have a JPanel that has a couple of radio buttons and an "OK/Next" button on it. ...
1
by: chanshaw | last post by:
Alright so basically I have a logo named "DirSync.jpg" and I want to display it on the top portion of my application window in a container (either JLabel or JPanel which ever is normally recommended)...
1
by: chanshaw | last post by:
How would I go about using an image as a background inside of a JPanel? I want to be able to place components on top of my background image.
2
by: chanshaw | last post by:
Alright I'm trying to place the textarea on top of the image but right now it displays the textarea below the image, how do i go about doing this. import java.awt.Color; import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.