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

JButton.setAction or new JButton( Action ) do not behave as expected.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.3.0
    • 1.3.0
    • client-libs
    • kestrel
    • x86, sparc
    • generic, windows_95



      Name: skT88420 Date: 08/27/99


      The JButton( Action x ) constructor and
      JButton.setAction( Action x ) method do not behave as expected from the provided documentation.

      Basically, the NAME property from the action is not used to set the text of the button; while the SHORT_DESCRIPTION and SMALL_ICON properties are set to the ToolTipText and Icon as exepcted. These methods in the similar classes JCheckBox and JRadioButton work as expected.

       From 'javap -private javax.swing.JButton'

      Method void configurePropertiesFromAction(javax.swing.Action)
         0 aload_0
         1 aload_1
         2 ifnull 28
         5 aload_0
         6 ldc #20 <String "displayActionText">
         8 invokevirtual #21 <Method java.lang.Object getClientProperty(java.lang.Object)>
        11 ifnull 28
        14 aload_1
        15 ldc #22 <String "Name">
        17 invokeinterface (args 2) #23 <InterfaceMethod java.lang.Object getValue(java.lang.String)>
        22 checkcast #24 <Class java.lang.String>
        25 goto 29
        28 aconst_null
        29 invokevirtual #25 <Method void setText(java.lang.String)>
        32 aload_0
        33 aload_1
        34 ifnull 51
        37 aload_1
        38 ldc #26 <String "SmallIcon">
        40 invokeinterface (args 2) #23 <InterfaceMethod java.lang.Object getValue(java.lang.String)>
        45 checkcast #27 <Class javax.swing.Icon>
        48 goto 52
        51 aconst_null
        52 invokevirtual #28 <Method void setIcon(javax.swing.Icon)>
        55 aload_0
        56 aload_1
        57 ifnull 69
        60 aload_1
        61 invokeinterface (args 1) #29 <InterfaceMethod boolean isEnabled()>
        66 goto 70
        69 iconst_1
        70 invokevirtual #30 <Method void setEnabled(boolean)>
        73 aload_0
        74 aload_1
        75 ifnull 92
        78 aload_1
        79 ldc #31 <String "ShortDescription">
        81 invokeinterface (args 2) #23 <InterfaceMethod java.lang.Object getValue(java.lang.String)>
        86 checkcast #24 <Class java.lang.String>
        89 goto 93
        92 aconst_null
        93 invokevirtual #32 <Method void setToolTipText(java.lang.String)>
        96 aload_0
        97 iconst_0
        98 invokevirtual #33 <Method void setHorizontalTextPosition(int)>
       101 aload_0
       102 iconst_3
       103 invokevirtual #34 <Method void setVerticalTextPosition(int)>
       106 return

      ----- End of javap

      It appears that the code in JButton looks for a client property called 'displayActionText' (see bytecode 6). There is no documentation describing how to use this; and doing a search on the JDK 1.3 class files reveals that this is the only occurrence of this property.

      Specifically with the constructor, there is no way to set this property before requiring it. Also, even with the setAction method this requires the property to be set on every JButton created.

      Some code to demonstrate:

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

      public class ActionTest extends AbstractAction
      {
          public static void main( String[] args )
          {
              JButton button = new JButton( "Button Before Text" );
              button.setToolTipText( "Button Before Tooltip" );
              JCheckBox checkbox = new JCheckBox( "CheckBox Before Text" );
              checkbox.setToolTipText( "CheckBox Before Tooltip" );
              JRadioButton radiobutton = new JRadioButton( "RadioButton Before Text" );
              radiobutton.setToolTipText( "RadioButton Before Tooltip" );
              
              System.out.println( "Button Text: " + button.getText() );
              System.out.println( "Button Tooltip: " + button.getToolTipText() );
              button.setAction( new ActionTest( "Button" ) );
              System.out.println( "Button Text After: " + button.getText() );
              System.out.println( "Button Tooltip After: "
                  + button.getToolTipText() );
              button.putClientProperty( "displayActionText" , "true" );
              button.setAction( new ActionTest( "Button (displayActionText)" ) );
              System.out.println( " Set the displayActionText client property" );
              System.out.println( "Button Text After set: " + button.getText() );
              System.out.println( "Button Tooltip After set: "
                  + button.getToolTipText() );
              System.out.println();
              
              System.out.println( "CheckBox Text: " + checkbox.getText() );
              System.out.println( "CheckBox Tooltip: " + checkbox.getToolTipText() );
              checkbox.setAction( new ActionTest( "CheckBox" ) );
              System.out.println( "CheckBox Text After: " + checkbox.getText() );
              System.out.println( "CheckBox Tooltip After: "
                  + checkbox.getToolTipText() );
              System.out.println();
              
              System.out.println( "RadioButton Text: " + radiobutton.getText() );
              System.out.println( "RadioButton Tooltip: "
                  + radiobutton.getToolTipText() );
              radiobutton.setAction( new ActionTest( "RadioButton" ) );
              System.out.println( "RadioButton Text After: "
                  + radiobutton.getText() );
              System.out.println( "RadioButton Tooltip After: "
                  + radiobutton.getToolTipText() );
              
              System.exit( 0 );
          }
          
          public ActionTest( String type )
          {
              putValue( NAME , type + " After Name" );
              putValue( SHORT_DESCRIPTION , type + " After Short Description" );
          }
          
          public void actionPerformed( ActionEvent evt )
          {
              // Do nothing
          }
      }

      ----- End of Code

      Produces output:
      Button Text: Button Before Text
      Button Tooltip: Button Before Tooltip
      Button Text After: null
      Button Tooltip After: Button After Short Description
        Set the displayActionText client property
      Button Text After set: Button (displayActionText) After Name
      Button Tooltip After set: Button (displayActionText) After Short Description

      CheckBox Text: CheckBox Before Text
      CheckBox Tooltip: CheckBox Before Tooltip
      CheckBox Text After: CheckBox After Name
      CheckBox Tooltip After: CheckBox After Short Description

      RadioButton Text: RadioButton Before Text
      RadioButton Tooltip: RadioButton Before Tooltip
      RadioButton Text After: RadioButton After Name
      RadioButton Tooltip After: RadioButton After Short Description
      ------ End of output

      java -version :>
      java version "1.3beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
      Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)

      java -fullversion :>
      java full version "1.3beta-O"
      (Review ID: 94553)
      ======================================================================

            gsaab Georges Saab
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: