
-----------------------------------
GUImaster
Thu Jul 08, 2010 3:09 pm

How Do you use a Printer (Peripheral) in Java?
-----------------------------------
ok so i have been searching on the web and asking a couple of friends 

how to use a printer

For example i have a 

JTextArea
JButton

if the button was clicked it would print out the contents of  JTextArea

I checked out the sun website but i dont understand what it says

*If this helps at all im using netbeans 6.8*

-----------------------------------
Kam
Mon Jul 12, 2010 11:49 pm

Re: How Do you use a Printer (Peripheral) in Java?
-----------------------------------
Hi. After thinking for a while, I could not think of a direct way to pull this off, but here's two ideas. Collect the string from the JTextArea (using the getText() method for JTextArea objects) and write the string into a text file. Print the text file (I don't know if there is a way to print text files in Java, however.) Or if you don't mind printing out the entire GUI of the program, along with the JTextArea, refer to PrintUtilities.java and copy and paste the contents into that class. Import this class within your main program (I'm assuming it's the class that uses a JFrame to create your program GUI; but, anything that is a Component will work). In the method that is called when the button is pressed, invoke PrintUtilities.printComponent( JFrame frame ). Below is an example.

// button event method
public void actionPerformed( ActionEvent evt ){
   PrintUtilities.printComponent( programFrame );
}
// where programFrame is the JFrame

This will open the print dialogue you commonly see before the print job starts. Hope this helps.

-----------------------------------
DemonWasp
Tue Jul 13, 2010 10:22 am

RE:How Do you use a Printer (Peripheral) in Java?
-----------------------------------
Not to be insulting, but Sun does have some very good Java tutorials and they are well-indexed by Google. I found [url=http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/misc/printtext.html]this extremely helpful tutorial in about 30 seconds of googling.

-----------------------------------
GUImaster
Fri Jul 16, 2010 6:38 pm

Re: How Do you use a Printer (Peripheral) in Java?
-----------------------------------
Thanks guys...and you've been really helpful ...I'll try it out.
