Michal Vician
Michal Vician

Reputation: 2545

Java Print API - Space character incorrectly printed using monospaced 'Courier New' font

Let me first describe the picture below:

enter image description here

I want all characters highlighted by the green line to be printed in one column.

Font of a String is monospaced Courier New. However, it seems that space characters are not printed as monospaced (see 'dotted' lines vs. lines with space characters at the beginning).

To print the string I use standard Java Print Service API over JTextPane component:

PrinterJob pj = PrinterJob.getPrinterJob(); 
pj.setPrintable(myTextPane);
pj.print();

To my knowledge the Java Print Service API in fact calls paint() methods of myTextPane. Therefore the preview should look exactly the same as the printed version of String.

However, it doesn't. The preview does not seem to misinterpret monospaced space characters (see the last picture). The preview looks exactly as I want the text to be printed.

enter image description here

Any suggestions how to force JavaPrintServiceAPI to print monospaced space characters correctly?

Upvotes: 6

Views: 1576

Answers (1)

trashgod
trashgod

Reputation: 205795

I don't think you can fix this in the printing API.

First divide each line after the "green" digit into a left and (possibly empty) right substring.

In your JTextArea, use align to justify a two-column HTML table, as described in How to Use HTML in Swing Components.

As an alternative to JTextPane, use a GridLayout of JLabel. Give the left column RIGHT_ALIGNMENT and the right column LEFT_ALIGNMENT.

A two-column JTable, which uses JLabel for rendering, might be a third alternative. See How to Use Tables—Concepts: Editors and Renderers for details.

Upvotes: 1

Related Questions