Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7092205

No spacing in Printing JTextArea with some font and size

XMLWordPrintable

        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 ----------

              prr Philip Race
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: