Solved

Printing javax.swing.JInternalFrame?

Posted on 2000-03-07
14
692 Views
Last Modified: 2008-02-20
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().getPrintJob(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().getPrintJob(f, "Printing Test", null);

Can't convert javax.swing.JInternalFrame to java.awt.Frame
0
Comment
Question by:akoifman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 

Author Comment

by:akoifman
ID: 2595595
Adjusted points to 200
0
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2596998
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.JInternalFrame internal) {
    try {
      PrinterJob pjob = PrinterJob.getPrinterJob();
      pjob.setPrintable((Printable)internal);
      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.
0
 

Author Comment

by:akoifman
ID: 2597102
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2597149
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.getImageableX(), pageFormat.getImageableY());
      disableDoubleBuffering(this);
      paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
    }
  }


AlexJ
0
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2597197
Forgot to mention that the code above was also published by diakov as part of an answer in another question.

AlexJ
0
 

Author Comment

by:akoifman
ID: 2597519
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(this);
             <-------------------------->
** Error: No method named "disableDoubleBuffering" was found in


  226.       enableDoubleBuffering(componentToBePrinted);
                                   <------------------>
0
 

Author Comment

by:akoifman
ID: 2597549
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(this);
             <-------------------------->
** Error: No method named "disableDoubleBuffering" was found in


  226.       enableDoubleBuffering(componentToBePrinted);
                                   <------------------>
0
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2597560
Sorry, this was taken from another program. Please replace the two lines about double buffering by

setDoubleBuffer(false);

and

setDoubleBuffer(true);

respectively.
0
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2597599
If your performance problem persist after disabling the double buffering, you will probably have to write your own print method for this frame.
0
 

Author Comment

by:akoifman
ID: 2597753
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(this);
             <-------------------------->
** Error: No method named "disableDoubleBuffering" was found in


  226.       enableDoubleBuffering(componentToBePrinted);
                                   <------------------>
0
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2597827
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.
0
 

Author Comment

by:akoifman
ID: 2597981
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.
0
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2598026
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.
0
 
LVL 2

Accepted Solution

by:
AlexJ030200 earned 200 total points
ID: 2601365
A final comment.

There is a good discussion about performance of component printing in the java development connection. Maybe there are other ideas there.

http://developer.java.sun.com/developer/qow/archive/20/index.html 
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 40
Java Restore security prompts not working 10 140
import as existing maven project 3 41
Netbeans and org.apache.commons.lang3 issue 3 28
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question