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

Incorrect spacing when printing JTextField

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 7u15
    • client-libs
    • None
    • 2d
    • windows

      FULL PRODUCT VERSION :
      java version " 1.7.0_15 "
      Java(TM) SE Runtime Environment (build 1.7.0_15-b03)
      Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      Space between capital words appeared to be trimmed if printing java.swing.JTextField or java.swing.JTextArea.
      If the text is appended with plenty of spaces, the space between the capital words is widened. The printed text is appeared to be justified.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Printing java.swing.JTextField or java.swing.JTextArea, text with font " SansSerif " or " Arial " for example and font size 11.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Spaces between words are clear and human easily readable.
      ACTUAL -
      No spacing between capital words.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package demo;

      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.Font;
      import java.awt.FontMetrics;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.GraphicsEnvironment;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.ItemEvent;
      import java.awt.event.ItemListener;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;
      import java.awt.print.PageFormat;
      import java.awt.print.Printable;
      import java.awt.print.PrinterException;
      import java.awt.print.PrinterJob;

      import javax.swing.JButton;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JTextField;
      import javax.swing.UIManager;

      public class JTextComponentPrinter implements Printable, ActionListener {
      // It can be replaced into JTextArea
      private static JTextField textfield;
      private static JComboBox<String> fontNameComboBox;
      private static JComboBox<Integer> fontSizeComboBox;

      static class FontChangeListener implements ItemListener {
          public void itemStateChanged(ItemEvent e) {
          textfield.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 = textfield.getFontMetrics(textfield.getFont());
              // A more than enough line spacing to prevent ANOTHER JAVA BUG...
              int lineSpacing = fm.getMaxAscent() + fm.getHeight() + fm.getMaxDescent();
              String text = textfield.getText();
              
              // Print normal text
              g2d.translate(100, 100);
              textfield.print(g2d);
              
              // Print small letters
              g2d.translate(0, lineSpacing);
              textfield.setText(text.toLowerCase());
              textfield.print(g2d);
              
              // Print capital letters
              g2d.translate(0, lineSpacing);
              textfield.setText(text.toUpperCase());
              textfield.print(g2d);
              
              // Test printing with spaces appending
              String textWithSpaceAppending = text + " " ;
              g2d.translate(0, lineSpacing);
              textfield.setText(textWithSpaceAppending.toLowerCase());
              textfield.print(g2d);
              
              // Print small letters
              g2d.translate(0, lineSpacing);
              textfield.setText(textWithSpaceAppending.toLowerCase());
              textfield.print(g2d);
              
              // Print capital letters
              g2d.translate(0, lineSpacing);
              textfield.setText(textWithSpaceAppending.toUpperCase());
              textfield.print(g2d);
              
              // Set the original text back
              textfield.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
              textfield = new JTextField( " ABC Company Limited " );
              textfield.setPreferredSize(new Dimension(200, 30));
              textfield.setBorder(null);
              
              // 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());
              
              // Problematic font and size
              fontNameComboBox.setSelectedItem( " SansSerif " );
              fontSizeComboBox.setSelectedItem(11);
              
              JButton printButton = new JButton( " Print the JTextComponent " );
              printButton.addActionListener(new JTextComponentPrinter());
              
              f.setLayout(new BorderLayout(5, 5));
              f.add(textfield, BorderLayout.WEST);
              f.add(fontNameComboBox, BorderLayout.CENTER);
              f.add(fontSizeComboBox, BorderLayout.EAST);
              f.add(printButton, BorderLayout.SOUTH);
              f.pack();
              f.setVisible(true);
          }
      }

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

      SUPPORT :
      YES

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

              Created:
              Updated:
              Resolved: