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

On Ubuntu 22.04 text cursor in TextArea stops blinking after couple of cycles

XMLWordPrintable

      On Ubuntu 22 run the attached text case. After code is complete click on the button in the window and then click on the text field and then stop moving the mouse. Cursor will blink couple of times and then will stop blinking. If mouse enters or leaves the text field cursor will start blinking again but again will stop after couple of times. Seems like some problem with the repaint() because as far as i can tell the repaint is being called after visibility is triggered.

      -- 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:
            6 Start watching this issue

              Created:
              Updated: