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

Repains Called in Swing Timer Have Inconsistent Intervals

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8u72, 9
    • client-libs
    • 14.04 LTS Ubuntu64

      FULL PRODUCT VERSION :
      java version "1.8.0_72"
      Java(TM) SE Runtime Environment (build 1.8.0_72-b15)


      ADDITIONAL OS VERSION INFORMATION :
      Linux 3.16.0-62-generic #83~14.04.1-Ubuntu SMP Fri Feb 26 22:52:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux


      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Ubuntu's Unity Desktop

      A DESCRIPTION OF THE PROBLEM :
      When a custom JComponent is made with an overridden paintComponent() method with repaint() called by a Swing timer on Ubuntu Linux then the Timer's timing is consistent but the repaint()'s method new images displaying are inconsistent.

      In my specific example the "cursor" (a vertical line down the middle of the component) is supposed to blink every 200 milliseconds. This only occurs when the mouse is being moved over the component or keys are being typed into the component. When this is not the case, the blinking slows down to a point where is sometimes takes longer than a second for a single visible interval.

      When I ran the same code on Windows 7 the issue did nor occur and a person on StackOverflow has stated that the issue does not occur on Mac. I am unable to test this myself as I do not have access to a Mac.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the attached code on Ubuntu Linux 14.04.3 LTS x64. I have not tested it on other Linux Distributions however I suspect this is due to the somewhat unique method X uses to communicate with processes (sockets rather than method calls) so I hypothesize that the same issue would occur on other Linux distributions.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I was expecting the blinking "cursor" to blink with a fixed interval of 200 milliseconds.
      ACTUAL -
      The "cursor" blinks at the correct interval while input is being provided but the interval slows down when input is no longer provided. A video is available here: https://i.imgur.com/xdXICCt.gifv

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package src.john01dav.customtestfield;

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.ActionEvent;

      public class CustomTextField extends JComponent{
          private boolean cursorVisible = false;

          public static void main(String[] args){
              JFrame frame = new JFrame("CustomTextField");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(128, 22);
              frame.setLocationRelativeTo(null);
              frame.setLayout(new BorderLayout());
              frame.add(new CustomTextField(), BorderLayout.CENTER);
              frame.setVisible(true);
          }

          public CustomTextField(){
              Timer timer = new Timer(200, this::toggleCursor);
              timer.start();
          }

          @Override
          public void paintComponent(Graphics g){
              super.paintComponent(g);

              g.setColor(Color.WHITE);
              g.fillRect(0, 0, getWidth(), getHeight());

              if(cursorVisible){
                  int half = getWidth() / 2;

                  g.setColor(Color.BLACK);
                  g.drawLine(half, 0, half, getHeight());
              }
          }

          private void toggleCursor(ActionEvent e){
              System.out.println("Timer Called: " + System.currentTimeMillis());

              cursorVisible = !cursorVisible;
              repaint();
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I have not found a workaround.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: