Results 1 to 2 of 2
Thread: Please help in prining JPanel
- 05-20-2011, 08:28 PM #1
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
Please help in prining JPanel
Hey friends,
I want to print a JPanel along with its component for which i have my PrintUtilities class as I attached it.
Here the problem is that whenever i want to print the jPanel i have to passed it through parameter of printComponent(Component) method and it shows me the print dialog.
What i actually want is to show the printDialog only once in running application for printing setup and after that it directly prints the component whenever i call the printComponent() method.
Please suggest me what i have to change in my code???
Please help me...
- 05-20-2011, 08:30 PM #2
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
Here is the same code what i have attached before:
package Classes;
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
public static void printComponent(Component c) {
new PrintUtilities(c).print();
}
public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
}
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);
}
}
@Override
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(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
}
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
Similar Threads
-
Adding a jpanel to a customized Jpanel Class
By trishtren in forum AWT / SwingReplies: 7Last Post: 04-05-2011, 06:52 PM -
Adding Jpanel ontop of another Jpanel
By Manfizy in forum AWT / SwingReplies: 4Last Post: 03-05-2011, 10:34 PM -
Placing a new JPanel over a paint overriden JPanel
By Tanshaydar in forum AWT / SwingReplies: 4Last Post: 12-08-2010, 06:00 PM -
Need to Save a data to file from JPanel and read the data back to Jpanel
By yashrajsen in forum AWT / SwingReplies: 1Last Post: 11-09-2010, 09:28 AM -
.add to a JPanel
By harrier in forum NetBeansReplies: 11Last Post: 07-13-2010, 10:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks