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