Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So I found a way that works perfectly for my situation and I thought I would just post what it was in case it would be useful to anyone. </p> <p>The basics of the solution are that Java does have its own full fledged (At least compared to mine) printDialog popUp that has more than I needed (Page layout editing, previews, etc) and all you have to do to use it is hand it an object that implements Printable and it is within that object that you create a graphic and draw your document. </p> <p>I just needed to draw my output String and that was easily done, I even found a StringReader so I can stop naively Writing a File just to get my output in a BufferedReader.</p> <p>Here's the code. There are two parts, the method and the class where I draw the image:</p> <p>Method: </p> <pre><code>private void printToPrinter() { String printData = CalculationTextArea.getText() + "\n" + SpecificTextArea.getText(); PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(new OutputPrinter(printData)); boolean doPrint = job.printDialog(); if (doPrint) { try { job.print(); } catch (PrinterException e) { // Print job did not complete. } } } </code></pre> <p>And here is the Class where the document is printed:</p> <pre><code>public class OutputPrinter implements Printable { private String printData; public OutputPrinter(String printDataIn) { this.printData = printDataIn; } @Override public int print(Graphics g, PageFormat pf, int page) throws PrinterException { // Should only have one page, and page # is zero-based. if (page &gt; 0) { return NO_SUCH_PAGE; } // Adding the "Imageable" to the x and y puts the margins on the page. // To make it safe for printing. Graphics2D g2d = (Graphics2D)g; int x = (int) pf.getImageableX(); int y = (int) pf.getImageableY(); g2d.translate(x, y); // Calculate the line height Font font = new Font("Serif", Font.PLAIN, 10); FontMetrics metrics = g.getFontMetrics(font); int lineHeight = metrics.getHeight(); BufferedReader br = new BufferedReader(new StringReader(printData)); // Draw the page: try { String line; // Just a safety net in case no margin was added. x += 50; y += 50; while ((line = br.readLine()) != null) { y += lineHeight; g2d.drawString(line, x, y); } } catch (IOException e) { // } return PAGE_EXISTS; } } </code></pre> <p>Anyways that is how I solved this problem! Hope it can be of some use to someone!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload