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

JSlider created with BoundedRangeModel fires twice when changed

XMLWordPrintable

    • b38
    • x86
    • solaris_8, windows_xp
    • Not verified

      FULL PRODUCT VERSION :
      Java 1.5.0
      Java 1.4.2_05

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP 5.1.2600
      Mac OS X

      A DESCRIPTION OF THE PROBLEM :
      If you create a JSlider using the constructor that takes a BoundedRangeModel and then add a ChangeListener to the slider, then programmatically setting the value of the slider will result in the ChangeListener being called twice. I have located the problematic code.

      The code for the JSlider constructor that takes a BoundedRangeModel is:

          public JSlider(BoundedRangeModel brm)
          {
              this.orientation = JSlider.HORIZONTAL;
              setModel(brm);
              sliderModel.addChangeListener(changeListener);
              updateUI();
          }

      Note that after adding an internal ChangeListener you call setModel(). The problem is that setModel itself adds a ChangeListener. This results in a double call to the user's ChangeListener when programmatically setting the value of the JSlider.

      Fortunately there is an easy workatound in this case. Rather than calling the constructor that takes a BoundedRangeModel, call the one that takes a min, max and value. Here is the code for that constructor:

          public JSlider(int orientation, int min, int max, int value)
          {
              checkOrientation(orientation);
              this.orientation = orientation;
              sliderModel = new DefaultBoundedRangeModel(value, 0, min, max);
              sliderModel.addChangeListener(changeListener);
              updateUI();
          }

      Note that rather than calling setModel(), this code just assigns a new BoundedRangeModel to the sliderModel variable directly. You don't end up with two change listeners installed.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just create a JSlider using the constructor that takes a BoundedRangeModel. Then add a ChangeListener to the slider. Then programattically set the slider's value.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When the sliders's value is programmatically set, my ChangeListener should be called once.
      ACTUAL -
      The ChangeListener is called twice in succession.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      If you can call the constructor that takes min, max and value, the bug will not happen.
      ###@###.### 2005-06-01 10:51:03 GMT

            rupashka Pavel Porvatov (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: