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

Text selection does not clear after focus is lost

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Version:
      9
    • Resolved In Build:
      b31
    • CPU:
      x86
    • OS:
      other

      Description

      FULL PRODUCT VERSION :
      java version "9"
      Java(TM) SE Runtime Environment (build 9+181)
      Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.15063] (1703 Compilation 15063.608)



      A DESCRIPTION OF THE PROBLEM :
      The text selection does not clear after focus is lost. This means that if the user selects text in one component, let's say a JTextField, and then goes to another component, the selection is not cleared. If the user selects text in this other component, both selections will be visible. This happens even when the CTRL key is not pressed and seems to affect all components that accept text selection (at least JTextArea and JTextField, but probably all components under JTextComponent).

      CTRL+C (copy) only copies the last selection, so this bug is merely visual.

      The exact same program will work as expected in Java 8, but not in Java 9.

      REGRESSION. Last worked in version 8u144

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.8.0_144"
      Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1 - Create a JFrame with two or more JTextFields.
      2 - Put some text in the text fields.
      3 - Select the text in one of the text fields.
      4 - Move to the other one and select the text in it.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The text selection on the first text field should be cleared as soon as the user moves to the other text field.
      ACTUAL -
      If running the code with Java 8, the text selection on the first text field is cleared as soon as the user moves to the other text field (as it is expected). On Java 9, this does not happen, giving the (wrong) idea of a multi-selection.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No error is produced.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class SelectionTestJFrame extends javax.swing.JFrame {

          public SelectionTestJFrame() {
              this.initComponents();
          }

          private void initComponents() {

              jLabel1 = new javax.swing.JLabel();
              jTextField1 = new javax.swing.JTextField();
              jTextField2 = new javax.swing.JTextField();

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

              jLabel1.setText("Select the text on one of the text fields and then select the other text field:");
              jTextField1.setText("The selection won't clear after losing focus");
              jTextField2.setText("The selection won't clear after losing focus");

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                          .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                          .addComponent(jTextField1)
                          .addComponent(jTextField2))
                      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              layout.setVerticalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addComponent(jLabel1)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                      .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              pack();
          }

          public static void main(String args[]) {
              java.awt.EventQueue.invokeLater(() -> {new SelectionTestJFrame().setVisible(true);});
          }
                      
          private javax.swing.JLabel jLabel1;
          private javax.swing.JTextField jTextField1;
          private javax.swing.JTextField jTextField2;
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Workaround is to force the clearance of the selection after FocusLost by adding a FocusListener to every single text component present in the JFrame (all JTextComponent components: JEditorPane, JTextArea, JTextField, JTextPane, DefaultTreeCellEditor.DefaultTextField, JFormattedTextField, JPasswordField). This is the code needed:

          /**
           * Clears the text selection (to fix Java 9 bug).
           * @param evt - Event.
           */
          private void forceClearSelection(java.awt.event.FocusEvent evt) {
              if (evt.getComponent() instanceof JTextComponent){
                  ((JTextComponent)evt.getComponent()).select(0, 0);
              }
          }


              // Then, for each component, do this:
              jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
                  public void focusLost(java.awt.event.FocusEvent evt) {
                      forceClearSelection(evt);
                  }
              });

              jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
                  public void focusLost(java.awt.event.FocusEvent evt) {
                      forceClearSelection(evt);
                  }
              });

              // And so on for all components.


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              ssadetsky Semyon Sadetsky (Inactive)
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: