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

Documented AbstractButton - setSelected - behaviour is misleading.

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P5
    • Resolution: Unresolved
    • Affects Version/s: 6u10
    • Fix Version/s: None
    • Component/s: client-libs

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_10"
      Java(TM) SE Runtime Environment (build 1.6.0_10-b33)
      Java HotSpot(TM) Client VM (build 11.0-b15, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Ubuntu Linux 8.10, kernel 2.6.27-11-generic

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Not useful here.

      A DESCRIPTION OF THE PROBLEM :
      The JButton class inherits the abstract method "setSelected(boolean b)" from the abstract class AbstractButton.

      The description states the following :

      ====
          Sets the state of the button. Note that this method does not trigger an actionEvent. Call doClick to perform a programatic action change.
      ====

      1) It doesn't explicitly tell that an ItemEvent is fired (to all registered ItemListener) when the selection state of the button has changed.
      2) Worse, even if the ItemEvent class has been specifically designed for this kind of situation, we could understand, at first, that nothing is fired when reading this described behavior.

      I suggest this comment fix :

      ====
          Sets the state of the button and notifies all listeners that have registered interest for notification on "this". Note that this method does not trigger an actionEvent. Call doClick to perform a programatic action change.
      ====


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Create and show some kind of Swing container (JFrame + JPanel).
      2) Add a new button with a registered ItemListener which will print something on the console.
      3) Change the selected state of the button of interest via button.setSelected(bool).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Reading the setSelected() description, I didn't think that an ItemEvent would be fired with that method. I thought a I had to override the method and make an explicit call to fireItemStateChanged().
      ACTUAL -
      setSelected(bool) fires ItemEvent in JButton.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No crash.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.event.*;

      public class Test implements ItemListener, ActionListener {

          JFrame frame;
          JPanel framePanel;
          JButton button;

          public Test() {
      frame = new JFrame();
      framePanel = new JPanel();
      button = new JButton("(UN)SELECT ME BY CLICKING ON ME !");

      //(Un)Selection of the button is made through action on it.
      button.addActionListener(this);
      button.addItemListener(this);

      frame.setContentPane(framePanel);
      framePanel.add(button);

      frame.pack();
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          }

          public void itemStateChanged(ItemEvent e) {
      if (button.isSelected()) {
      System.out.println("(* 6 7)");
      }
      else {
      System.out.println("life(_) :- disp('42').");
      }

          }

          public void actionPerformed(ActionEvent e) {
      button.setSelected(!button.isSelected());
          }

          public static void main(String[] args) {
      new Test();
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I've just "reverse engineered" (what a rocket science thing !) the curious behavior ;-) .

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Imported:
              Indexed: