JDK-4680670 : Adjustable.setValue() does not notify AdjustmentListener
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.1,1.3.0,1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic,solaris_7
  • CPU: generic,sparc
  • Submitted: 2002-05-07
  • Updated: 2002-05-07
  • Resolved: 2002-05-07
Related Reports
Duplicate :  
Relates :  
Description

Name: atR10191			Date: 05/07/2002


specifications for method
 public void addAdjustmentListener(AdjustmentListener l)
of interface Adjustable states:

"Add a listener to recieve adjustment events when the value of the adjustable
 object changes"

The example below shows that call of setValue() does not cause
listener to be notified.
Adjustable is obtained by mean of ScrollPane.getVAdjustable() call.
Test example hangs.
============ Test127.java ==============================================
import java.awt.*;
import java.awt.event.*;

public class Test127 {
    public static void main(String argv[]) {
        Test127 test = new Test127();
        test.testRun();
    }

    public void testRun() {
        Frame frm = new Frame();
        frm.setSize(100,100);

        ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
        frm.add(sp);
        sp.add(new Button("ppp"));
    
        MyComponentAdapter listener = new MyComponentAdapter();
        frm.addComponentListener(listener);    
 
        synchronized (listener) {
            frm.setVisible(true);
            // here we are waiting for components to be drawn
            // (else minimum and maximum can be 0)
            try {  
                listener.wait();
            } catch (InterruptedException e) {
            }
        }

        Adjustable adjustable = sp.getVAdjustable();

        int minimum = adjustable.getMinimum();
        int maximum = adjustable.getMaximum();        
        int newValue = Math.min(minimum + 1, maximum);

        if (adjustable.getValue() != newValue) {
            MyAdjustmentListener al = new MyAdjustmentListener();
            adjustable.addAdjustmentListener(al);
            adjustable.setValue(newValue);

            synchronized (al) {
                try {
                    while(!al.changed) {
                        al.wait();
                    }
                } catch (InterruptedException e) { 
                    e.printStackTrace();
                    System.out.println(" interrupted");
                    System.exit(1);
                }
            }
        }
        System.out.println("OKAY");
        System.exit(0);
    }
}

class MyAdjustmentListener implements AdjustmentListener {
    boolean changed = false;

    synchronized public void adjustmentValueChanged(AdjustmentEvent e){
        changed = true;
        notify();
    }
}

class MyComponentAdapter extends ComponentAdapter {
    synchronized public void componentShown(ComponentEvent e) {
        notify();
    }
}
======== end of Test127.java ==========================================
there is no output
==================================================
======================================================================

Comments
EVALUATION This is a hole in the documentation, not a bug in AWT behavior. "The Java Class Libraries Second Edition, Volume 2" states in the section for Scrollbar.setValue(), "Calling setValue() does not fire an AdjustmentEvent." I don't know why this isn't in the JavaDoc, but I'm filing a doc bug to have it put in. JCK tests should not be written to check this behavior. I'll update once I have the new bug ID. In the meantime, you can see similar documentation in other AWT methods such as Checkbox.setState() List.select(). I haven't investigated why the test case hangs, but the synchronized(listener) call is probably a good place to start. ###@###.### 2002-05-07 Doc bug is 4681104. ###@###.### 2002-05-07
07-05-2002