Applet,AWT,Swing
Interview Questions - Part1
1)How
will you initialize an Applet ?
Write my initialization code in the applets
init method or applet constructor.
2)What is the order of method invocation
in an Applet ?
-
public void init() : Initialization method called once by browser.
-
public void start() : Method called after init() and contains code to
start processing. If the user leaves the page and returns without killing
the current browser session, the start () method is called without being
preceded by init ().
-
public void stop() : Stops all processing started by start (). Done
if user moves off page.
-
public void destroy() : Called if current browser session is being
terminated. Frees all resources used by applet.
-
3)When is update method called ?
Whenever a screen needs redrawing (e.g.,
upon creation, resizing, validating) the update method is called. By default,
the update method clears the screen and then calls the paint method, which
normally contains all the drawing code.
4)How will you communicate between two
Applets ?
The simplest method is to use the static
variables of a shared class since there's only one instance of the class and
hence only one copy of its static variables. A slightly more reliable method
relies on the fact that all the applets on a given page share the same
AppletContext. We obtain this applet context as follows:
code:
AppletContext ac = getAppletContext();
AppletContext provides applets with methods
such as getApplet(name), getApplets(),getAudioClip, getImage, showDocument and
showStatus().
5)How do you communicate in between
Applets & Servlets ?
We can use the java.net.URLConnection and
java.net.URL classes to open a standard HTTP connection and "tunnel" to the
web server. The server then passes this information to the servlet in the
normal way. Basically, the applet pretends to be a web browser, and the
servlet doesn't know the difference. As far as the servlet is concerned, the
applet is just another HTTP client.
6)What is the base class for all swing
components ?
JComponent (except top-level
containers)
7)What is JFC ?
Java Foundation Classes include:
-
Standard AWT 1.1
-
Accessibility interface
-
Lightweight components: which are user interface components that do not
subclass an existing AWT interface element. They do not use native interface
elements as provided by the underlying windowing system. This means that
they are less limiting than standard AWT components.
-
Java look and feel
-
Support for native look and feel
-
Services such as Java2D and Drag and Drop
-
8)What is Difference between AWT and Swing
?
Swing provides a richer set of components
than AWT. They are 100% Java-based. AWT on the other hand was developed with
the mind set that if a component or capability of a component weren’t
available on one platform, it wouldn’t be available on any platform. Due to
the peer-based nature of AWT, what might work on one implementation might not
work on another, as the peer-integration might not be as robust. There are a
few other advantages to Swing over AWT:
-
Swing provides both additional components and added functionality to
AWT-replacement components
-
Swing components can change their appearance based on the current "look and
feel" library that's being used.
-
Swing components follow the Model-View-Controller (MVC) paradigm, and thus
can provide a much more flexible UI.
-
Swing provides "extras" for components, such as:
-
Icons on many components
-
Decorative borders for components
-
Tool tips for components
-
Swing components are lightweight (less resource intensive than AWT)
-
Swing provides built-in double buffering
-
Swing provides paint debugging support for when you build your own
components
Swing also has a few disadvantages:
-
It requires Java 2 or a separate JAR file
-
If you're not very careful when programming, it can be slower than AWT (all
components are drawn)
-
Swing components that look like native components might not act exactly like
native components
9)How will you call an Applet using a Java
Script function ?
Like this:
code:
document.appletName.methodCall(...)
Does not work with IE
though
10)Why do you Canvas ?
The Canvas class of java.awt is used to provide custom drawing and event
handling. It provides a general GUI component for drawing images and text on the
screen. It does not support any drawing methods of its own, but provides access
to a Graphics object through its paint() method. The paint() method is invoked
upon the creation and update of a canvas so that the Graphics object associated
with a Canvas object can be updated.
|