JDK-6232607 : Clipping off JLabel-text in printer output using html
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux_redhat_9.0,windows_xp
  • CPU: x86
  • Submitted: 2005-02-24
  • Updated: 2011-02-16
  • Resolved: 2005-03-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u4Fixed 6 b28Fixed
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

EXTRA RELEVANT SYSTEM CONFIGURATION :
The bug occurs on different printers by different vendors.

A DESCRIPTION OF THE PROBLEM :
Filling html code in a JLabel (to get a multiline label or styled text like in JTextPane) the printer output is clipped off while using a special font-size. In the code example below the text is clipped off in font-size 6, 8, 15, 16, 17, while in font-size 7 or 9 there is to much space. It might differ between diffent font-styles.
On screen everything looks nice.

This behavior reminds me of bug 4352983.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code example below.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

import javax.swing.*;
import javax.swing.border.*;

public class PrintingLabel extends JFrame {

	public PrintingLabel() {

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		String head = "<html><p style=\"font-family: Arial; font-size: ";
		String dylanThomas = "pt\">When all my five and country senses see</p></html>";

		JButton printButton = new JButton("Print");

		PrintablePanel labelPanel = new PrintablePanel();
		GridBagLayout gridBagLayout = new GridBagLayout();
		labelPanel.setLayout(gridBagLayout);
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.NONE;
		constraints.anchor = GridBagConstraints.WEST;
		constraints.gridx = 0;
		
		JLabel label;
		String text;
		for (int i = 6; i < 31; i++) {
			text = head + i + dylanThomas;
			label = new JLabel(text);
			label.setBorder(new LineBorder(Color.BLACK, 1));
			constraints.gridy = i-1;
			gridBagLayout.setConstraints(label, constraints);
			labelPanel.add(label);
		}

		JPanel panel = (JPanel)getContentPane();
		panel.setLayout(new BorderLayout());
		panel.add(labelPanel, BorderLayout.CENTER);
		panel.add(printButton, BorderLayout.SOUTH);
		
		final PrinterJob prnJob = PrinterJob.getPrinterJob();
		prnJob.setPrintable(labelPanel);

		printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if (prnJob.printDialog()) {
					try {
						prnJob.print();
					} catch (PrinterException e) {
						e.printStackTrace();
						System.err.println("Printing error " + e.toString());
					}
				}
			}
		});

	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception exception) {
			System.out.println("Unsupported L&F");
		}
		
		JFrame frame = new PrintingLabel();
		frame.setLocation(400, 100);
		frame.pack();
		frame.setVisible(true);
	}

	private class PrintablePanel extends JPanel implements Printable {
		public PrintablePanel() {
			super();
		}
		public int print(Graphics g, PageFormat pageFormat, int	pageIndex) {
			if (pageIndex != 0) return NO_SUCH_PAGE;
			Graphics2D g2 = (Graphics2D)g;
			g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

			setDoubleBuffered(false);
			paint(g2);
			setDoubleBuffered(true);

			return PAGE_EXISTS;
		}

	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
We cannot use JTextPane instead because we are developing an editor for buildung formulars and our customers have been constructing over hundred formulars which contains over 5000 JLabels with html text during the last three years.
###@###.### 2005-2-24 12:02:19 GMT

Comments
SUGGESTED FIX http://sa.sfbay.sun.com/projects/swing_data/mustang/6232607.1/ ###@###.### 2005-03-02 20:25:51 GMT
02-03-2005

EVALUATION html printing works fine when html resides in JTextComponent. BasicTextUI stashes FontRenderContext to paint text with. JLabel does not stash FontRenderContext as a result html text is painted for the printer device and thus we have clipping (see 4352983 Clipping of text when printing for more details) After the change for 5030990 all drawing methods have component they drawing for as an argument. FontRenderContext is to be derived from the component. AppContext stashing should be removed. That guarantees text will be painted with correct FontRenderContext always. ###@###.### 2005-2-25 20:00:55 GMT
25-02-2005