Results 1 to 2 of 2
Thread: Printing the current window
- 08-17-2011, 12:09 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Printing the current window
I am hoping that this is a easy one.
I have written an app which once the various fields completed by the user needs to be printed.
I have a jmenubar at the top with the usual "File" jmenu top left which when clicked drops down a list of Jmenuitems.
I am calling other classes on most items in the list but the one I am stuck on is how to print.
The layoout of the screen is perfect and just needs to be sent "as is" to the printer.
I have set the listener on the jmenuitem thus:
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 1094, 19);
desktopPane.add(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
mntmPrint = new JMenuItem("Print");
mntmPrint.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
PrintUtilities.printComponent(componentToBePrinted )
}
});
mnFile.add(mntmPrint);
And I am using this class to do the printing.
package Navigation;
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, Java Programming Resources -- Java, Java, and more Java
* May be freely used or adapted.
*/
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);
}
}
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);
}
}
/** 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);
}
}
My problem is that I get the following error.
The method setPrintable(Printable) in the type PrinterJob is not applicable for the arguments (new MouseAdapter(){})
I am new to Java and just don't know where to start to solve this problem.
Please could somebody point me in the right direction.
Thank you very much.
PaulLast edited by Paul_White; 08-17-2011 at 12:12 PM.
- 08-17-2011, 12:18 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Get a PrinterJob (read the API documentation for that class) and feed it an implementation of a Printable (it's an interface). The Printable receives a (special) Graphics2D object and it should make your JComponent paint itself on the Graphics2D object. (make sure you disable double buffering for the JComponent while printing). Some objects (JTextComponents and JTables can do this all for you but you have to implement the above yourself for other JComponents).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
how to disable the parent window when child window is open
By ayushi in forum Java ServletReplies: 1Last Post: 07-25-2011, 10:24 AM -
Need to pass a value from a parent window to a pop up or child window
By blackpanther in forum Advanced JavaReplies: 4Last Post: 01-10-2010, 07:48 AM -
change url in parent window from child window
By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-09-2009, 12:17 PM -
[SOLVED] printing address of current instance of object?
By emceenugget in forum New To JavaReplies: 1Last Post: 02-09-2009, 09:36 PM -
To get the current active window`s path using Windows API
By jihadrh in forum Advanced JavaReplies: 8Last Post: 12-01-2008, 02:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks