event hadling in java
event hadling in java
Graphical User Interface (GUI) offers user interaction via some graphical components. For example
our underlying Operating System also offers GUI via window,frame,Panel, Button, Textfield, TextArea,
Listbox, Combobox, Label, Checkbox etc. These all are known as components. Using these
components we can create an interactive user interface for an application.
GUI provides result to end user in response to raised events.GUI is entirely based events. For
example clicking over a button, closing a window, opening a window, typing something in a textarea
etc. These activities are known as events.GUI makes it easier for the end user to use an application. It
also makes them interesting.
Basic Terminologies
Term Description
Component is an object having a graphical representation that can be displayed on the screen
Componen
and that can interact with the user. For examples buttons, checkboxes, list and scrollbars of a
t
graphical user interface.
Panel provides space in which an application can attach any other components, including other
Panel
panels.
Window is a rectangular area which is displayed on the screen. In different window we can
execute different program and display different data. Window provide us with multitasking
Window
environment. A window must have either a frame, dialog, or another window defined as its
owner when it's constructed.
A Frame is a top-level window with a title and a border. The size of the frame includes any area
Frame designated for the border. Frame encapsulates window. It and has a title bar, menu bar,
borders, and resizing corners.
Canvas component represents a blank rectangular area of the screen onto which the
Canvas application can draw. Application can also trap input events from the use from that blank area
of Canvas component.
Layouts: They define how UI elements should be organized on the screen and provide a final
look and feel to the GUI (Graphical User Interface). This part will be covered in Layout
chapter.
Behavior: These are events which occur when the user interacts with UI elements. This part
will be covered in Event Handling chapter.
Sr.
Control & Description
No.
Component
1 A Component is an abstract super class for GUI controls and it represents an object with graphical
representation.
AWT UI Elements:
Following is the list of commonly used controls while designed GUI using AWT.
Sr.
Control & Description
No.
Label
1
A Label object is a component for placing text in a container.
2 Button
This class creates a labeled button.
Check Box
3
A check box is a graphical component that can be in either an on (true) or off (false) state.
List
5
The List component presents the user with a scrolling list of text items.
Text Field
6
A TextField object is a text component that allows for the editing of a single line of text.
Text Area
7
A TextArea object is a text component that allows for the editing of a multiple lines of text.
Choice
8 A Choice control is used to show pop up menu of choices. Selected choice is shown on the top of the
menu.
Canvas
9 A Canvas control represents a rectangular area where application can draw something or can receive
inputs created by user.
Image
10
An Image control is superclass for all image classes representing graphical images.
Scroll Bar
11 A Scrollbar control represents a scroll bar component in order to enable user to select from range of
values.
Dialog
12 A Dialog control represents a top-level window with a title and a border used to take some form of
input from the user.
13 File Dialog
A FileDialog control represents a dialog window from which the user can select a file.
Label is a passive control because it does not create any event when accessed by the user. The label
control is an object of Label. A label displays a single line of read-only text. However the text can be
changed by the application programmer but cannot be changed by the end user in any way.
Class declaration
extends Component
implements Accessible
Field
static int LEFT -- Indicates that the label should be left justified.
static int RIGHT -- Indicates that the label should be right justified
Constructor &
Description
Label()
1
Constructs an empty label.
Label(String text)
2
Constructs a new label with the specified string of text, left justified.
Class constructors
Constructor &
Description
Label()
1
Constructs an empty label.
Label(String text)
2
Constructs a new label with the specified string of text, left justified.
Class methods
void addNotify()
1
Creates the peer for this label.
AccessibleContext getAccessibleContext()
2
Gets the AccessibleContext associated with this Label.
int getAlignment()
3
Gets the current alignment of this label.
String getText()
4
Gets the text of this label.
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.
Class declaration
extends Component
implements Accessible
Class constructors
Button()
1
Constructs a button with an empty string for its label.
Button(String text)
2
Constructs a new button with specified label.
Class methods
void addActionListener(ActionListener l)
1
Adds the specified action listener to receive action events from this button.
2 void addNotify()
Creates the peer of the button.
AccessibleContext getAccessibleContext()
3
Gets the AccessibleContext associated with this Button.
String getActionCommand()
4
Returns the command name of the action event fired by this button.
ActionListener[] getActionListeners()
5
Returns an array of all the action listeners registered on this button.
String getLabel()
6
Gets the label of this button.
void removeActionListener(ActionListener l)
11
Removes the specified action listener so that it no longer receives action events from this button.
Checkbox control is used to turn an option on(true) or off(false). There is label for each checkbox
representing what the checkbox does.The state of a checkbox can be changed by clicking on it.
Class declaration
extends Component
implements ItemSelectable,Accessible
Class constructors
Checkbox()
1
Creates a check box with an empty string for its label.
Checkbox(String label)
2
Creates a check box with the specified label.
Class methods
1 void addItemListener(ItemListener l)
Adds the specified item listener to receive item events from this check box.
void addNotify()
2
Creates the peer of the Checkbox.
AccessibleContext getAccessibleContext()
3
Gets the AccessibleContext associated with this Checkbox.
CheckboxGroup getCheckboxGroup()
4
Determines this check box's group.
ItemListener[] getItemListeners()
5
Returns an array of all the item listeners registered on this checkbox.
String getLabel()
6
Gets the label of this check box.
Object[] getSelectedObjects()
8
Returns an array (length 1) containing the checkbox label or null if the checkbox is not selected.
boolean getState()
9
Determines whether this check box is in the on or off state.
void setCheckboxGroup(CheckboxGroup g)
14
Sets this check box's group to the specified check box group.