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

1

6

Let me first describe the picture below:

  • There are two printed papers. The only difference between them is that few space " " characters from paper in left are replaced by dot "." character in the paper in right.
  • Red line represents the left border to which the text should be aligned
  • Green curve represents my intention to align all characters it connects into a single column. In fact the green curve is supposed to be a vertical line.

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?

Judiejudith answered 5/1, 2012 at 15:16 Comment(3)
The issue might be related to one described in #3805167Judiejudith
+1 for compelling illustrations.Cortneycorty
I seems that the bug is already listed in sun's bug database: bugs.sun.com/bugdatabase/view_bug.do?bug_id=6784397Judiejudith
C
1

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.

Cortneycorty answered 7/1, 2012 at 16:15 Comment(4)
Please don't hesitate to update this question—or pose a new one—if you need help working around the bug. In particular, consider Font. MONOSPACED in place of a Windows font.Cortneycorty
@trashgod: I used Font.MONOSPACED and I'm running Linux, not Windows. The bug is there anyway.Judiejudith
Have you found any workaround? I can't reproduce it on 1.6.0_26.Cortneycorty
I'm running on 1.6.0_27. I tried to workaround it as described by 'DReese' in the bug's [page]{bugs.sun.com/bugdatabase/view_bug.do?bug_id=6784397}. Unfortunatelly it didn't work, so I decided to write my own JComponent which is capable to preview and print any plain text. To reproduce the bug set monospace font to [Print preview JTextPane component]{java-sl.com/JEditorPanePrinter.html} and print it.Judiejudith

© 2022 - 2024 — McMap. All rights reserved.