This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Printing using java.awt.PrintJob  RSS feed

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a little program that gets the selected objects (in the form of checkboxes) and when the user desires to view them, it displays the information in a seperate frame. I am trying to get my application to print this information when the user selects the "Print" button but I am not sure how to get the printjob going...
Hope you can help! Thanks in advance...
SCJP2
[ July 14, 2002: Message edited by: Aaron O'Brien ]
 
Ranch Hand
Posts: 67
IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get your data into a JEditorPane or JTextArea which is placed your frame . Now get this JEditorPane or JTextArea to implement the printable interface and containing the print method .
here is some code you can use
package wetprint;
import java.awt.print.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.beans.*;
import javax.swing.text.*;
public class HTMLPrint extends JEditorPane implements Printable
{
public HTMLPrint(String fileName)
//ie fileName="file:/c:/work/testprint.txt"
//you can read from a text file or simply populate the JEditorPane using streams
{

EditorKit kit=new DefaultEditorKit();
try
{
this.setPage(fileName);
}
catch(Exception e)
{
System.out.println("Exception e="+e);
}
}

public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
//for printing the 1st 3 pages
if(pi<=2)
{

Graphics2D g2 = (Graphics2D) g;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.translate(0,-pf.getImageableHeight()*pi);
g2.setClip(0,(int)(pf.getImageableHeight()*pi),(int)pf.getImageableWidth(),(int)pf.getImageableHeight());
Font f = new Font("TimesNewRoman", Font.PLAIN,19);
g2.setFont (f);
return Printable.PAGE_EXISTS;
}
else
{
System.out.println("END OF DOC");
return Printable.NO_SUCH_PAGE;
}
}
}
and the following links would be useful
http://www.spindoczine.com/sbe/files/uts2/Chapter22html/Chapter22.htm
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/advprint.html
[ July 14, 2002: Message edited by: Anamika Singh ]
 
Aaron O'Brien
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anamika...
My application uses java.awt.* not javax....
I have an inner class that extends Canvas and implements Printable. Although our code differed quite differently, your help paid off!
Here is what my paint() looks like...

if(i > 0){
return Printable.NO_SUCH_PAGE;
}else{

Graphics2D g2D = (Graphics2D)g;
g2D.translate(pf.getImageableX(),pf.getImageableY());

paint(g2D);
return Printable.PAGE_EXISTS;
}
and I called...
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(new Ordered());
try{
printJob.print();
}catch(PrinterException p){System.out.println("PrinterException "+p);}
from a listener for my Print Button.
Thanks Again!
Aaron-SCJP2
 
Anamika Singh
Ranch Hand
Posts: 67
IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anytime.....
Printing using canvas is anyday a good idea .It saves u the trouble of having to calculate your document size as in conventional printing methods .
[ July 15, 2002: Message edited by: Anamika Singh ]
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!