Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTextPane vs JTextArea printing alignment with monospaced font

800474Jan 8 2010 — edited Jan 28 2010
Hi all!

When printing monospaced text, I find that JTextPane does it wrongly, whereas JTextArea does it correctly.
I mean, the text in the screen is correctly vertically aligned (all characters have the same width) with both components. However, when printing, I find that, when using JTextPane, some characters (mainly blank spaces) are narrower.

I don't know if this is due to the printer or something inherent to JTextPane. I've tried with HP and Xerox printers; in both cases, the difference is noticed.

Here is a simple program to reproduce the issue:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.RepaintManager;
//import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class Spr9050PlainJava {

    public static void main(String[] args) {
	SwingUtilities.invokeLater(new Runnable() {
	    public void run() { showGui(); }
	});
    }

    public static void showGui() {
//	final JTextArea editor = new JTextArea();
	final JTextPane editor = new JTextPane();
	editor.setFont(Font.decode("monospaced-12"));
	editor.setText("// Testing monospaced font\n" +
	               "String text = \"something\";\n" +
	               "text = text.toUpperCase();\n" +
	               "System.out.println(text);");
	editor.getDocument().putProperty("i18n", Boolean.TRUE);
	JButton button = new JButton("Print");
	button.addActionListener(new ActionListener() {
	    @Override public void actionPerformed(ActionEvent event) {
		PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
		PrinterJob printerJob = PrinterJob.getPrinterJob();
		PageFormat format = printerJob.defaultPage();
		printerJob.setPageable(new ComponentPrinter(editor, format));
		if (printerJob.printDialog(attr)) {
		    try {
	                printerJob.print(attr);
                    } catch (PrinterException e) {
	                e.printStackTrace();
                    }
		}
            }
	});
	JFrame frame = new JFrame("Print test");
	JPanel panel = new JPanel(new BorderLayout());
	panel.add(new JScrollPane(editor), BorderLayout.CENTER);
	panel.add(button, BorderLayout.SOUTH);
	frame.setContentPane(panel);
	frame.pack();
	frame.setLocationRelativeTo(null);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
    }
}
With this example, I see the second dot (after "out") in the last line almost under the dot (after "text") in the penultimate line.
However, it should appear exactly under the second "t" of the second "text" in the penultimate line.

Is this something known?
Could it be solved somehow?

Thanks in advance for any help!

Note: class ComponentPrinter is put in next post
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 25 2010
Added on Jan 8 2010
4 comments
1,100 views