- Creating buttons in java
- Nested Class Summary
- Nested classes/interfaces declared in class java.awt.Component
- Field Summary
- Fields declared in class java.awt.Component
- Fields declared in interface java.awt.image.ImageObserver
- Constructor Summary
- Method Summary
- Methods declared in class java.awt.Component
- Methods declared in class java.lang.Object
- Constructor Detail
- Button
- Button
- Method Detail
- addNotify
- getLabel
- setLabel
- setActionCommand
- getActionCommand
- addActionListener
- removeActionListener
- getActionListeners
- getListeners
- processEvent
- processActionEvent
- paramString
- getAccessibleContext
- Class Button
- Class Button
Creating buttons in java
This class creates a labeled button. The application can cause some action to happen when the button is pushed. This image depicts three views of a » Quit » button as it appears under the Solaris operating system: The first view shows the button as it appears normally. The second view shows the button when it has input focus. Its outline is darkened to let the user know that it is an active object. The third view shows the button when the user clicks the mouse over the button, and thus requests that an action be performed. The gesture of clicking on a button with the mouse is associated with one instance of ActionEvent , which is sent out when the mouse is both pressed and released over the button. If an application is interested in knowing when the button has been pressed but not released, as a separate gesture, it can specialize processMouseEvent , or it can register itself as a listener for mouse events by calling addMouseListener . Both of these methods are defined by Component , the abstract superclass of all components. When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button. The button’s processEvent method receives all events for the button; it passes an action event along by calling its own processActionEvent method. The latter method passes the action event on to any action listeners that have registered an interest in action events generated by this button. If an application wants to perform some action based on a button being pressed and released, it should implement ActionListener and register the new listener to receive events from this button, by calling the button’s addActionListener method. The application can make use of the button’s action command as a messaging protocol.
Nested Class Summary
Nested classes/interfaces declared in class java.awt.Component
Field Summary
Fields declared in class java.awt.Component
Fields declared in interface java.awt.image.ImageObserver
Constructor Summary
Method Summary
Processes action events occurring on this button by dispatching them to any registered ActionListener objects.
Methods declared in class java.awt.Component
Methods declared in class java.lang.Object
Constructor Detail
Button
Button
public Button(String label) throws HeadlessException
Method Detail
addNotify
Creates the peer of the button. The button’s peer allows the application to change the look of the button without changing its functionality.
getLabel
setLabel
setActionCommand
Sets the command name for the action event fired by this button. By default this action command is set to match the label of the button.
getActionCommand
Returns the command name of the action event fired by this button. If the command name is null (default) then this method returns the label of the button.
addActionListener
Adds the specified action listener to receive action events from this button. Action events occur when a user presses or releases the mouse over this button. If l is null, no exception is thrown and no action is performed. Refer to AWT Threading Issues for details on AWT’s threading model.
removeActionListener
Removes the specified action listener so that it no longer receives action events from this button. Action events occur when a user presses or releases the mouse over this button. If l is null, no exception is thrown and no action is performed. Refer to AWT Threading Issues for details on AWT’s threading model.
getActionListeners
getListeners
public EventListener> T[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as FooListener s upon this Button . FooListener s are registered using the addFooListener method. You can specify the listenerType argument with a class literal, such as FooListener.class . For example, you can query a Button b for its action listeners with the following code:
ActionListener[] als = (ActionListener[])(b.getListeners(ActionListener.class));
processEvent
Processes events on this button. If an event is an instance of ActionEvent , this method invokes the processActionEvent method. Otherwise, it invokes processEvent on the superclass. Note that if the event parameter is null the behavior is unspecified and may result in an exception.
processActionEvent
- An ActionListener object is registered via addActionListener .
- Action events are enabled via enableEvents .
Note that if the event parameter is null the behavior is unspecified and may result in an exception.
paramString
Returns a string representing the state of this Button . This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null .
getAccessibleContext
@BeanProperty(expert=true, description="The AccessibleContext associated with this Button.") public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Button . For buttons, the AccessibleContext takes the form of an AccessibleAWTButton . A new AccessibleAWTButton instance is created if necessary.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Class Button
This class creates a labeled button. The application can cause some action to happen when the button is pushed. This image depicts three views of a » Quit » button as it appears under the Solaris operating system:
The first view shows the button as it appears normally. The second view shows the button when it has input focus. Its outline is darkened to let the user know that it is an active object. The third view shows the button when the user clicks the mouse over the button, and thus requests that an action be performed.
The gesture of clicking on a button with the mouse is associated with one instance of ActionEvent , which is sent out when the mouse is both pressed and released over the button. If an application is interested in knowing when the button has been pressed but not released, as a separate gesture, it can specialize processMouseEvent , or it can register itself as a listener for mouse events by calling addMouseListener . Both of these methods are defined by Component , the abstract superclass of all components.
When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button. The button’s processEvent method receives all events for the button; it passes an action event along by calling its own processActionEvent method. The latter method passes the action event on to any action listeners that have registered an interest in action events generated by this button.
If an application wants to perform some action based on a button being pressed and released, it should implement ActionListener and register the new listener to receive events from this button, by calling the button’s addActionListener method. The application can make use of the button’s action command as a messaging protocol.
Class Button
This class creates a labeled button. The application can cause some action to happen when the button is pushed. This image depicts three views of a » Quit » button as it appears under the Solaris operating system:
The first view shows the button as it appears normally. The second view shows the button when it has input focus. Its outline is darkened to let the user know that it is an active object. The third view shows the button when the user clicks the mouse over the button, and thus requests that an action be performed.
The gesture of clicking on a button with the mouse is associated with one instance of ActionEvent , which is sent out when the mouse is both pressed and released over the button. If an application is interested in knowing when the button has been pressed but not released, as a separate gesture, it can specialize processMouseEvent , or it can register itself as a listener for mouse events by calling addMouseListener . Both of these methods are defined by Component , the abstract superclass of all components.
When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button. The button’s processEvent method receives all events for the button; it passes an action event along by calling its own processActionEvent method. The latter method passes the action event on to any action listeners that have registered an interest in action events generated by this button.
If an application wants to perform some action based on a button being pressed and released, it should implement ActionListener and register the new listener to receive events from this button, by calling the button’s addActionListener method. The application can make use of the button’s action command as a messaging protocol.