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

REGRESSION:JButton with setSelected(true) loses selected state on gaining focus

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b94
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      Name: gm110360 Date: 05/19/2004


      FULL PRODUCT VERSION :
      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      When you have a JButton with an icon for for both setIcon and setSelectedIcon and the button is set to the selected state it becomes un-selected when the mouse is over the button. Almost as if the act of gaining the focus makes it unselected. Then when it loses the mouse focus it becomes selected again.

      It is hard to notice without an icon being set on the button, but that may be the cause as well.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Create a JFrame and add two buttons.
      2) Set icons for regular state and selected state.
      3) Set one of the buttons to be selected using setSelected(true)

      4) When running, move the mouse between the two buttons, you will notice that when the selected button gains the mouse focus it becomes un-selected. Then when it loses the mouse focus it becomes selected again. This never happened in any prior JDK release.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A JButton should not be changing selected/unselected state based on gaining/losing the mouse focus as default behavior. The JavaDocs make no mention of this behavior and this did not happen in any prior JDK release.

      A JButton should remain selected until it's setSelected(false) is called.
      ACTUAL -
      I saw the selected button become unselected and then go back to normal when the mouse passed over and left it.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      There were no error messages or stack traces.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      This is a simple application. The Java Look And Feel Graphics Repository must be in the classpath.


      import java.awt.FlowLayout;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;
      import java.net.URL;
      import javax.swing.JFrame;
      import javax.swing.JButton;
      import javax.swing.JToggleButton;
      import javax.swing.ImageIcon;

      /**
       * Created by IntelliJ IDEA.
      * You must have the Java Look and Feel Graphics Repository .JAR in classpath.. jlfgr-1_0.jar
       */
      public class ButtonTest extends JFrame{

          public static void main(String args[]){
              ButtonTest test=new ButtonTest();
              JButton selectedButton=new JButton("Selected Button");
              JToggleButton tog=new JToggleButton("Toggle Button");
              URL url=test.getClass().getResource("/toolbarButtonGraphics/general/Add24.gif");
              ImageIcon icon=new ImageIcon(url);
              selectedButton.setIcon(icon);
              tog.setIcon(icon);

              url=test.getClass().getResource("/toolbarButtonGraphics/general/Remove24.gif");
              icon=new ImageIcon(url);
              selectedButton.setSelectedIcon(icon);
              tog.setSelectedIcon(icon);

              selectedButton.setSelected(true);
              tog.setSelected(true);

              JButton normalButton=new JButton("Normal Button");
              test.getContentPane().setLayout(new FlowLayout());
              test.getContentPane().add(selectedButton);
              test.getContentPane().add(normalButton);
              test.getContentPane().add(tog);
              test.pack();
              test.setVisible(true);

              test.addWindowListener(new WindowAdapter(){
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      There is none, the only work around I can think of will be to attach a mouse listener to every button to re-invoke setSelected(true) when focus is gained.

      Release Regression From : 1.4.2
      The above release value was the last known release where this
      bug was known to work. Since then there has been a regression.

      (Incident Review ID: 237368)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              shickeysunw Shannon Hickey (Inactive)
              Reporter:
              gmanwanisunw Girish Manwani (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: