Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6232607
Votes 13
Synopsis Clipping off JLabel-text in printer output using html
Category java:classes_swing
Reported Against tiger-beta2
Release Fixed mustang(b28), 5.0u4(b02) (Bug ID:2123894)
State 10-Fix Delivered, bug
Priority: 3-Medium
Related Bugs 4352983 , 5067892
Submit Date 24-FEB-2005
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 :
 customer  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.
  xxxxx@xxxxx   2005-2-24 12: customer :19 GMT
Work Around
N/A
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.

  xxxxx@xxxxx   2005-2-25 20:00:55 GMT
Comments
  
  Include a link with my name & email   

Submitted On 09-MAR-2006
BIG_O
We experience the same cutting off of characters 
in plain JTextArea with font size 10.... font size 7 works...any thoughts?



PLEASE NOTE: JDK6 is formerly known as Project Mustang