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

JSpinner focusGained(...) listener does not allow selectAll functionality

XMLWordPrintable

    • generic, x86, sparc
    • generic, solaris_7, windows_2000, windows_xp

      Name: jk109818 Date: 06/10/2002


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

      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      I am attempting to force the JSpinner to select its text
      when receiving a focusGained notification. I extract the
      JTextField from the spinner and call the selectAll()
      method. Printing out the selected text works correctly.
      However, in the GUI the text is not selected.

      I have debugged into the swing source, and I believe that
      after my FocusAdapter executes, the text in the JSpinner is
      validated (the text is apparently removed and re-inserted)
      which annuls the selection made in my FocusAdapter.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile and execute the included source
      2. Tab to the JSpinner
      3. Notice that the selection output in the debug statement
      indicates that text is selected.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Expected result: When tabbing to the JSpinner, the text
      should become selected.

      Actual result: When tabbing to the JSpinner, the text is
      not selected. The caret is set on the left-hand-side of
      the right-aligned text.

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------


      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JSpinner;
      import javax.swing.JTextField;
      import javax.swing.SpinnerNumberModel;

      import java.awt.event.FocusAdapter;
      import java.awt.event.FocusEvent;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;


      /**
       * A small class that can be used to demonstrate the lack of behavior of the
       * JSpinner when tabbing to it. (10/06/2002)
       * @author Stephanie Legault
       */
      public class JSpinnerTabTest
      {
          /**
           * Main method creates a UI.
           */
          public static void main(String[] args)
          {
              // Build a dummy GUI
              JFrame frame = new JFrame("Test JSpinner");
              
              // A useless button
              JButton button = new JButton("A Button");
              
              // A numeric JSpinner
              SpinnerNumberModel spinnerModel = new SpinnerNumberModel(new Integer
      (0), new Integer(0), new Integer(100), new Integer(1));
              JSpinner spinner = new JSpinner(spinnerModel);

              // Create a focus adapter
              FocusAdapter focusAdapter = new FocusAdapter()
              {
                  public void focusGained(FocusEvent e)
                  {
                      JTextField textField = (JTextField) e.getSource();
                      textField.selectAll();
                      System.out.println("Currently selected text: \"" +
      textField.getSelectedText() + "\"");
                  }
              };

              // Add the focus listener to the actual JTextComponent (it doesn't
              // work if added directly to the JSpinner)
              ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField
      ().addFocusListener(focusAdapter);
              
              // And a panel to hold the UI pieces
              JPanel panel = new JPanel();
              panel.add(button);
              panel.add(spinner);
              
              // Finish up the frame
              frame.getContentPane().add(panel);
              
              frame.addWindowListener(new WindowAdapter()
              {
                  public void windowClosing(WindowEvent e)
                  {
                      System.exit(0);
                  }
              });

              frame.setVisible(true);
              frame.pack();
          }
      }

      ---------- END SOURCE ----------
      (Review ID: 153417)
      ======================================================================

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: