jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes Printing on JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Printing on JTextArea" Watch "Printing on JTextArea" New topic

Printing on JTextArea

Jan Michael Soan
Ranch Hand

Joined: Feb 08, 2003
Posts: 36
I dont really know how to print on JTextArea, is there anyway I could print in the printer the contents of JTextArea.
I really need some codes on how to print it in the printer, the easier way.
For example :
JTextArea a1 = new JTextArea(" Hello World ! ");
Pls. Help, Urgent . . .
>> Hello World, is what I wanted to print on the printer by just using JTextArea.
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
I found this: http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-print_p.html
There is a section : The Printing Recipe. The API will probably help, im not sure is Strings implement Printable.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
I made this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
public class PrintObject{
JTextArea text;
public static void main (String[] args){
PrintObject printgui = new PrintObject();
public void go(){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
text= new JTextArea(10,20);
JScrollPane scroller = new JScrollPane(text);
JButton printbutton = new JButton("Print me");
printbutton.addActionListener(new PrintButtonListener());
public class PrintButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
//MyString printing = new MyString(text.getText());
public void printPage(String textToPrint){
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat landscape = job.defaultPage();
Book bk = new Book();
bk.append(new PaintCover(), job.defaultPage());
//bk.append(new PaintContent(), landscape);
//job.setPrintable(new PaintCover(), job.defaultPage());
if (job.printDialog()) {
try {
}catch (Exception exc){
class PaintCover implements Printable{
Font fnt = new Font("Helvetica-Bold", Font.PLAIN, 48);
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
g.drawString(text.getText(), 100, 200);
return Printable.PAGE_EXISTS;
After Reading this: http://java.sun.com/docs/books/tutorial/2d/printing/index.html
Most of the code is nicked from the example in the last Page.
Jan Michael Soan
Ranch Hand

Joined: Feb 08, 2003
Posts: 36
Dear Tom Hill;
Thank you very much for giving me some codes, I really appriciate it. We have the same coding format and thats a good thing cause I'm not gonna have problem understanding it.
I'll try my best to try it okey. I'll let you know if it worked for me.
Thank you, the reason I asked for a print in JTextArea is because I've developed a payroll software made in java with jdbc connectivity using microsoft access.
I've developed it well and spent a lot of time in it by developing sql commands for password, username and a whole lot more. I've also developed its splash screen and add music capability and word processing. I've also created its GUI interface with 2x much better with swing.
I've also created an installer for my software using installanywhere this software was the software that I defended in my thesis way back in college.
My professor liked it at 1st but he critisize me because my program doesnt have printing capability, Since at start I was having a problem with the printing api of JAVA I cant seem to understand it. I was posting some questions before but your the only one who gave me a code, thanks a lot dude. I'll give it a try.
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
No problem, It was the first time Id written a bit of code to print anything and I had never seen it in any books! I am suprised at how difficult it is to do, I would have thought it would have been similar to a Streaming object like file handelling. The Objects required all seem to make sense though.
Ive started to play around with a - make text appear where a user wants it application that allows a user to print whats on screen after making that application! Its pretty naff.
Thanks for asking a question thats let me find something new out!
Jan Michael Soan
Ranch Hand

Joined: Feb 08, 2003
Posts: 36
Thank you very much, here's my own format.
This codes is just for printing test on JTextArea.

[ September 15, 2003: Message edited by: Jan Michael Soan ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Printing on JTextArea
It's not a secret anymore!