• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing on JTextArea

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello;
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.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
printgui.go();
}
public void go(){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
text= new JTextArea(10,20);
text.setLineWrap(true);
text.append("poo");
JScrollPane scroller = new JScrollPane(text);
panel.add(scroller);
JButton printbutton = new JButton("Print me");
printbutton.addActionListener(new PrintButtonListener());
panel.add(printbutton);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(250,250);
frame.setVisible(true);
System.out.println(text.getText());
}
public class PrintButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
//MyString printing = new MyString(text.getText());
printPage(text.getText());
}
public void printPage(String textToPrint){
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat landscape = job.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
Book bk = new Book();
bk.append(new PaintCover(), job.defaultPage());
//bk.append(new PaintContent(), landscape);
job.setPageable(bk);
//job.setPrintable(new PaintCover(), job.defaultPage());
if (job.printDialog()) {
try {
job.print();
}catch (Exception exc){
System.out.println(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.setFont(fnt);
g.setColor(Color.black);
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
javatm.4t.com
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'm still in control here. LOOK at this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic