-4

I am trying to print a JTextArea through a printer, but text from JTextArea is not visible in print file i.e. .xps file.

bprint.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
      try {
         int num;
         JFrame f1 = new JFrame("PrintJob");
         Toolkit tkp = p1.getToolkit();
         Properties prop = new Properties();
         PrintJob pjp = tkp.getPrintJo(f1, "Printjob", prop);
         Graphics g = pjp.getGraphics();
         p1.print(g);
         g.dispose();
         pjp.end();
      } catch (Exception e109) {
         System.out.println(e109);
      }
   }
});
Joel
  • 4,732
  • 9
  • 39
  • 54
  • 2
    Please put in the effort ask an actual answerable question and to provide pertinent details so that we have half a chance to understand what may be wrong. If you won't put in the effort to clarify your problem, why should we put in the effort to try to help you? – Hovercraft Full Of Eels Feb 23 '14 at 05:16

2 Answers2

2

That's not really how printing is done, you should be passing the PrintJob a Printable, which will then tell you when it wants something printed

Take a look at Printing for more details

For example, example and example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Note, you're trying to print from a JFrame created de novo within your ActionListener, one that has no JTextArea, and certainly no text component that has any code input from any user. I presume that there's a real visualized GUI out there that you are intent on printing, but if so, your code above doesn't approach a solution because it is creating its own GUI, one that is never displayed and with a JTextArea that never gets text, and then tries to print it. The solution is to try to get a reference to the actual visualized JTextArea and then to extract its text.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373