AWT Classes java.awt package|| Introduction to AWT || Bcis Notes

AWT Classes java.awt package|| Introduction to AWT || Bcis Notes

AWT Classes java.awt package

The AWT classes are contained in the java.awt package. It is one of Java’s largest packages. The AWT provides nine basic non-container component classes from which a user interface may be constructed. Awt classes java.awt package are class Button, Canvas, Checkbox, Choice, Label, List, Scrollbar, TextArea, TextField.

Window Fundamentals

The AWT defines windows according to a class hierarchy that adds functionality and specificity to each level. The two most common windows are those derived from Panel, which is used by applets and those derived from Frame, which creates a standard window. Much of the functionality of these windows is derived from their parent classes. Thus, a description of the class hierarchies relating to these two classes is fundamental to their understanding. The figure below shows the class hierarchy for Panel and Frame.

AWT hierarchy

1.Component
At the top of the AWT, hierarchy is the Component class. The component is an abstract class that encapsulates all of the attributes of a visual component. All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component. It defines over a hundred public methods that are responsible for managing events, such as mouse and keyboard input, positioning and sizing the window, and repainting. A Component object is responsible for remembering the current foreground and background colors and the currently selected text font.

2.Container
The Container class is a subclass of Component. It has additional methods that allow other Component objects to be nested within it. Other Container objects can be stored inside of a Container (since they are themselves instances of Component). This makes for a multileveled containment system. A container is responsible for laying out (that is, positioning) any components that it contains. It does this through the use of various layout managers.

3.Panel
The Panel class is a concrete subclass of Container. It doesn’t add any new methods; it simply implements Container. A Panel may be thought of as a recursively nestable, concrete screen component. The panel is the super-class for Applet. When screen output is directed to an applet, it is drawn on the surface of a Panel object. In essence, a Panel is a window that does not contain a title bar, menu bar, or border. This is why we don’t see these items when an applet is run inside a browser. When we run an applet using an applet viewer, the applet viewer provides the title and border. Other components can be added to a Panel object by its add( ) method (inherited from Container). Once these components have been added, we can position and resize them manually using the setLocation( ), setSize( ), or setBounds() methods defined by Component.

4.Window
The Window class creates a top-level window. A top-level window is not contained within any other object; it sits directly on the desktop. Generally, we won’t create Window objects directly. Instead, we will use a subclass of Window called Frame.

5.Frame :
Frame encapsulates what is commonly thought of as a “window.” It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners. If we create a Frame object from within an applet, it will contain a warning message, such as “Java Applet Window,” to the user that an applet window has been created. This message warns users that the window they see was started by an applet and not by software running on their computer. When a Frame window is created by a program rather than an applet, a normal window is created.

6.Canvas
Canvas encapsulates a blank window upon which we can draw.
7.Button
This class represents a push-button which displays some specified text. When a button is pressed, it notifies its Listeners. To be a Listener for a button, an object must implement the ActionListener interface.
Panel aPanel = new Panel();
Button okButton = new Button(“Ok”);
Button cancelButton = new Button(“Cancel”);

aPanel.add(okButton));
aPanel.add(cancelButton));
okButton.addActionListener(controller2);
cancelButton.addActionListener(controller1);

8.Labels
This class is a Component that displays a single line of text. Labels are read-only. That is, the user cannot click on a label to edit the text it displays. Text can be aligned within the label
Label aLabel = new Label(“Enter password:”)
aLabel.setAlignment(Label.RIGHT);
aPanel.add(aLabel);

9.List
This class is a Component which displays a list of Strings. The list is scrollable, if necessary. Sometimes called Listbox in other languages. Lists can be set up to allow single or multiple selections. The list will return an array indicating which Strings are selected
List aList = new List();
aList.add(“Calgary”);
aList.add(“Edmonton”);
aList.add(“Regina”);
aList.add(“Vancouver”);
aList.setMultipleMode(true);

Checkbox
This class represents a GUI checkbox with a textual label. The Checkbox maintains a boolean state indicating whether it is checked or not. If a Checkbox is added to a CheckBoxGroup, it will behave like a radio button.
Checkbox creamCheckbox = new CheckBox(“Cream”);
Checkbox sugarCheckbox = new CheckBox(“Sugar”);
[…]
if (creamCheckbox.getState())
{
coffee.addCream();
}

Choice
This class represents a dropdown list of Strings. Similar to a list in terms of functionality, but displayed differently. Only one item from the list can be selected at one time and the currently selected element is displayed.
Choice aChoice = new Choice();
aChoice.add(“Calgary”);
aChoice.add(“Edmonton”);
aChoice.add(“Alert Bay”);
[…]
String selectedDestination= aChoice.getSelectedItem();

TextField
This class displays a single line of optionally editable text. This class inherits several methods from TextComponent. This is one of the most commonly used Components in the AWT
TextField emailTextField = new TextField();
TextField passwordTextField = new TextField();
passwordTextField.setEchoChar(“*”);
[…]
String userEmail = emailTextField.getText();
String userpassword = passwordTextField.getText();

TextArea
This class displays multiple lines of optionally editable text. This class inherits several methods from TextComponent. TextArea also provides the methods: appendText(), insertText() and replaceText()
// 5 rows, 80 columns
TextArea fullAddressTextArea = new TextArea(5, 80);
[…]
String userFullAddress= fullAddressTextArea.getText();

 

you may also like Abstract Window Toolkit

Be the first to comment

Leave a Reply

Your email address will not be published.


*