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

Inconsistent JFormattedTextField selection behavior at FocusGained

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0, 1.4.1, 1.4.1_02, 1.4.2
    • client-libs
    • x86, sparc
    • solaris_7, windows_2000



      Name: jk109818 Date: 04/17/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
      Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

      FULL OS VERSION :
      Windows 2000 Professional, SP2

      A DESCRIPTION OF THE PROBLEM :
      Using a JFormattedTextField, the behavior when focus is gained is not as expected and may not allow use of selectAll to restore the expected behavior.

      Per "Java Look and Feel Guidlines", 2nd Ed, pages 218 - 219, editable text fields should have their text selected when the user tabs into the field. This behavior is discussed at the referenced location and others in this book. In my case, I am trying to obtain an integer from the user, replicating default Windows behavior, which would be to select the whole field and allow only numbers to be entered. The example below uses the default LAF but it is similar with Windows LAF.

      Using selectAll to force the selection when a FocusGained event occurs works when the default constructor is used. However, as soon as a DecimalFormat is introduced, the behavior changes again. In this case, the field is not selected by default, but allows selectAll to work on the first tab through. Subsequent tab throughs fail to select the text. Different formats and formatters do not seem to help.

      This may be related to 4699955 which reports difficulties with selectAll and JSpinners.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Using the attached code, tab through the three JFormattedTextFields. The first field, created with a default constructor, is not selected on entry at all. The second field uses selectAll to force selection when focus is gained.

      Both of these fields behave the same each time through.

      The third field has a DecimalFormat. Without the selectAll, it is never selected on entry. With selectAll, it is selected the first time in but not on subsequent entries.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Each field should be selected on entry by default without using selectAll and on every entry.
      See above.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.text.DecimalFormat;
      import java.awt.event.WindowAdapter;
      import java.awt.event.FocusAdapter;
      import java.awt.event.WindowEvent;
      import java.awt.event.FocusEvent;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JFormattedTextField;

      public class TextTest extends JFrame {
          private JPanel jPanel1;
          private JFormattedTextField ftf;
          private JFormattedTextField ftf2;
          private JFormattedTextField ftf3;
          public TextTest() {
              jPanel1 = new JPanel();
              ftf = new JFormattedTextField();
              ftf2 = new JFormattedTextField();
              ftf3 = new JFormattedTextField(new DecimalFormat("##0"));
              
              addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent evt) {
                      exitForm(evt);
                  }
              });
              ftf.setText("111");
              jPanel1.add(ftf);
              ftf2.setText("222");
              ftf2.addFocusListener(new FocusAdapter() {
                  public void focusGained(FocusEvent evt) {
                      ftf2FocusGained(evt);
                  }
              });
              jPanel1.add(ftf2);
              ftf3.setText("333");
              ftf3.addFocusListener(new FocusAdapter() {
                  public void focusGained(FocusEvent evt) {
                      ftf3FocusGained(evt);
                  }
              });
              jPanel1.add(ftf3);
              getContentPane().add(jPanel1, BorderLayout.NORTH);
              pack();
          }
          private void ftf2FocusGained(FocusEvent evt) {
              ftf2.selectAll();
          }
          private void ftf3FocusGained(FocusEvent evt) {
              //seems to have no effect after first tab through
              ftf3.selectAll();
          }
          private void exitForm(WindowEvent evt) {
              System.exit(0);
          }
          public static void main(String args[]) {
              new TextTest().show();
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      I will either use a JTextField and DocumentListener or a JFormattedTextField with no DecimalFormat and a DocumentFilter to get rid of anything but numbers.

      (Small flame - why should it be so hard to just input an integer and provide feedback to the user if a non digit is typed? The behavior described in the book above would be excellent. Let the user have three errors and then display an error message. I would use a JSpinner, but other open bugs seem to make it "less than optimal".)
      (Review ID: 183942)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: