Search This Blog

ADVANCED JAVA

C-PROGRAMMING

Wednesday, December 28, 2022

Java AWT Button

 

Java AWT Button

A button is basically a control component with a label that generates an event when pushed. 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 pushed.

When we press a button and release it, AWT sends an instance of ActionEvent to that button by calling processEvent on the button. The processEvent method of the button receives the 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

  1. public class Button extends Component implements Accessible  

Button Class Constructors

Following table shows the types of Button class constructors

Sr. no.ConstructorDescription
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

Sr. no.MethodDescription
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.

Note: The Button class inherits methods from java.awt.Component and java.lang.Object classes.

Java AWT Button Example

Example 1:

ButtonExample.java

  1. import java.awt.*;    
  2. public class ButtonExample {    
  3. public static void main (String[] args) {   
  4.   
  5.     // create instance of frame with the label   
  6.     Frame f = new Frame("Button Example");    
  7.   
  8.     // create instance of button with label  
  9.     Button b = new Button("Click Here");   
  10.   
  11.     // set the position for the button in frame   
  12.     b.setBounds(50,100,80,30);    
  13.   
  14.     // add button to the frame  
  15.     f.add(b);    
  16.     // set size, layout and visibility of frame  
  17.     f.setSize(400,400);    
  18.     f.setLayout(null);    
  19.     f.setVisible(true);     
  20. }    
  21. }    

To compile the program using command prompt type the following commands

  1. C:\Users\Anurati\Desktop\abcDemo>javac ButtonExample.java  

If there's no error, we can execute the code using:

  1. C:\Users\Anurati\Desktop\abcDemo>java ButtonExample  

Output:

Java AWT Button

Example 2:

  1. // importing necessary libraries  
  2. import javax.swing.*;  
  3. import java.awt.*;  
  4. import java.awt.event.*;  
  5.   
  6. public class ButtonExample2  
  7. {  
  8. // creating instances of Frame class and Button class  
  9. Frame fObj;  
  10. Button button1, button2, button3;  
  11. // instantiating using the constructor   
  12. ButtonExample2() {  
  13. fObj = new Frame ("Frame to display buttons");  
  14. button1 = new Button();  
  15. button2 = new Button ("Click here");  
  16. button3 = new Button();  
  17. button3.setLabel("Button 3");  
  18. fObj.add(button1);  
  19. fObj.add(button2);  
  20. fObj.add(button3);  
  21. fObj.setLayout(new FlowLayout());  
  22. fObj.setSize(300,400);  
  23. fObj.setVisible(true);  
  24. }  
  25. // main method  
  26. public static void main (String args[])  
  27. {  
  28. new ButtonExample2();  
  29. }  
  30. }  

Output:

Java AWT Button

Java AWT Button Example with ActionListener

Example:

In the following example, we are handling the button click events by implementing ActionListener Interface.

ButtonExample3.java

  1. // importing necessary libraries  
  2. import java.awt.*;    
  3. import java.awt.event.*;    
  4. public class ButtonExample3 {    
  5. public static void main(String[] args) {    
  6.     // create instance of frame with the label   
  7.     Frame f = new Frame("Button Example");    
  8.     final TextField tf=new TextField();    
  9.     tf.setBounds(50,50150,20);  
  10.     // create instance of button with label  
  11.     Button b=new Button("Click Here");    
  12.     // set the position for the button in frame   
  13.     b.setBounds(50,100,60,30);   
  14.     b.addActionListener(new ActionListener() {    
  15.     public void actionPerformed (ActionEvent e) {    
  16.             tf.setText("Welcome to Javatpoint.");    
  17.         }    
  18.     });  
  19. // adding button the frame  
  20.     f.add(b);  
  21. // adding textfield the frame  
  22.     f.add(tf);    
  23. // setting size, layout and visibility   
  24.     f.setSize(400,400);    
  25.     f.setLayout(null);    
  26.     f.setVisible(true);     
  27. }    
  28. }    

Output:

Java AWT Button

0 comments:

Post a Comment

OS

Top