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

ToggleButton.ToggleButtonModel fires incorrect events

XMLWordPrintable

    • beta2
    • generic, x86
    • generic, windows_nt



      Name: krT82822 Date: 09/30/99


      When the following code is called

        JCheckBox box = new JCheckBox();
        box.setSelected(false);
        box.addItemListener(new ItemListener() {
          public void itemStateChanged(ItemEvent e) {
            System.err.println("stateChanged!");
          }
        }
        box.setSelected(false); // this yields an event!

      the second 'setSelected(false)' call causes an event to be
      fired, although the state of the model factually didn't change.

      The solution for the problem is pretty obvious (someone commented
      out some source code and did not consider all the consequences):
      In the code for the ToggleButtonModel (starting with line 222
      in the JDK 1.2.2 sources) you get the following source code --
      just look at the first three lines of the method and you will see
      the problem: the check for real changes has erroneously been
      commented out:

              /**
               * Sets the selected state of the button.
               * @param b true selects the toggle button,
               * false deselects the toggle button.
               */
              public void setSelected(boolean b) {
                  // if (this.isSelected() == b) {
                  // return;
                  // }
                      
                  if(group != null) {
                      // use the group model instead
                      group.setSelected(this, b);
                  } else {
                      if (b) {
                          stateMask |= SELECTED;
                      } else {
                          stateMask &= ~SELECTED;
                      }
                  }

                  // Send ChangeEvent
                  fireStateChanged();

                  // Send ItemEvent
                  fireItemStateChanged(
                          new ItemEvent(this,
                                        ItemEvent.ITEM_STATE_CHANGED,
                                        this,
                                        this.isSelected() ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
              
              }

      ---------------

      9/30/99 eval1127@eng -- still true in kestrel-beta. Couldn't find existing dupe bug.
      (Review ID: 95953)
      ======================================================================

            peterz Peter Zhelezniakov
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: