Saturday, December 11, 2021

Java AWT Button

A button is basically a control component with a label that generates an event when pressed. The Button class is used to create a labeled button that has platform independent implementation. The
application result in some action when the button is pressed.

When a button is pressed and released, AWT sends an instance of ActionEvent to that button by calling processEvent on the button. The processEvent method of the button receives all the events, then it passes an action event by calling its own method processActionEvent. This method passes the action event on to action listeners that are interested in the action events generated by the button.

To perform an action on a button being pressed and released, the ActionListener interface needs to be
implemented. The registered new listener can receive events from the button by calling addActionListener method of the button. The Java application can use the button's action command as a messaging protocol.

AWT Button Class Declaration:

public class Button extends Component implements Accessible  

Following shows the types of Button class constructors:

1.   Button( ) - It constructs a new button with an empty string i.e. it has no label.

2.   Button (String text) - It constructs a new button with given string as its label.

Button Class Methods:

1. void setText (String text)  - It sets the string message on the button

2. String getText()  - It fetches the String message on the button.

3. void setLabel (String label)  - It sets the label of button with the specified string.

4. String getLabel() - It fetches the label of the button.

5. void addNotify() - It creates the peer of the button.

6. AccessibleContext getAccessibleContext() - It fetched the accessible context associated with the                                                                                     button.

7. void addActionListener(ActionListener l)  - It adds the specified action listener to get the action                                                                                     events from the button.

8. String getActionCommand() - It returns the command name of the action event fired by the button.

9. ActionListener[ ] getActionListeners()    - It returns an array of all the action listeners registered on                                                                            the button.

10. T[ ] getListeners(Class listenerType)  - It returns an array of all the objects currently registered as                                                                         FooListeners upon this Button.

11. protected String paramString() - It returns the string which represents the state of button.

12. protected void processActionEvent (ActionEvent e)    - It process the action events on the button by                                                                                                 dispatching them to a registered                                                                                                                     ActionListener object.

13. protected void processEvent (AWTEvent e)     - It process the events on the button

14. void removeActionListener (ActionListener l)      - It removes the specified action listener so that it                                                                                         no longer receives action events from the button.

15. void setActionCommand(String command)      - It sets the command name for the action event                                                                                             given by the button.

Ex:

import java.awt.*;   

import java.awt.event.*;   

public class ButtonExample3 {   

public static void main(String[] args) {   

    // create instance of frame with the label  

    Frame f = new Frame("Button Example");   

    final TextField tf=new TextField();   

    tf.setBounds(50,50, 150,20); 

    // create instance of button with label 

    Button b=new Button("Click Here");   

    // set the position for the button in frame  

    b.setBounds(50,100,60,30);  

    b.addActionListener(new ActionListener() {   

    public void actionPerformed (ActionEvent e) {   

            tf.setText("Welcome to Ashraf’s class.");   

        }   

    }); 

// adding button the frame  

f.add(b); 

// adding textfield the frame 

f.add(tf);   

// setting size, layout and visibility  

f.setSize(400,400);   

f.setLayout(null);   

f.setVisible(true);    

}   

    

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top