George Profenza George Profenza - 1 month ago 12
Java Question

How to update a JSlider's maximum value without dispatching a change event

I have wee bit of code that updates a

; and at another point in time, the
's maximum value needs to updated. The problem is when I call
on the slider, it also dispatches a
. To avoid that I'm doing this:


Is there a cleaner/more elegant way of doing this?


A clean way might be (depending a bit on what you actually need) to implement a custom BoundedRangeModel which fires a custom ChangeEvent that can carry the actually changed properties:

 * Extended model that passes the list of actually changed properties
 * in an extended changeEvent.
public static class MyBoundedRangeModel extends DefaultBoundedRangeModel {

    public MyBoundedRangeModel() {

    public MyBoundedRangeModel(int value, int extent, int min, int max) {
        super(value, extent, min, max);

    public void setRangeProperties(int newValue, int newExtent, int newMin,
            int newMax, boolean adjusting) {
        int oldMax = getMaximum();
        int oldMin = getMinimum();
        int oldValue = getValue();
        int oldExtent = getExtent();
        boolean oldAdjusting = getValueIsAdjusting();
        // todo: enforce constraints of new values for all
        List<String> changedProperties = new ArrayList<>();
        if (oldMax != newMax) {
        if (oldValue != newValue) {
        // todo: check and add other properties 
        changeEvent = changedProperties.size() > 0 ? 
                new MyChangeEvent(this, changedProperties) : null;
        super.setRangeProperties(newValue, newExtent, newMin, newMax, adjusting);


 * Extended ChangeEvent that provides a list of actually 
 * changed properties. 
public static class MyChangeEvent extends ChangeEvent {

    private List<String> changedProperties;

     * @param source
    public MyChangeEvent(Object source, List<String> changedProperties) {
        this.changedProperties = changedProperties;

    public List<String> getChangedProperties() {
        return changedProperties;


Its usage something like:

final JSlider slider = new JSlider();
slider.setModel(new MyBoundedRangeModel(0, 0, -100, 100));
ChangeListener l = new ChangeListener() {

    public void stateChanged(ChangeEvent e) {
        if (e instanceof MyChangeEvent) {
            MyChangeEvent me = (MyChangeEvent) e;
            if (me.getChangedProperties().contains("value")) {
               System.out.println("new value: " + 
                    ((BoundedRangeModel) e.getSource()).getValue()); 
            if (me.getChangedProperties().contains("maximum")) {
                System.out.println("new max: " + 
                    ((BoundedRangeModel) e.getSource()).getMaximum()); 
        } else {
            // do something else or nothing

Note that you have to register the listener with the model, not with the slider (reason being that the slider creates a new changeEvent of the plain type)