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

Preferred size of JFormattedTextField is too narrow

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_10-beta"
      Java(TM) SE Runtime Environment (build 1.6.0_10-beta-b21)
      Java HotSpot(TM) Client VM (build 11.0-b11, mixed mode, sharing)

      This bug is all the 6uX releaes;ie, 6u3, 6u4, 6u5, 6u6

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      The default width for a JFormattedTextField is often too narrow by a pixel or two. In both Windows and Metal look and feels. With the Windows look and feel this can actually cut off a pixel of text. In both look and feels there is a horizontal shift while selecting and editing.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run sample code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The content of the JFormattedTextField in the example is fixed length and should not shift horizontally while editing. The leading pixels of the first '0' should not be clipped initially.
      ACTUAL -
      clipped pixels, horizontal shifts while editing and selecting.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package fieldwidthbug;

      import java.awt.Container;
      import java.awt.FlowLayout;
      import java.util.logging.Level;
      import java.util.logging.Logger;
      import javax.swing.JFormattedTextField;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.SwingUtilities;
      import javax.swing.UIManager;
      import javax.swing.text.MaskFormatter;

      public class Main {

          public static void main(String[] args) {
              // TODO code application logic here
      SwingUtilities.invokeLater(new Runnable() {
      public void run()
      {
      try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      JFrame f = new JFrame("Width bug in JFormattedTextField");
      MaskFormatter mf = new MaskFormatter("##:##:##:##");
      mf.setPlaceholderCharacter('0');
      JFormattedTextField ftf = new JFormattedTextField(mf);
      ftf.setText("00:00:00:00");
      Container cp = f.getContentPane();
      cp.setLayout(new FlowLayout());
      cp.add(new JLabel(
      "<html>The JFormattedTextField is too narrow the text is<br>" +
      "clipped by default with the Windows LaF and there<br>" +
      "is a horizontal shift when editing and selecting in<br>both Metal and Windows LaF."));
      cp.add(ftf);
      f.pack();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setVisible(true);
      } catch (Exception ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
      }
      }
      });
          }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Manually tweaking the preferred size.

            peterz Peter Zhelezniakov
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: