-
December 6th, 2001, 12:41 AM
#1
I want to implement a print button that prints a textarea...
Hi everyone!
I need to implement a Print button to print the text contained on a JtextArea, does anyone can help me do it? I've used the following code, but it doesn,t work:
else if(command == "Print Code...")
{
PrinterJob printJob = PrinterJob.getPrinterJob();
String text = codeTextArea.getText();
printJob.setPrintable(text);
if (printJob.printDialog()) {
}
}
the compiler gives me the following error on it
JavaFFT.java:349: cannot resolve symbol
symbol : method setPrintable (java.lang.String)
location: class java.awt.print.PrinterJob
printJob.setPrintable(text);
^
1 error
Also, I dont know how to specify that I want to print the JTextArea named codeTextArea...
Please someone help me as soon as possible!!!
Thanx
JNP
-
December 6th, 2001, 07:46 AM
#2
Re: I want to implement a print button that prints a textarea...
If you read the JavaDocs for PrinterJob (http://java.sun.com/j2se/1.3/docs/ap...rinterJob.html), you'll find that setPrintable(...) takes a Printable object, not a String. It does help to read the docs occasionally...
A simple way to print the contents of any Component (e.g. a JTextArea) might look like this: PrintJob pjob = getToolkit().getPrintJob(this,
"Print TextArea", null);
if (pjob != null) {
Graphics pg = pjob.getGraphics();
if (pg != null) {
myTextArea.printAll(pg); // Print the Component contents
pg.dispose(); // flush page
}
pjob.end();
}
The Java Tutorial has an example of printing component contents by implementing Printable: http://java.sun.com/docs/books/tutor...component.html,
You might find these articles useful:
http://java.sun.com/docs/books/tutor.../overview.html
http://manning.spindoczine.com/sbe/f.../Chapter22.htm
Dave
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 6th, 2001, 08:06 AM
#3
Re: I want to implement a print button that prints a textarea...
Incidentally, the easiest way I know to do what you want is to download the PrintUtilities class from http://www.apl.jhu.edu/~hall/java/Sw...-Printing.html. Then you can just do:PrintUtilities.printComponent(textField);
Which saves a lot of hassle ;-)
Dave
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 6th, 2001, 10:48 AM
#4
Re: I want to implement a print button that prints a textarea...
A comment about your code:
The if test: command == "Print Code..."
is NOT a good way to see it a String variable has a certain value. The == operator tests for equality of reference variables or primitives, not equality of the contents of the objects referenced. Two reference variables(pointers) have the same value if they both refer to the same object.
You should use the class's equals() method (or what ever the class's programmer has provided) to test for equality.
Norm
Norm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|