JDK-7092205 : No spacing in Printing JTextArea with some font and size
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2011-09-19
  • Updated: 2013-05-17
  • Resolved: 2013-05-17
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.
JDK 8
8Resolved
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) 64-Bit Server VM (build 21.0-b17, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
If JTextArea with font "SansSerif" and size of 11 is printed, all spaces appears to be trimmed for text with all capital letters only.
For some other fonts, for example, the default font of Chinese character, pMingLiu, space becomes visually un-determinable amid capital letters with slanting edges, eg. "A", "V", "W"...etc
The bug seems to be related to Bug ID: 6488219 solving uneven spacing as last workable version is 1.6.0 update 7.

REGRESSION.  Last worked in version 6u26


REPRODUCIBILITY :
This bug can be reproduced always.

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

public class JTextAreaPrinter implements Printable, ActionListener {
	private static JTextArea textArea;
	private static JComboBox<String> fontNameComboBox;
	private static JComboBox<Integer> fontSizeComboBox;
	
	static class FontChangeListener implements ItemListener {
    	public void itemStateChanged(ItemEvent e) {
    		textArea.setFont(new Font((String)fontNameComboBox.getSelectedItem(), Font.PLAIN, ((Integer)fontSizeComboBox.getSelectedItem()).intValue()));
    	};
    }
	
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
        if (page > 0) {
            return NO_SUCH_PAGE;
        }
        
        Graphics2D g2d = (Graphics2D)g;
        FontMetrics fm = textArea.getFontMetrics(textArea.getFont());
        int lineSpacing = fm.getHeight();
        String text = textArea.getText();
        
        // Print normal text
        g2d.translate(100, 100);
        textArea.print(g2d);
        
        // Print small letters
        g2d.translate(0, lineSpacing);
        textArea.setText(text.toLowerCase());
        textArea.print(g2d);
        
        // Print capital letters
        g2d.translate(0, lineSpacing);
        textArea.setText(text.toUpperCase());
        textArea.print(g2d);
        
        // Set the text back
        textArea.setText(text);
        
        return PAGE_EXISTS;
    }

    public void actionPerformed(ActionEvent e) {
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         boolean ok = job.printDialog();
         if (ok) {
             try {
                  job.print();
             } catch (PrinterException ex) {
              	ex.printStackTrace();
             }
         }
    }
    
    public static void main(String args[]) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JFrame f = new JFrame("JTextArea Printer");
        f.addWindowListener(new WindowAdapter() {
        	public void windowClosing(WindowEvent e) {
        		System.exit(0);
        	}
        });
        
        // Default Text
        textArea = new JTextArea("This is the text");
        textArea.setPreferredSize(new Dimension(200, 30));
        
        // Prepare font name and size
        fontNameComboBox = new JComboBox<String>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
        Integer[] fontSize = new Integer[50];
        for (int i = 0; i < fontSize.length; i++)
        	fontSize[i] = new Integer(i + 1);
        fontSizeComboBox = new JComboBox<Integer>(fontSize);
        
        // Add change listener
        fontNameComboBox.addItemListener(new FontChangeListener());
        fontSizeComboBox.addItemListener(new FontChangeListener());
        
        // Problemetic font and size
        fontNameComboBox.setSelectedItem("SansSerif");
        fontSizeComboBox.setSelectedItem(11);
        
        JButton printButton = new JButton("Print the JTextArea");
        printButton.addActionListener(new JTextAreaPrinter());
        f.setLayout(new BorderLayout(5, 5));
        f.add(textArea, BorderLayout.WEST);
        f.add(fontNameComboBox, BorderLayout.CENTER);
        f.add(fontSizeComboBox, BorderLayout.EAST);
        f.add(printButton, BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);
    }
}
---------- END SOURCE ----------

Comments
EVALUATION JTextField is a swing component. Re-assigning for initial evaluation.
21-09-2011