Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6278700
Votes 0
Synopsis JSlider created with BoundedRangeModel fires twice when changed
Category java:classes_swing
Reported Against
Release Fixed 7(b38)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs 6789082
Submit Date 01-JUN-2005
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.
  xxxxx@xxxxx   2005-06-01 10:51:03 GMT
Work Around
N/A
Evaluation
Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=16704
Posted Date : 2006-11-07 22:42:30.0

The *public JSlider(BoundedRangeModel brm)* constructor shouldn't add change listener directly because it does the setModel method
Posted Date : 2008-09-02 09:40:30.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang