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

JSlider created with BoundedRangeModel fires twice when changed

    Details

    • Subcomponent:
    • Resolved In Build:
      b38
    • CPU:
      x86
    • OS:
      solaris_8, windows_xp
    • Verification:
      Not verified

      Description

      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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: