Professional Documents
Culture Documents
Without Core Java no one can Where as advanced java only deals
with some specialization like
develop any advanced java
applications. Database, DOM(web), networking
etc.
OOP, data types, operators, Apart from the core java parts it
has some specific sections like
functions, loops, exception
handling, threading etc. database connectivity, web
services, servlets etc.
It uses only one tier architecture It uses two tier architecture i.e.
that is why it is called as ‘stand client side architecture and server
alone’ application. side or backend architecture.
AWT(Abstrct Window Tollkit)
Window
The window is the container that have no borders and
menu bars. You must use frame, dialog or another
window for creating a window.
Panel
The Panel is the container that doesn't contain title
bar and menu bars. It can have other components like
button, textfield etc.
Frame
The Frame is the container that contain title bar and
can have menu bars. It can have other components like
button, textfield etc.
Control Fundamentals
Controls are componenets that allow a user to interact with your
application in various ways.
The AWT supports the following types of controls:
■ Labels
■ Push buttons
■ Check boxes
■ Choice lists
■ Lists
■ Scroll bars
■ Text Area
■ Text Field
These controls are subclasses of Component.
Adding and Removing controls
In order to include a control in a window, you must add it to the
window. So, you must first create an instance of the desired control
and then add it to a window by calling add( ), which is defined by
Container.
The add( ) method has several forms. The following form is the one
That is used for the first part of this chapter:
Component add(Component compObj)
Here, compObj is an instance of the control that we want to add. A
reference to compObj is returned. Once a control has been added, it
will automatically be visible whenever its parent window is displayed.
Sometimes we will want to remove a control from a window
when the control is no longer needed. To do this, call
remove( ). This method is also defined by Container. It has
this general form:
void remove(Component obj)
Here, obj is a reference to the control that we want to
remove. We can
remove all controls by calling removeAll( ).
Responding to Controls
Except for labels, which are passive controls, all
controls generate events when they are accessed by
the user. For example, when the user clicks on a
push button, an event is sent that identifies the
push button. In general, our
program simply implements the appropriate
interface and then registers an event listener for
each control that we need to monitor.
Label:
The easiest control to use is a label. A label is an
object of type Label,and it contains a string, which
it displays. Labels are passive controls that do not
support any interaction with the user. Label
defines the following
constructors:
Label( )
Label(String str)
Label(String str, int how)
The first version creates a blank label.
The second version creates a label that contains the
string specified by str.
This string is left-justified.
Constructors:
EventObject(Object src)
‘src’ is object that generates event.
Event class hierarchy
Methods:
getSource()-returns the source of the event.
toString()-returns string equivalent of event.
NOTE:
AWT Event is the subclass of EventObject. It is in
java.awt package.It is the super class of all AWT-
based events used by delegation event model..
The ComponentEvent class is super class of
FocusEvent,InputEvent,WindowEvent.
delegation Event model
The modern approach to handling events is based on the
delegation event model. In this model, an event is sent
to component form which it originated. The component
registers a listener object with the program. The listener
processes the event and then returns.
For e.g.: When you click a button, the action to be
performed is handled by an object registered to handle
the button click event. The following diagram illustrates
the delegation event model
every event has a corresponding listener interface
that specifies the methods that are required to
handle the event. Event object are sent to
registered listeners. To enable a component to
handle events, you must register an appropriate
listener for it.
Event Handling:
• Applets are event-driven programs.
• Use graphical user interface to interact with the
user.
• Thus, event handling is at the fundamental of
successful applet programming.
• Events are supported by number of pkgs.
• Ex java.util,
• java.awt,
• java.awt.event.
• Two Event Handling Mechanisms:
• Imp pts. must be made: The way in which events
are handled changed significantly between
original versions of java(1.0) and morden versions
of java, beginning with version 1.1
• The 1.0 method of event handling is still
supported, but it is not recommended for new
programs. Also many of the methods that support
the old 1.0 event model have been
deprecated(Evaluated).
Delegation Event Model:
The modern approach to handling events is based
on the delegation event model, which defines
standard and consistent mechanisms to
generate and process events.
Its concept is quite simple: a source generates an
event and sends it to one or more listeners.
In this scheme, the listener simply waits until
it receives an event. Once received, the listener
processes the event and then returns.
The advantage of this design is that the
application logic that processes events is cleanly
separated from the user interface logic that
generates those events.
In the delegation event model, listeners must register
with a source in order to receive an event notification.
This provides an important benefit: notifications are
sent only to listeners that want to receive them.
This is a more efficient way to handle events than the
design used by the old Java 1.0 approach.
Previously an event was propagated up the
containment hierarchy until it was handled by a
component.
This required components to receive events that they
did not process, and it wasted valuable time. The
delegation event model eliminates this overhead.
Event Handling Model of AWT
Event object
Event handling
methods
InputEvent Abstract super class for all component input event classes.
ItemEvent Generated when a check box or list item is clicked; also
occurs when a choice selection is made or a checkable menu
item is selected or deselected.
KeyEvent Generated when input is received from the keyboard.
MouseEvent Generated when the mouse is dragged, moved,
clicked,
pressed, or released; also generated when the mouse
enters
or exits a component.
Component getComponent()
Container Event
There are three types of key events ,which are identified by these
integer Constants:
KEY_PRESSED
KEY_RELEASED
KEY_TYPED:
event occurs when a character is generated, not all key presses result in
character.(Ex pressing SHIFT does not generate a character)
Methods:
Constants:
TEXT_VALUE_CHANGED
Mouse Event class
Generated when the mouse is dragged, moved, clicked,
pressed or released,also generated when mouse enters or
exits a component.
Constructor:
MouseEvent(Component src,int type,long when,int
modifier,int x,int y,int clicks,Boolean triggersPopup)
AdjustmentListener Interface
This interface defines the adjustmentValueChanged( )
method that is invoked when an adjustment event occurs.
Its general form is shown here:
void adjustmentValueChanged(AdjustmentEvent ae)
ComponentListener Interface
This interface defines four methods that are invoked
when a component is resized, moved, shown, or
hidden. Their general forms are shown here:
void componentResized(ComponentEvent ce)
void componentMoved(ComponentEvent ce)
void componentShown(ComponentEvent ce)
void componentHidden(ComponentEvent ce)
The AWT processes the resize and move events.
The componentResized()
and componentMoved(
ContainerListener Interface
This interface contains two methods. When a
component is added to a
container, componentAdded( ) is invoked. When a
component is removed from
a container, componentRemoved( ) is invoked. Their
general forms are shown
here:
void componentAdded(ContainerEvent ce)
void componentRemoved(ContainerEvent ce)
FocusListener Interface
This interface defines two methods. When a component
obtains keyboard focus, focusGained( ) is invoked. When a
component loses keyboard focus,focusLost( ) is called.
Their general forms are shown here:
void focusGained(FocusEvent fe)
void focusLost(FocusEvent fe)
ItemListener Interface
This interface defines the itemStateChanged( ) method that is
invoked
when the state of an item changes. Its general form is shown
here:
void itemStateChanged(ItemEvent ie)
KeyListener Interface
This interface defines three methods. The keyPressed( ) and
keyReleased( ) methods are invoked when a key is pressed and released,
respectively. The keyTyped( ) method is invoked when a character has
beenentered.
For example, if a user presses and releases the A key, three events
are generated in sequence: key pressed, typed, and released. If a user
presses
and releases the HOME key, two key events are generated in sequence:
key
pressed and released. The general forms of these methods are shown
here:
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
MouseListener Interface
This interface defines five methods. If the mouse is pressed and
released at the same point, mouseClicked( ) is invoked. When the
mouse enters acomponent, the mouseEntered( ) method is called.
When it leaves,
mouseExited( ) is called. The mousePressed( ) and
mouseReleased( ) methods
are invoked when the mouse is pressed and released, respectively.
The general
forms of these methods are shown here:
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
MouseMotionListener Interface
This interface defines two methods. The
mouseDragged( ) method is
called multiple times as the mouse is dragged. The
mouseMoved( ) method is
called multiple times as the mouse is moved. Their
general forms are shown
here:
void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)
TextListener Interface
This interface defines the textChanged( ) method that
is invoked when a
change occurs in a text area or text field. Its general
form is shown here:
void textChanged(TextEvent te)