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

If setEditable(true) is called on a JTextField that has focus the caret will stop updating

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8, 17, 19, 20
    • client-libs
    • None

      Run the attached test case. Note that after the test is complete the caret stops being updated. With fix for JDK-4512626 the caret stays non-blinking, without it the caret disappears. If you click a button and then click back to the textField causing the focus to change caret will start working as expected.

      -- Cut
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JTextField;
      import javax.swing.SwingUtilities;
      import java.awt.BorderLayout;
      import java.awt.Component;
      import java.awt.Robot;
      import java.lang.reflect.InvocationTargetException;

      public class CaretThing {

          static JFrame frame;
          static JTextField textField;
          static Robot robot;
          static JButton button;

          static void printBlinkRate() {
              System.err.println("Caret blink rate: " + textField.getCaret().getBlinkRate());
          }

          static void requestFocus(Component component) throws InterruptedException, InvocationTargetException {
              robot.waitForIdle();
              robot.delay(300);

              SwingUtilities.invokeAndWait(() -> {
                  System.err.println("Requesting focus on " + component);
                  printBlinkRate();
                  component.requestFocus();
                  printBlinkRate();
              });
          }

          static void changeEditable() throws InterruptedException, InvocationTargetException {
              robot.waitForIdle();
              robot.delay(300);
              SwingUtilities.invokeAndWait(()-> {
                  System.err.println("Changing editable");
                  printBlinkRate();
                  textField.setEditable(!textField.isEditable());
                  printBlinkRate();
              });
          }

          public static void main(String[] args) throws Exception {
              robot = new Robot();
              robot.setAutoWaitForIdle(true);
              robot.setAutoDelay(50);

              SwingUtilities.invokeAndWait(() -> {
                  frame = new JFrame("Hey");
                  frame.setLocationRelativeTo(null);
                  textField = new JTextField("Some long field value");

                  textField.setEditable(false);
                  frame.setLayout(new BorderLayout());
                  frame.add(textField, BorderLayout.NORTH);

                  button = new JButton("Button");
                  button.addActionListener((e)-> printBlinkRate());

                  frame.add(button, BorderLayout.CENTER);
                  frame.pack();
                  frame.setVisible(true);

                  button.requestFocus();
                  textField.getCaret().setBlinkRate(250);
              });

              requestFocus(textField);

      // requestFocus(button); // comment this to get 0 blink rate on editable field

              changeEditable();

              requestFocus(textField);

              SwingUtilities.invokeAndWait(CaretThing::printBlinkRate);
          }
      }

            tr Tejesh R
            kizune Alexander Zuev
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: