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

(Motif toolkit) TextField widget busywaits for input while container has focus

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • 1.0
    • client-libs
    • None
    • sparc
    • solaris_2.4

      If I have the mouse focused on a container (in this case, a Frame) which contains
      a TextField, the TextField seems to busywait for X input events, consuming 50%
      of CPU time in the process.

      An interesting symptom of this is that the cursor only blinks if the TextField's
      container notices an input event.

      A rather odd effect of this is that I can make my applications consume less CPU
      time by continuously wiggling the mouse.
      ________
      I'm adding this user's description of this problem because it includes a good code example and a very accurate description.
       
      Category: AWT

      Priority: CRITICAL! (Low/Medium/High/Critical)

      JDK version: JDK-1_0-solaris2-sparc.tar.Z

      Platforms: Solaris2.4, SPARCStation20


      Synopsis
      --------

      Motif textfield blocks repainting while it has the focus


      Description
      -----------

      This bug occurs on Motif.

      Click inside a textfield. Notice that the text cursor should be
      blinking, but is not. As you move the mouse, the text cursor will
      start to blink erratically.

      Repaints appear to be blocked while a textfield has the focus. When
      the mouse is moved, the queued up repaints will then be unblocked.
      When the mouse stops moving again, repainting once again becomes
      blocked.


      Test Case
      ---------

      - Compile and run textfield.java:

      javac textfield.java
      java textfield

      - The button has the focus. Notice the label updating every second.

      - Click on the textfield. Notice that the label does not update anymore,
        nor is the textfield cursor blinking.

      - Move the mouse about. The label starts to update again, and the textfield
        cursor begins to blink.

      - Stop moving the mouse. The label doesn't update, and the textfield cursor
        doesn't blink.

      - Click on the button. The textfield no longer has focus, so the label
        continues to update.


      Workaround
      ----------

      Calling setText on a label will cause pending repaints to be
      unblocked. The call to setText must contain a new value for the
      label, otherwise the label won't update.
      ----------
      X-Sun-Data-Type: default
      X-Sun-Data-Description: default
      X-Sun-Data-Name: textfield.java
      X-Sun-Charset: us-ascii
      X-Sun-Content-Lines: 66


      import java.awt.*;

      class CanvasLabel extends Canvas {
        private String text;

        public CanvasLabel (String text) {
          this.text = text;
        }

        public void setText(String text) {
          this.text = text;
          repaint();
        }

        public Dimension minimumSize() {
          int w, h;
          FontMetrics fm = getFontMetrics(getFont());
          w = fm.stringWidth(text);
          h = fm.getHeight();
          return new Dimension(w, h);
        }

        public Dimension preferredSize() {
          return minimumSize();
        }

        public void paint(Graphics g) {
          int x, y;

          Dimension d = size();
          FontMetrics fm = getFontMetrics(getFont());

          x = (d.width - fm.stringWidth(text))/2;
          y = d.height - 3;

          g.setFont(getFont());
          g.setColor(getForeground());
          g.drawString(text, x, y);
        }
      }

      public class textfield extends Frame {
        public static void main(String argv[]) {
          new textfield();
        }

        public textfield() {
          Button button = new Button("Button");
          TextField textfield = new TextField("TextField");
          CanvasLabel label = new CanvasLabel("0");

          add("North", button);
          add("Center", textfield);
          add("South", label);

          show();
          resize(preferredSize());
          validate();

          for (int i=0; ; i++) {
            label.setText(Integer.toString(i));
            try { Thread.sleep(1000); } catch (Exception ex) {}
          }
        }
      }

            amfowler Anne Fowler (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: