How Do you use a Printer (Peripheral) in Java?
Author |
Message |
GUImaster
|
Posted: Thu Jul 08, 2010 3:09 pm Post subject: 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* |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Kam

|
Posted: Mon Jul 12, 2010 11:49 pm Post subject: 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 http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-Printing.html as there is free source code of an printing utility that allows you to print the GUI of the program.
If, in fact you are going the second option,
create 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
|
Posted: Tue Jul 13, 2010 10:22 am Post subject: 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 this extremely helpful tutorial in about 30 seconds of googling. |
|
|
|
|
 |
GUImaster
|
Posted: Fri Jul 16, 2010 6:38 pm Post subject: Re: How Do you use a Printer (Peripheral) in Java? |
|
|
Thanks guys...and you've been really helpful ...I'll try it out. |
|
|
|
|
 |
|
|