akoifman
asked on
Printing javax.swing.JInternalFrame?
Hi,
My javax.swing.JFrame contains a number of internal javax.swing.JInternalFrame 's.
How do I print the contents (widget) of an JInternalFrame?
I do not want to add the code to each JInternalFrame.
I would like to do it from the (parent) JFrame becasue I
have a reference to each JInternalFrame.
Thanks,
Alex.
PS.
I added this code to my JFrame, but it does not work (where javax.swing.JInternalFrame f is a reference to my internal frame); I get a compile error:
PrintJob pjob = this.getToolkit().getPrint Job(f, "Printing Test", null);
// or PrintJob pjob = f.getToolkit().getPrintJob (f, "Printing Test", null);
if (pjob != null) {
Graphics pg = pjob.getGraphics();
if (pg != null) {
this.printAll(pg); // or f.printAll(pg);
pg.dispose();
}
pjob.end();
}
This is the compile error on this line:
PrintJob pjob = this.getToolkit().getPrint Job(f, "Printing Test", null);
Can't convert javax.swing.JInternalFrame to java.awt.Frame
My javax.swing.JFrame contains a number of internal javax.swing.JInternalFrame
How do I print the contents (widget) of an JInternalFrame?
I do not want to add the code to each JInternalFrame.
I would like to do it from the (parent) JFrame becasue I
have a reference to each JInternalFrame.
Thanks,
Alex.
PS.
I added this code to my JFrame, but it does not work (where javax.swing.JInternalFrame
PrintJob pjob = this.getToolkit().getPrint
// or PrintJob pjob = f.getToolkit().getPrintJob
if (pjob != null) {
Graphics pg = pjob.getGraphics();
if (pg != null) {
this.printAll(pg); // or f.printAll(pg);
pg.dispose();
}
pjob.end();
}
This is the compile error on this line:
PrintJob pjob = this.getToolkit().getPrint
Can't convert javax.swing.JInternalFrame
One solution to your problem is to implement interface Printable by your JInternalFrames. If you do that, then you can add a method say printInternal to print their contents. The code for the printInternal method could be:
public void printInternal(javax.swing. JInternalF rame internal) {
try {
PrinterJob pjob = PrinterJob.getPrinterJob() ;
pjob.setPrintable((Printab le)interna l);
if (pjob.printDialog())
pjob.print();
} catch (Exception e) {
e.printStackTrace();
}
}
The implementation of the print method of the Printable interface can be done with the help of the paint(java.awt.Graphics) method of Internal frames.
Hope this helps.
public void printInternal(javax.swing.
try {
PrinterJob pjob = PrinterJob.getPrinterJob()
pjob.setPrintable((Printab
if (pjob.printDialog())
pjob.print();
} catch (Exception e) {
e.printStackTrace();
}
}
The implementation of the print method of the Printable interface can be done with the help of the paint(java.awt.Graphics) method of Internal frames.
Hope this helps.
ASKER
Hi,
Does that mean if I have a number of different JInternalFrames, I have to add this to all of them?
I am using Java 2, JDK 1.2.2.
Thanks,
Alex.
Does that mean if I have a number of different JInternalFrames, I have to add this to all of them?
I am using Java 2, JDK 1.2.2.
Thanks,
Alex.
You can define a PrintableJInternalFrame which extends JInternalFrame and implements Printable. Then you inherit from this class instead.
If you are talking about the print method, it depends; if you only need a generic print for any internal frame, this approach will work. If you need specialized print functions, then you could rewrite the print method when necessary and use the default one elsewhere.
Here is a possible implementation of the generic print method of the Printable interface:
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.g etImageabl eX(), pageFormat.getImageableY() );
disableDoubleBuffering(thi s);
paint(g2d);
enableDoubleBuffering(comp onentToBeP rinted);
return(PAGE_EXISTS);
}
}
AlexJ
If you are talking about the print method, it depends; if you only need a generic print for any internal frame, this approach will work. If you need specialized print functions, then you could rewrite the print method when necessary and use the default one elsewhere.
Here is a possible implementation of the generic print method of the Printable interface:
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.g
disableDoubleBuffering(thi
paint(g2d);
enableDoubleBuffering(comp
return(PAGE_EXISTS);
}
}
AlexJ
Forgot to mention that the code above was also published by diakov as part of an answer in another question.
AlexJ
AlexJ
ASKER
Hi,
Thanks a lot. We are making progress. It took about 15 min. to print a tab panel. Possible reason that I had to comment out xxxDoubleBuffering, because I could not compile your code. I am getting No Method errors. Why?
What is componentToBePrinted?
JComponent c = (JComponent) myFrame.getContentPane();
224. disableDoubleBuffering(thi s);
<------------------------- ->
** Error: No method named "disableDoubleBuffering" was found in
226. enableDoubleBuffering(comp onentToBeP rinted);
<------------------>
Thanks a lot. We are making progress. It took about 15 min. to print a tab panel. Possible reason that I had to comment out xxxDoubleBuffering, because I could not compile your code. I am getting No Method errors. Why?
What is componentToBePrinted?
JComponent c = (JComponent) myFrame.getContentPane();
224. disableDoubleBuffering(thi
<-------------------------
** Error: No method named "disableDoubleBuffering" was found in
226. enableDoubleBuffering(comp
<------------------>
ASKER
Hi,
Thanks a lot. We are making progress. It took about 15 min. to print a tab panel. Possible reason that I had to comment out xxxDoubleBuffering, because I could not compile your code. I am getting No Method errors. Why?
What is componentToBePrinted?
JComponent c = (JComponent) myFrame.getContentPane();
224. disableDoubleBuffering(thi s);
<------------------------- ->
** Error: No method named "disableDoubleBuffering" was found in
226. enableDoubleBuffering(comp onentToBeP rinted);
<------------------>
Thanks a lot. We are making progress. It took about 15 min. to print a tab panel. Possible reason that I had to comment out xxxDoubleBuffering, because I could not compile your code. I am getting No Method errors. Why?
What is componentToBePrinted?
JComponent c = (JComponent) myFrame.getContentPane();
224. disableDoubleBuffering(thi
<-------------------------
** Error: No method named "disableDoubleBuffering" was found in
226. enableDoubleBuffering(comp
<------------------>
Sorry, this was taken from another program. Please replace the two lines about double buffering by
setDoubleBuffer(false);
and
setDoubleBuffer(true);
respectively.
setDoubleBuffer(false);
and
setDoubleBuffer(true);
respectively.
If your performance problem persist after disabling the double buffering, you will probably have to write your own print method for this frame.
ASKER
Hi,
Thanks a lot. We are making progress. It took about 15 min. to print a tab panel. Possible reason that I had to comment out xxxDoubleBuffering, because I could not compile your code. I am getting No Method errors. Why?
What is componentToBePrinted?
JComponent c = (JComponent) myFrame.getContentPane();
224. disableDoubleBuffering(thi s);
<------------------------- ->
** Error: No method named "disableDoubleBuffering" was found in
226. enableDoubleBuffering(comp onentToBeP rinted);
<------------------>
Thanks a lot. We are making progress. It took about 15 min. to print a tab panel. Possible reason that I had to comment out xxxDoubleBuffering, because I could not compile your code. I am getting No Method errors. Why?
What is componentToBePrinted?
JComponent c = (JComponent) myFrame.getContentPane();
224. disableDoubleBuffering(thi
<-------------------------
** Error: No method named "disableDoubleBuffering" was found in
226. enableDoubleBuffering(comp
<------------------>
Hi,
this is the third time we see your previous comment, it seems that reloading the page (or something like that) is making your message to be reinserted.
Please push the Reload Question button near the top right of the page so that this strange page behavior stops.
this is the third time we see your previous comment, it seems that reloading the page (or something like that) is making your message to be reinserted.
Please push the Reload Question button near the top right of the page so that this strange page behavior stops.
ASKER
Hi,
No speed improvement.
"setDoubleBuffer" is not found, so I used "setDoubleBuffered". Should I run in a separate thread? ANy other recomendations? Thanks.
I did this:
setDoubleBuffered(false);
paint(g2d);
setDoubleBuffered(true);
Thanks a lot,
Alex.
No speed improvement.
"setDoubleBuffer" is not found, so I used "setDoubleBuffered". Should I run in a separate thread? ANy other recomendations? Thanks.
I did this:
setDoubleBuffered(false);
paint(g2d);
setDoubleBuffered(true);
Thanks a lot,
Alex.
If using the paint(Graphics) method to print the frame is unacceptable in time you will have to write your own print method by iterating over rows and columns and printing the text of each cell.
The java.awt.Graphics and java.awt.Graphics2D have all what you need in order to achieve your goal.
The java.awt.Graphics and java.awt.Graphics2D have all what you need in order to achieve your goal.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER