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

unix: javax.swing.JOptionPane must not fire PropertyChangeEvent

XMLWordPrintable



      Name: skR10017 Date: 06/01/2000



      Java Beans 1.01 Specification reads about PropertyChangeSupport class:

      public void firePropertyChange(String propertyName, Object oldValue, Object newValue)
      Report a bound property update to any registered listeners.
      No event is fired if old and new are equal and non-null.

      Parameters:
      propertyName The programmatic name of the property that was changed.
      oldValue The old value of the property.
      newValue The new value of the property.

      Class javax.swing.JOptionPane contains a bug.

      If the following code is executed:

      --------------------------test.java-----------------
      import java.beans.*;
      import javax.swing.*;

      public class test implements PropertyChangeListener {
          boolean fired=false;
          public void propertyChange(PropertyChangeEvent evt)
          {
              fired=true;
          }
          public test()
          {
              try
              {
                  JOptionPane jp=new JOptionPane();
                  jp.addPropertyChangeListener(this);
                  jp.setOptions(jp.getOptions());
                  Thread.sleep(1000);
                  System.out.println("Fired="+fired);
              } catch(Exception e) { }
              System.exit(0);
          }
          public static void main(String [] args)
          {
              test t=new test();
          }
      }
      -----------------------------------------------------

      PropertyChangeEvent will be fired but due to Beans 1.01 Specification it must not if
      old and new property values are equal.
      It takes place because getOptions returns not the real Options array but it's copy.

      Here is citation from the source file:

      ------------------ src/javax/swing/JOptionPane.java --------------------
          public void setOptions(Object[] newOptions) {
              Object[] oldOptions = options;

              options = newOptions;
              firePropertyChange(OPTIONS_PROPERTY, oldOptions, options);
          }

          public Object[] getOptions() {
              if(options != null) {
                  int optionCount = options.length;
                  Object[] retOptions = new Object[optionCount];

                  System.arraycopy(options, 0, retOptions, 0, optionCount);
                  return retOptions;
              }
              return options;
          }
      --------------------------------------------------------------------------------

      BeanFire test from Swing testsuite fails due to this reason.

      This bug affects JDK1.3 for Linux(beta07) and Solaris(build 1.3.0rc3-Z)

      Suggestion fix:
      Not to copy options array in getOptions method and return the original one
      or compare old and new values in getOptions not as references but as
      arrays and fire event only if the differs.

      ======================================================================

            svioletsunw Scott Violet (Inactive)
            kotlsunw Kotl Kotl (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: