Solved

Printing in Java Application

Posted on 1998-02-16
8
155 Views
Last Modified: 2013-11-23
A rather simple question I hope. How do I print the text within a component in Java? or print the
 contents of a file? For example, in a TextArea? I see there is a printall method for graphics but it only
 does a screen snap shot and not the contents of the component. I looked into the System.out.println but
 not sure how to reroute the standard output stream. If possible?

 Thanks

 Benny
0
Comment
Question by:bcsystem
  • 4
  • 4
8 Comments
 

Accepted Solution

by:
ammorris earned 100 total points
ID: 1233074
Using the PrintJob class a good way to print.  If you don't want your printout to look the same as your component, you can do this with your paint() method:

if (g instanceof PrintGraphics) // this is the interface given for printAll calls
{
   // do printout how you want it, use normal graphics calls like    // drawString(), etc.
}
else
{
   // your normal paint or super.paint(g)
}

Just get a PrintJob object from the Toolkit, do a printAll, and dispose of the Graphics object.  It should do the special printing you want.
0
 

Author Comment

by:bcsystem
ID: 1233075
Can you give me an example of how I would print text from within a textarea class to a print?

Thanks
0
 

Expert Comment

by:ammorris
ID: 1233076
Read the text line by line from the text area and drawString() it on the Graphics object.  Or you might be able to call textArea.paint(g).
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:bcsystem
ID: 1233077
Below is a sample bit of code I have..

void print_Clicked(Event event) {

      PrintJob pjob = getToolkit().getPrintJob(this,"Printing Test", null);

    if (pjob != null)
    {          
      Graphics pg = pjob.getGraphics();

      if (pg != null)
      {
        String tempx = codeTextBox.getText();
        pg.drawString(tempx,0,0);
       
        pg.dispose(); // flush page
      }
                 
    pjob.end();
      }
    }

The program GPFs when it hits the drawString method.  Is it Visual Cafe 1.1 bug?

Thanks
0
 

Expert Comment

by:ammorris
ID: 1233078
I don't know... that should work, it's the way my co-worker and I did it.  I have never used Visual Cafe, so I don't know.  The way I was suggesting was to put that drawString call in your paint method, calling it only if the graphics object implemented the PrintGraphics interface.

Java programs should never GPF, is that right?
0
 

Author Comment

by:bcsystem
ID: 1233079
This is what I get below..I get the windows GPF box and when I click on details..it shows

JAVA caused an invalid page fault in
module WINAWT.DLL at 0137:1000c8c4.
Registers:
EAX=00000000 CS=0137 EIP=1000c8c4 EFLGS=00010202
EBX=00e7af88 SS=013f ESP=020afd2c EBP=020afd38
ECX=ffffff4c DS=013f ESI=00e83a10 FS=39d7
EDX=00000000 ES=013f EDI=007a3370 GS=0000
Bytes at CS:EIP:
8b 08 8b 51 14 8b 1a 8b 4b 08 85 c9 74 1b 8b 4d
Stack dump:
020afd74 020afd70 00000000 020afee0 1001d30d 00e7af88 00e83a10 00000000 00000000 00e7af88 00e7ac40 21c87727 020afd70 020afee0 01f6d360 007a2920
0
 

Expert Comment

by:ammorris
ID: 1233080
Sounds like a bug.  It works with Sun's VM.

0
 

Author Comment

by:bcsystem
ID: 1233081
I still continue to have problems with printing.  I'm trying to determine if this is a Visual Cafe problem or Sun VM.  Does anyone have a simple print application that I can compile and test with JDK 1.1?
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 95
allswap challenge 6 96
Java SE 8u111  Lot of stuff broke 11 68
Convert from a json string array to a Java object 3 28
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now