JDK-8064541 : java.awt.Checkbox.setState() call generates ItemEvent
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 8u25,9
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: other
  • CPU: x86
  • Submitted: 2014-10-22
  • Updated: 2015-05-01
  • Resolved: 2015-05-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9Resolved
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
OS X Yosemite
Version 10.10 (14A389)

A DESCRIPTION OF THE PROBLEM :
As the documentation for java.awt.Checkbox.addItemListener() call states that  "Item events are sent to listeners in response to user input, but not in response to calls to setState()."  But for all recent java on OS X, including the most recent 1.8.0_25, the setState() call also sends an ItemEvent to the listener.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the attached source code on a Mac computer, run "java bug.Bug".  This Bug class listens on both "left" and "right" Checkbox, and print a message when receiving an ItemEvent.  But when left Checkbox is clicked, it will call right Checkbox setState().  But this call should not generate another ItemEvent to send to the listener, but it does.  Click on the right Checkbox will only cause one message on the console, but click on the left Checkbox will see two messages on the console, one for left and one for right.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Click on the left should only see one message for left.
ACTUAL -
Click on the left see one message for left, and another for right.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package bug;

import java.awt.*;
import java.awt.event.*;

class Bug extends Frame implements WindowListener, ItemListener {

  public static void main(String[] args) {
    new Bug();
  }

  Bug() {
    super("Bug");
    setLayout(new GridBagLayout());
    GridBagConstraints grid = new GridBagConstraints();
    grid.insets = new Insets(50, 50, 50, 50);
    add(left = new Checkbox("Left"), grid);
    add(right = new Checkbox("Right"), grid);
    addWindowListener(this);
    left.addItemListener(this);
    right.addItemListener(this);
    pack();
    setVisible(true);
  }

  public void windowClosing(WindowEvent event) {System.exit(0);}
  public void windowOpened(WindowEvent event) {}
  public void windowClosed(WindowEvent event) {}
  public void windowIconified(WindowEvent event) {}
  public void windowDeiconified(WindowEvent event) {}
  public void windowActivated(WindowEvent event) {}
  public void windowDeactivated(WindowEvent event) {}

  public void itemStateChanged(ItemEvent event) {
    Object source = event.getSource();
    if (source == left) {
      System.out.println("item event from left");
      right.setState(!right.getState());
    } else if (source == right) {
      System.out.println("item event from right");
    }
  }

  private Checkbox left, right;
}

---------- END SOURCE ----------


Comments
is it reproducible on 8 GA?
11-11-2014

Tested this with 8u25 and 8u40 (b12) on MAC. This is reproducible as stated by the submitter. Similar code behave fine in Windows and Linux. According to user the intention is "If the user change the state of left, I want to change the state of right as well, (not always set it to false.) But that setState() call to the right by program code should not generate an ItemEvent, but it does on Mac."
11-11-2014