Event handling and Java Technology
How can I extend Java event handling code to suit
application specific event handling?
Ans:
There are four parts to event handling, one is listener,
second one is event object, the third one is the event
container and fourth one is the event propagator.
Extension point for first two parts are provided by
Java api as java.util.EventListener and java.util.EventObject
respectively. The third one (Event listener container) and
fourth one (event generator) has to be provided by programmer
Event generator should extend event listener container,
and every event generator/source should add event listeners
added to it, and calls event methods and passes event object
associated with this event listener.
The first one is the event listener as follows:
public interface DemoListener extends
java.util.EventListener
{
public void demoEvent(DemoEvent dm);
}
and second one the event object as follows:
public class DemoEvent extends java.util.EventObject
{
Object obj;
public DemoEvent(Object source)
{
super(source);
obj = source;
}
public Object getSource()
{
return obj;
}
......
}
The third part is the event container:
public class EventContainer
{
private Vector repository = new Vector();
DemoListener dl;
public EventContainer()
{
}
public void addDemoListener(DemoListener dl)
{
repository.addElement(dl);
}
public void notifyDemoEvent()
{
Enumeration enum = repository.elements();
while(enum.hasMoreElements())
{
dl = (DemoListener)enum.nextElement();
dl.demoEvent(new DemoEvent(this));
}
}
......
}
The fourth one is the event listener implementor, that
implements event listener and implements event handling
code/method.
EventContainer adds implemented DemoListener and based
on certain event, notifyDemoEvent methods executes
demoEvent method for all the demo listeners in the
repository inside EventContainer.
Event in Java[2065]
Ans:
An event is a state change in any object. E.g. when a button is clicked the click event of button occurs. Similarly load event of a page occurs when a HTML page loaded first time. The event may be of two type
a. Interactive event
b. Non-interactive
Type of Event[2060]
Ans:
Interactive event:-
Those events which are fixed due to user’s interaction with java controls. Eg. Click, key down, key up, mouse move, etc.
Non-interactive
Those events in which there is no need of user interaction for their occurrence. They are fixed automatically. E.g. load, unload, etc.
Event handler
The event Handler are method or routine which execute after event occurred. For each event there is one corresponding event handler. The responsibility of a event handler of a event is to perform specific assigned task after occurance of associated event.
e.g.
on click,
on change
on select
What is AWT?
Ans:
AWT stands for Abstract Window Toolkit. It is a portable GUI library between Solaris and Windows 95/NT. The Abstract Window Toolkit provides many classes for programmers to use. AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support AWT.
The AWT helps to create graphical component as textbox, Radio button, Command button, etc. which helps for data manipulation.
What are the AWT containers? What are the different types of layout managers? [2060,2062]. [4+4M]
Ans:
Applet
This subclass of Panel is actually part of the java.applet package. It is the base class for all applets.
Panel
A generic container used to create nested layouts.
Window
A heavyweight window with no titlebar or other decoration, suitable for pop-up menus and similar uses.
ScrollPane
A container that contains a single child and allows that child to be scrolled vertically and horizontally
Dialog
A window suitable for dialog boxes.
Frame
A window suitable for use as the main window of an application. In AWT, Frame is the only container that can contain a MenuBar and related menu components.
A layout Manager is an object that is used to organize components in a container. The different layouts available in java.awt are:
FlowLayout: The elements of a FlowLayout are organized in a top to bottom, left to right fashion.
BorderLayout: The elements of a BorderLayout are organized at the borders (North, South, East and West) and the center of a container.
CardLayout: The elements of a CardLayout are stacked, one on top of the other, like a deck of cards.
GridLayout: The elements of a GridLayout are of equal size and are laid out using the square of a grid.
GridBagLayout:
The elements of a GridBagLayout are organized according to a grid.However, the elements are of different sizes and may occupy more
than one row or column of the grid. In addition, the rows and columns may have different sizes.
What is interface? What are the major differences and similarities between interface and classes.
Ans:
An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. An interface can also include constant declarations. Interfaces are declared using the interface keyword. and may only contain method signatures and constant declarations. An interface may never contain method definitions.
Similarities
Classes and Interfaces are both Java files. They are saved as .java files with the name as the public class or public interface name.
Differences
The difference is that, classes are fully coded. That is, they have all the necessary variables and methods coded in them. But in case of Interfaces, only methods are declared. They are not defined. Interfaces are only like skeletons whereas classes can implement interfaces and complete the skeleton.
Interfaces are syntactically similar to classes, but they lack instance variables,and their methods are declared without any body.One class can implement any number of interfaces.Interfaces are designed to support dynamic method resolution at run time.