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

JTextField text selection doesn't stop if ended during loss of window focus

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Reproduced on:
      OpenJDK 22-ea+22-1754 (Windows 11)
      OpenJDK 21.0.1+12-29 (Windows 11)
      Amazon Corretto 8.362.08.1 (Windows 11)

      Observed, but not yet reproduced, on OpenJDK 17.0.8.1 (MacOS 13)

      A DESCRIPTION OF THE PROBLEM :
      It is possible for a JTextField to enter a state where it believes the user is still actively selecting text with the mouse, despite it no longer being the case. In this situation, the text selection process appears to behave as if the user stopped moving the mouse and never released the mouse button.

      If this happens while the last known mouse position was to the left of the input field, it creates a situation where newly entered characters are immediately selected and thus overwritten by subsequent characters, which themselves immediately become selected, and so on.

      If this happens while the last known mouse position was to the right of the input field, text entry works properly at the end of the textbox, but any attempt to step backwards through the text with the arrow keys will result in the caret resetting to the end of the text and the last few characters being selected.

      In either situation, once the bug is triggered, mouse movements will not influence the result. However, clicking in the JTextField will resolve the issue.

      The steps to reproduce below is the way we've managed to forcibly reproduce the issue. We have randomly encountered this issue a few times in the past, perhaps under less contrived circumstances, but haven't found any other way to reproduce it so far.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      These steps are for Windows. Similar approaches may work for other operating systems.

      1. Run the sample program below, which is a simple JFrame containing a JTextField.
      2. Have some other application open behind the Java window, such that switching to it will completely obscure the Java window.
      3. In the Java window's JTextField, start making a selection from the end to the start, with the mouse cursor ending up outside the left edge of the window and with the text fully selected.
      4. Without releasing the mouse button, Alt+Tab to the other window to obscure the Java window.
      5. Release the mouse button and Alt+Tab back.
      6. Without clicking in the text box - which retains input focus - start typing.

      (Alternative: Instead of Alt+Tab, press the Windows key to open the Start menu, release the mouse button, press Escape to close the Start menu, and start typing. Any situation where the window neither has focus nor the cursor directly on top of it as the mouse key is released appear to trigger this bug.)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Entering text should replace the portion of the text that was selected. This should also remove the selection, resulting in subsequent typing being inserted after the previously entered text.
      ACTUAL -
      Entering text replaces the portion of the text that was selected and then causes that text itself to become selected. Subsequent typing continues to replace, select, and replace the text being entered until the user clicks somewhere in the JTextField to finish the selection process.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;

      public class Example {
          public static void main(String[] args) {
              JFrame f = new JFrame("Example");

              JTextField t = new JTextField("This is an example");
              t.setBounds(10, 10, 200, 30);

              f.add(t);
              f.setSize(300, 100);
              f.setLayout(null);
              f.setVisible(true);
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        1. Example.java
          0.4 kB
          Praveen Narayanaswamy

            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: