Applet AWT Button Control

Applet AWT Button Control

 

Introduction


Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in java.
Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components are using the resources of OS.
The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

Java AWT Hierarchy

The hierarchy of Java AWT classes are given below.



Button is a control component that has a label and generates an event when pressed. 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 declaration


Following is the declaration for java.awt.Button class:

public class Button
   extends Component
      implements Accessible

 

Class constructors


S.N.
Constructor & Description
1
Button()
Constructs a button with an empty string for its label.
2
Button(String text)
Constructs a new button with specified label.

 

Class methods


S.N.
Method & Description
1
void addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this button.
2
void addNotify()
Creates the peer of the button.
3
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Button.
4
String getActionCommand()
Returns the command name of the action event fired by this button.
5
ActionListener[] getActionListeners()
Returns an array of all the action listeners registered on this button.
6
String getLabel()
Gets the label of this button.
7
<T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered as FooListeners upon this Button.
8
protected String paramString()
Returns a string representing the state of this Button.
9
protected void processActionEvent(ActionEvent e)
Processes action events occurring on this button by dispatching them to any registered ActionListener objects.
10
protected void processEvent(AWTEvent e)
Processes events on this button.
11
void removeActionListener(ActionListener l)
Removes the specified action listener so that it no longer receives action events from this button.
12
void setActionCommand(String command)
Sets the command name for the action event fired by this button.
13
void setLabel(String label)
Sets the button's label to be the specified string.

In this demo program i add 5 buttons and use different methods.



AWTButtonDemo.java

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

/*
 <applet code="AWTButtonDemo" width=300 height=300>
 </applet>
 */

public class AWTButtonDemo extends Applet {

            boolean isButton1Enabled;
            boolean isButton4Enabled;
            boolean isButton1Visible;
            boolean isButton3Visible;

            public void init() {

                        /*
                         * To create a button use Button() constructor.
                         */

                        Button button1 = new Button();
                        Button button2 = new Button("Button 2");
                        Button button3 = new Button("Button 3");
                        Button button4 = new Button("Button 4");
                        Button button5 = new Button("Button 5");

                        /*
                         * To create button with the caption use Button(String text) constructor
                         * of AWT Button class.
                         *
                         * Set button caption or label using void setLabel(String text) method
                         * of AWT Button class.
                         */

                        button1.setLabel("Button 1");

                        /*
                         * To change font of a button use setFont(Font f) method.
                         */

                        Font myFont = new Font("Courier", Font.ITALIC, 12);
                        button1.setFont(myFont);

                        myFont = new Font("Arial Black", Font.BOLD, 14);
                        button2.setFont(myFont);
                        /*
                         * To change foreground color of a button use setForeground(Color c)
                         * method.
                         */

                        button1.setForeground(Color.magenta);
                        button2.setForeground(Color.blue);

                        /*
                         * To change background color of a button use setBackground(Color c)
                         * method.
                         */

                        button1.setBackground(Color.cyan);
                        button2.setBackground(Color.green);

                        /*
                         * To hide a Button, use void setVisible(Boolean visible) method.
                         */

                        button3.setVisible(false);

                        /*
                         * To disable a Button, use void setEnabled(Boolean enable) method.
                         */

                        button4.setEnabled(false);
                        /*
                         * To determine if the Button is enabled or not use boolean isEnabled()
                         * method.
                         */

                        isButton1Enabled = button1.isEnabled();
                        isButton4Enabled = button4.isEnabled();

                        /*
                         * To determine if the Button is visible or not use boolean isVisible()
                         * method.
                         */

                        isButton1Visible = button1.isVisible();
                        isButton3Visible = button3.isVisible();

                        // add buttons using add method
                        add(button1);
                        add(button2);
                        add(button3);
                        add(button4);
                        add(button5);

                        /*
                         * To remove a Button from window, use void remove(Component c) method.
                         * Remove method removes the specified component from the container.
                         */

                        remove(button5);
            }

            public void paint(Graphics g) {
                        g.drawString("Is Button 1 enabled? " + isButton1Enabled, 10, 50);
                        g.drawString("Is Button 4 enabled? " + isButton4Enabled, 10, 70);
                        g.drawString("Is Button 1 visible? " + isButton1Visible, 10, 90);
                        g.drawString("Is Button 3 visible? " + isButton3Visible, 10, 110);
                        g.drawString("Button 5 is removed.", 10, 130);
            }
}






 In this demo program i add 2 buttons and use action event.

Action Events for AWT Button

/*
This java example shows how to handle action event of AWT Button by implementing
ActionListener interface.
 */

import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*
 <applet code="AWTButtonActionEventDemo" width=400 height=400>
 </applet>
 */
public class AWTButtonActionEventDemo extends Applet implements ActionListener {
            String str = "";
            String msg = "";

            public void init() {
                        // create Buttons
                        Button btnOK = new Button("Ok");
                        Button btnCancel = new Button("Cancel");

                        // add Buttons
                        add(btnOK);
                        add(btnCancel);

                        // set action listeners for buttons
                        btnOK.addActionListener(this);
                        btnCancel.addActionListener(this);
                       
                        /*
                         * By default, button's action command is its label. But in some cases,
                         * labels are too long and is not appropriate to use it as an action
                         * command. In such situation you would want to define custom short
                         * action command for a button.
                         *
                         * To set custom action command for a button, use void
                         * setActionCommand(String command) method of AWT Button class.
                         */
btnCancel.setActionCommand("No");
            }

            public void paint(Graphics g) {
                        g.drawString(str, 10, 50);
                        g.drawString(msg, 10, 80);
            }

            public void actionPerformed(ActionEvent ae) {

                        /*
                         * Get the action command using String getActionCommand() method.
                         */

                        String action = ae.getActionCommand();

                        if (action.equals("Ok"))
                                    str = "Ok Button Pressed";
                        else if (action.equals("Cancel"))
                                    str = "Cancel Button Pressed";

                        msg = ae.getActionCommand();
            msg += " button pressed!";
       
                        repaint();
            }
}






Comments

  1. Thank you for sharing this amazing post. Looking forward to reading more.
    Visit us: Java Training
    Visit us: Java Course

    ReplyDelete

Post a Comment

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes