src/share/classes/java/awt/Button.java

Print this page
rev 10048 : 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
Reviewed-by:


  65  * <code>processEvent</code> on the button. The button's
  66  * <code>processEvent</code> method receives all events
  67  * for the button; it passes an action event along by
  68  * calling its own <code>processActionEvent</code> method.
  69  * The latter method passes the action event on to any action
  70  * listeners that have registered an interest in action
  71  * events generated by this button.
  72  * <p>
  73  * If an application wants to perform some action based on
  74  * a button being pressed and released, it should implement
  75  * <code>ActionListener</code> and register the new listener
  76  * to receive events from this button, by calling the button's
  77  * <code>addActionListener</code> method. The application can
  78  * make use of the button's action command as a messaging protocol.
  79  *
  80  * @author      Sami Shaio
  81  * @see         java.awt.event.ActionEvent
  82  * @see         java.awt.event.ActionListener
  83  * @see         java.awt.Component#processMouseEvent
  84  * @see         java.awt.Component#addMouseListener
  85  * @since       JDK1.0
  86  */
  87 public class Button extends Component implements Accessible {
  88 
  89     /**
  90      * The button's label.  This value may be null.
  91      * @serial
  92      * @see #getLabel()
  93      * @see #setLabel(String)
  94      */
  95     String label;
  96 
  97     /**
  98      * The action to be performed once a button has been
  99      * pressed.  This value may be null.
 100      * @serial
 101      * @see #getActionCommand()
 102      * @see #setActionCommand(String)
 103      */
 104     String actionCommand;
 105 


 211                 testvalid = true;
 212             }
 213         }
 214 
 215         // This could change the preferred size of the Component.
 216         if (testvalid) {
 217             invalidateIfValid();
 218         }
 219     }
 220 
 221     /**
 222      * Sets the command name for the action event fired
 223      * by this button. By default this action command is
 224      * set to match the label of the button.
 225      *
 226      * @param     command  a string used to set the button's
 227      *                  action command.
 228      *            If the string is <code>null</code> then the action command
 229      *            is set to match the label of the button.
 230      * @see       java.awt.event.ActionEvent
 231      * @since     JDK1.1
 232      */
 233     public void setActionCommand(String command) {
 234         actionCommand = command;
 235     }
 236 
 237     /**
 238      * Returns the command name of the action event fired by this button.
 239      * If the command name is <code>null</code> (default) then this method
 240      * returns the label of the button.
 241      */
 242     public String getActionCommand() {
 243         return (actionCommand == null? label : actionCommand);
 244     }
 245 
 246     /**
 247      * Adds the specified action listener to receive action events from
 248      * this button. Action events occur when a user presses or releases
 249      * the mouse over this button.
 250      * If l is null, no exception is thrown and no action is performed.
 251      * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
 252      * >AWT Threading Issues</a> for details on AWT's threading model.
 253      *
 254      * @param         l the action listener
 255      * @see           #removeActionListener
 256      * @see           #getActionListeners
 257      * @see           java.awt.event.ActionListener
 258      * @since         JDK1.1
 259      */
 260     public synchronized void addActionListener(ActionListener l) {
 261         if (l == null) {
 262             return;
 263         }
 264         actionListener = AWTEventMulticaster.add(actionListener, l);
 265         newEventsOnly = true;
 266     }
 267 
 268     /**
 269      * Removes the specified action listener so that it no longer
 270      * receives action events from this button. Action events occur
 271      * when a user presses or releases the mouse over this button.
 272      * If l is null, no exception is thrown and no action is performed.
 273      * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
 274      * >AWT Threading Issues</a> for details on AWT's threading model.
 275      *
 276      * @param           l     the action listener
 277      * @see             #addActionListener
 278      * @see             #getActionListeners
 279      * @see             java.awt.event.ActionListener
 280      * @since           JDK1.1
 281      */
 282     public synchronized void removeActionListener(ActionListener l) {
 283         if (l == null) {
 284             return;
 285         }
 286         actionListener = AWTEventMulticaster.remove(actionListener, l);
 287     }
 288 
 289     /**
 290      * Returns an array of all the action listeners
 291      * registered on this button.
 292      *
 293      * @return all of this button's <code>ActionListener</code>s
 294      *         or an empty array if no action
 295      *         listeners are currently registered
 296      *
 297      * @see             #addActionListener
 298      * @see             #removeActionListener
 299      * @see             java.awt.event.ActionListener
 300      * @since 1.4


 353                 actionListener != null) {
 354                 return true;
 355             }
 356             return false;
 357         }
 358         return super.eventEnabled(e);
 359     }
 360 
 361     /**
 362      * Processes events on this button. If an event is
 363      * an instance of <code>ActionEvent</code>, this method invokes
 364      * the <code>processActionEvent</code> method. Otherwise,
 365      * it invokes <code>processEvent</code> on the superclass.
 366      * <p>Note that if the event parameter is <code>null</code>
 367      * the behavior is unspecified and may result in an
 368      * exception.
 369      *
 370      * @param        e the event
 371      * @see          java.awt.event.ActionEvent
 372      * @see          java.awt.Button#processActionEvent
 373      * @since        JDK1.1
 374      */
 375     protected void processEvent(AWTEvent e) {
 376         if (e instanceof ActionEvent) {
 377             processActionEvent((ActionEvent)e);
 378             return;
 379         }
 380         super.processEvent(e);
 381     }
 382 
 383     /**
 384      * Processes action events occurring on this button
 385      * by dispatching them to any registered
 386      * <code>ActionListener</code> objects.
 387      * <p>
 388      * This method is not called unless action events are
 389      * enabled for this button. Action events are enabled
 390      * when one of the following occurs:
 391      * <ul>
 392      * <li>An <code>ActionListener</code> object is registered
 393      * via <code>addActionListener</code>.
 394      * <li>Action events are enabled via <code>enableEvents</code>.
 395      * </ul>
 396      * <p>Note that if the event parameter is <code>null</code>
 397      * the behavior is unspecified and may result in an
 398      * exception.
 399      *
 400      * @param       e the action event
 401      * @see         java.awt.event.ActionListener
 402      * @see         java.awt.Button#addActionListener
 403      * @see         java.awt.Component#enableEvents
 404      * @since       JDK1.1
 405      */
 406     protected void processActionEvent(ActionEvent e) {
 407         ActionListener listener = actionListener;
 408         if (listener != null) {
 409             listener.actionPerformed(e);
 410         }
 411     }
 412 
 413     /**
 414      * Returns a string representing the state of this <code>Button</code>.
 415      * This method is intended to be used only for debugging purposes, and the
 416      * content and format of the returned string may vary between
 417      * implementations. The returned string may be empty but may not be
 418      * <code>null</code>.
 419      *
 420      * @return     the parameter string of this button
 421      */
 422     protected String paramString() {
 423         return super.paramString() + ",label=" + label;
 424     }




  65  * <code>processEvent</code> on the button. The button's
  66  * <code>processEvent</code> method receives all events
  67  * for the button; it passes an action event along by
  68  * calling its own <code>processActionEvent</code> method.
  69  * The latter method passes the action event on to any action
  70  * listeners that have registered an interest in action
  71  * events generated by this button.
  72  * <p>
  73  * If an application wants to perform some action based on
  74  * a button being pressed and released, it should implement
  75  * <code>ActionListener</code> and register the new listener
  76  * to receive events from this button, by calling the button's
  77  * <code>addActionListener</code> method. The application can
  78  * make use of the button's action command as a messaging protocol.
  79  *
  80  * @author      Sami Shaio
  81  * @see         java.awt.event.ActionEvent
  82  * @see         java.awt.event.ActionListener
  83  * @see         java.awt.Component#processMouseEvent
  84  * @see         java.awt.Component#addMouseListener
  85  * @since       1.0
  86  */
  87 public class Button extends Component implements Accessible {
  88 
  89     /**
  90      * The button's label.  This value may be null.
  91      * @serial
  92      * @see #getLabel()
  93      * @see #setLabel(String)
  94      */
  95     String label;
  96 
  97     /**
  98      * The action to be performed once a button has been
  99      * pressed.  This value may be null.
 100      * @serial
 101      * @see #getActionCommand()
 102      * @see #setActionCommand(String)
 103      */
 104     String actionCommand;
 105 


 211                 testvalid = true;
 212             }
 213         }
 214 
 215         // This could change the preferred size of the Component.
 216         if (testvalid) {
 217             invalidateIfValid();
 218         }
 219     }
 220 
 221     /**
 222      * Sets the command name for the action event fired
 223      * by this button. By default this action command is
 224      * set to match the label of the button.
 225      *
 226      * @param     command  a string used to set the button's
 227      *                  action command.
 228      *            If the string is <code>null</code> then the action command
 229      *            is set to match the label of the button.
 230      * @see       java.awt.event.ActionEvent
 231      * @since     1.1
 232      */
 233     public void setActionCommand(String command) {
 234         actionCommand = command;
 235     }
 236 
 237     /**
 238      * Returns the command name of the action event fired by this button.
 239      * If the command name is <code>null</code> (default) then this method
 240      * returns the label of the button.
 241      */
 242     public String getActionCommand() {
 243         return (actionCommand == null? label : actionCommand);
 244     }
 245 
 246     /**
 247      * Adds the specified action listener to receive action events from
 248      * this button. Action events occur when a user presses or releases
 249      * the mouse over this button.
 250      * If l is null, no exception is thrown and no action is performed.
 251      * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
 252      * >AWT Threading Issues</a> for details on AWT's threading model.
 253      *
 254      * @param         l the action listener
 255      * @see           #removeActionListener
 256      * @see           #getActionListeners
 257      * @see           java.awt.event.ActionListener
 258      * @since         1.1
 259      */
 260     public synchronized void addActionListener(ActionListener l) {
 261         if (l == null) {
 262             return;
 263         }
 264         actionListener = AWTEventMulticaster.add(actionListener, l);
 265         newEventsOnly = true;
 266     }
 267 
 268     /**
 269      * Removes the specified action listener so that it no longer
 270      * receives action events from this button. Action events occur
 271      * when a user presses or releases the mouse over this button.
 272      * If l is null, no exception is thrown and no action is performed.
 273      * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
 274      * >AWT Threading Issues</a> for details on AWT's threading model.
 275      *
 276      * @param           l     the action listener
 277      * @see             #addActionListener
 278      * @see             #getActionListeners
 279      * @see             java.awt.event.ActionListener
 280      * @since           1.1
 281      */
 282     public synchronized void removeActionListener(ActionListener l) {
 283         if (l == null) {
 284             return;
 285         }
 286         actionListener = AWTEventMulticaster.remove(actionListener, l);
 287     }
 288 
 289     /**
 290      * Returns an array of all the action listeners
 291      * registered on this button.
 292      *
 293      * @return all of this button's <code>ActionListener</code>s
 294      *         or an empty array if no action
 295      *         listeners are currently registered
 296      *
 297      * @see             #addActionListener
 298      * @see             #removeActionListener
 299      * @see             java.awt.event.ActionListener
 300      * @since 1.4


 353                 actionListener != null) {
 354                 return true;
 355             }
 356             return false;
 357         }
 358         return super.eventEnabled(e);
 359     }
 360 
 361     /**
 362      * Processes events on this button. If an event is
 363      * an instance of <code>ActionEvent</code>, this method invokes
 364      * the <code>processActionEvent</code> method. Otherwise,
 365      * it invokes <code>processEvent</code> on the superclass.
 366      * <p>Note that if the event parameter is <code>null</code>
 367      * the behavior is unspecified and may result in an
 368      * exception.
 369      *
 370      * @param        e the event
 371      * @see          java.awt.event.ActionEvent
 372      * @see          java.awt.Button#processActionEvent
 373      * @since        1.1
 374      */
 375     protected void processEvent(AWTEvent e) {
 376         if (e instanceof ActionEvent) {
 377             processActionEvent((ActionEvent)e);
 378             return;
 379         }
 380         super.processEvent(e);
 381     }
 382 
 383     /**
 384      * Processes action events occurring on this button
 385      * by dispatching them to any registered
 386      * <code>ActionListener</code> objects.
 387      * <p>
 388      * This method is not called unless action events are
 389      * enabled for this button. Action events are enabled
 390      * when one of the following occurs:
 391      * <ul>
 392      * <li>An <code>ActionListener</code> object is registered
 393      * via <code>addActionListener</code>.
 394      * <li>Action events are enabled via <code>enableEvents</code>.
 395      * </ul>
 396      * <p>Note that if the event parameter is <code>null</code>
 397      * the behavior is unspecified and may result in an
 398      * exception.
 399      *
 400      * @param       e the action event
 401      * @see         java.awt.event.ActionListener
 402      * @see         java.awt.Button#addActionListener
 403      * @see         java.awt.Component#enableEvents
 404      * @since       1.1
 405      */
 406     protected void processActionEvent(ActionEvent e) {
 407         ActionListener listener = actionListener;
 408         if (listener != null) {
 409             listener.actionPerformed(e);
 410         }
 411     }
 412 
 413     /**
 414      * Returns a string representing the state of this <code>Button</code>.
 415      * This method is intended to be used only for debugging purposes, and the
 416      * content and format of the returned string may vary between
 417      * implementations. The returned string may be empty but may not be
 418      * <code>null</code>.
 419      *
 420      * @return     the parameter string of this button
 421      */
 422     protected String paramString() {
 423         return super.paramString() + ",label=" + label;
 424     }