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

setSelectedIcon is ignored for JMenuItem and subclasses

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • client-libs
    • beta2
    • x86
    • windows_nt



      Name: boT120536 Date: 02/27/2001


      java version "1.3.0"

      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      Create a JRadioButtonMenuItem or JCheckBoxMenuItem and add to menu.
      Perform a setSelectedIcon( newIcon ) on that menuitem.
      Look at the menuitem in a menu.
      You will only be able to get the default icons for the .checkIcon

      In looking through the source code, it appears that BasicMenuItemUI.java
      will never look at the selectedIcon value, even the ones set by swing
      itself in the installDefaults() method of BasicRadioButtonMenuItemUI which
      sets the selected icon to the RadioButtonMenuItem.checkIcon value.

      Instead, BasicMenuItemUI sets its own Icon (checkIcon) to be that value in
      its own installDefaults() method and that is what it uses for painting and
      NOWHERE does it ever try to look at the getSelectedIcon().

      You currently see a radio button for the first
      selected menuitem and a checkbox for the second selected menuitem. These
      are the default icons for both of these. In my example, I retrieve the
      checkbox icon from the second menuitem, and I set it as the selected icon
      for the first one, so you should see TWO checkboxes, but you don't. The
      check.getSelectedIcon() will return the correct icon (i.e.
      javax.swing.plaf.metal.MetalIconFactory$CheckBoxMenuItemIcon@64dc11) but
      when set as the selected icon for the radio button, it is ignored). Note
      that setting it to any icon (I am trying to set it to my own icon) will
      fail, I just set it to a system icon because it simplified the example.
      Also, an additional problem is that there are really TWO icons that need to
      be replaced, but the API only allows a change of one (There is the empty
      radio button or empty checkbox and then the filled in radio button or the
      checked-off checkbox which is the selected icon).


      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      /*******************************************************************************
      * This class is demonstrates the problem with menu items in general
      (although
      * I found it in radio button menu item, the problem is really for all menu
      items)
      * as the bug appears to be in the BasicMenuItemUI class). If you change the
      * selected icon, it is ignored, as the UI renderer always uses the default
      * images (as described in the defect).
      *
      * @author David Bernath
      ********************************************************************************/
      public class RadioButtonExample
      {
        /**
         * Main routine
         */
        public static void main( String args[] )
        {
          // Create a standalone frame.
          JFrame f = new JFrame();
          f.setSize( 400, 200 );
          f.setLocation( 10,10 );

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

          // create a menu bar with two items
          JMenuBar mb = new JMenuBar();
          JMenu fileMenu = new JMenu( "File" );
          JRadioButtonMenuItem radio = new JRadioButtonMenuItem( "Example" );
          JCheckBoxMenuItem check = new JCheckBoxMenuItem( "Check" );

          // Change the radio button selected icon to use the check icon (or anyicon)
          radio.setSelectedIcon( check.getSelectedIcon() );
          fileMenu.add( radio );
          fileMenu.add( check );
          mb.add( fileMenu );
          f.setJMenuBar( mb );

          // Show it and you will always see the radio button icon.
          f.setVisible( true );
        }
      }
      (Review ID: 116799)
      ======================================================================

            mdavidsosunw Mark Davidson (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: