IADCS Diploma Course
Abstract Windowing Toolkits
Introduction to AWT
AWT stands for Abstract Windowing Toolkit AWT is a set of Java classes that allows us to create a GUI Provides various items to create an attractive and efficient GUI
Containers Components Layout managers Graphics and drawing capabilities Fonts Events
2
Copyright : MCC
Abstract Windowing ToolKits
Introduction to AWT (Contd)
AWT package consists of classes, interfaces, and other packages
Copyright : MCC
Abstract Windowing ToolKits
Containers
Area where you can place your components Can hold elements, can be drawn on, and painted Can have frames, panes, latches, hooks, and smaller-sized components within it [Link] package contains a class named Container. This class directly or indirectly derives two commonly used containers
Frames Panels
4
Copyright : MCC
Abstract Windowing ToolKits
Frames
Are separate windows Are a subclass of Window Are displayed within a separate window, and have borders A Frame is independent of an applet, and of the browser Constructors Frame( ) Frame(String, title)
Abstract Windowing ToolKits
Copyright : MCC
Panels
Are areas contained within a window Are displayed within the window that the browser or the appletviewer provides and do not have borders Are used to group a number of components A panel cannot be seen directly. Hence, we need to add it to a frame Constructor Panel()
Copyright : MCC
Abstract Windowing ToolKits
Dialog
Is a subclass of the class Window The dialog object is constructed as follows:
Frame myframe = new Frame(My frame); String title = Title; boolean modal = true; Dialog dlg = new Dialog( myframe, title, modal);
Copyright : MCC
Abstract Windowing ToolKits
Components
Can be placed on a user interface, and can be resized, or made visible Examples textfields, labels, checkboxes, textareas scrollbars, scrollpanes, dialog
Copyright : MCC
Abstract Windowing ToolKits
Label
Is used to display the String Constructors Label( ) Label(String labeltext) Label(String labeltext, int alignment) Methods setFont(Font f) setText(String s) getText( )
Abstract Windowing ToolKits
Copyright : MCC
TextField
Is a single line area, in which text can be displayed, or entered by the user Constructors
TextField( ) TextField(int columns) TextField(String s) TextField(String s, int columns)
Methods
setEchoChar(char) - setTest(String s) getText( ) -setEditable(boolean) isEditable( )
Abstract Windowing ToolKits
Copyright : MCC
10
TextArea
Is used when text is to be accepted as two or more lines Is an editable text field with multi-line features Steps for creating TextArea Create an element Specify the number of rows or columns it must have (optional) Decide its placement on the screen
Copyright : MCC
Abstract Windowing ToolKits
11
TextArea (Contd)
Constructors TextArea( ) TextArea(int rows, int cols ) TextArea(String text) TextArea(String text, int rows, int cols)
Copyright : MCC
Abstract Windowing ToolKits
12
TextArea Methods
setText(String) getText( ) setEditable(boolean) isEditable( ) insertText(String, int) replaceText(String, int, int)
Copyright : MCC
Abstract Windowing ToolKits
13
Button
Push/ Command button is the easiest way to trap user action Steps for creating buttons
Create the button element, preferably with a label indicating its purpose Decide where to place it on the screen Display it on the screen Button( ) Button(String text)
Abstract Windowing ToolKits
Constructors
Copyright : MCC
14
Checkboxes and RadioButtons
Checkboxes are used for multi-option user input Radiobuttons are used as an option button to specify choices Steps to create checkboxes or radiobuttons
Create the element Decide its initial state (as selected or unselected) Decide its placement on the screen Display it on the screen Checkbox( ) Checkbox(String text)
Constructors to create checkboxes
To create radiobuttons, a CheckBoxGroup object is to be created before creating the buttons
Abstract Windowing ToolKits
Copyright : MCC
15
Lists
Choice class enables to create multiple item lists When a list is first created, it is empty Steps to create lists Create the list element Add items (Strings) to it, one by one Decide where to place it on the screen Display it on the screen Example
Choice colors=new Choice( ); [Link](Red); [Link](Green);
Copyright : MCC
Abstract Windowing ToolKits
16
Layout Manager
Different types of layouts Flow Layout Border Layout Card Layout Grid Layout GridBag Layout Layout manager is set with the method called setLayout( )
Abstract Windowing ToolKits
Copyright : MCC
17
FlowLayout
Is the default layout manager for applets and panels Components are arranged from the upper left corner to the bottom right corner Constructors
FlowLayout mylayout = new FlowLayout(); FlowLayout exLayout = new flowLayout([Link]);
Copyright : MCC
Abstract Windowing ToolKits
18
BorderLayout
Is the default layout manager for Window, Frame and Dialog Layout arranges up to five components in a container Components can be assigned to the NORTH, EAST, SOUTH, WEST and CENTER of the container Example: To add component to North region
Button b1= new Button(North Button); setLayout(new BorderLayout( )); add(b1, [Link]);
Copyright : MCC
Abstract Windowing ToolKits
19
CardLayout
Can store a stack of several layouts Each layout is like a card in a deck Card is usually a Panel object A separate component such as a button controls the card to be displayed on top Steps for creating card layout Set the layout of the main panel to CardLayout Add the other panels to the main panel
Abstract Windowing ToolKits
Copyright : MCC
20
GridLayout
Helps to divide the container into a grid Components are placed in rows and columns Each grid should contain at least one component Is used when all components are of the same size Constructor
GridLayout gl = new GridLayout(no. of rows, no. of columns);
Copyright : MCC
Abstract Windowing ToolKits
21
GridBagLayout
Places components precisely Components need not be of the same size Components are arranged in a grid of rows and columns Order of placing the components is not left-toright and top-to-bottom Constructor
GridBagLayout gb = new GridBagLayout( );
Copyright : MCC
Abstract Windowing ToolKits
22
GridBagLayout
To use this layout, information on the size and layout of each component is needed The class GridBagLayoutConstraints holds all the information that the class GridLayout requires to position and size each component
Copyright : MCC
Abstract Windowing ToolKits
23
Handling Events
Events are handled by Abstract Windowing Toolkit Browser Event handler written explicitly by programmers Application needs to register an event handler with an object Handler will be called whenever the appropriate event takes place for the right object Event Listener listens to a particular event that an object generates Each event listener provides methods handle these events
Abstract Windowing ToolKits
Copyright : MCC
24
Handling Events (Contd)
The class that implements the listener needs to define these methods Steps to follow to use the Event Listener model Implement the appropriate listener interface Identify all components that generate events Identify all the events to be handled Implement the methods of the listener, and write the event handling code within the methods Interfaces define several methods to handle each event, that have to be overridden in the class that implements these interfaces
Abstract Windowing ToolKits
Copyright : MCC
25
Events & respective Listeners
ActionEvent AdjustmentEvent ComponentEvent FocusEvent ItemEvent WindowEvent TextEvent MouseEvent KeyEvent
ActionListener AdjustmentListener ComponentListener FocusListener ItemListener WindowListener TextListener MouseListener MouseMotionListener KeyListener
26
Copyright : MCC
Abstract Windowing ToolKits
Menus
Types of menus Pull-down Pop-up menu Only one menubar can be placed inside a Frame Components of Menu Menubar MenuItems
Copyright : MCC
Abstract Windowing ToolKits
27