JFC (Swings/Awt) 1. What Is JFC..?: Java - Lang.Object - Java - Awt.Container - Javax - Swing.Jcomponent
JFC (Swings/Awt) 1. What Is JFC..?: Java - Lang.Object - Java - Awt.Container - Javax - Swing.Jcomponent
1. What is JFC..?
Java Foundation Classes (JFC) offers lightweight Java language specific GUI controls.JFC consists of group
of classes for building graphical user interfaces (GUIs) and adding rich graphics functionality and
interactivity to Java applications.
JFC supports: -Swing GUI Components
-Pluggable Look-and-Feel Support
-Accessibility API
-Java 2D API
-Internationalization
A GUI component handles its events by implementing the required event listener interface.It adds its
own event listener in order to keep a track of all events associated with it.AWT has Java1.0 and Java1.1
of Event handling in different manners:
In Java1.0:Event handling is based on inheritance. A program catches and processes GUI events by
subclassing GUI components and override either action() or handleEvent() methods. There are two
possibilities in this scenario:
a) Each component is subclassed to specifically handle its target events. The results in too many
classes.
b) All events or a subset for an entire hierarchy for handling a particular container; results in
container's overridden action() or handleEvent() method and complex conditional statement for
events processing.
The event handling in Java 1.0 had issues like cumbersome to handle by developers,flitering of events
was not too efficient as it was done in a single method handleEvent().
In Java 1.1 these issues are resolved through delegation based event model.The GUI code can be
seprated from event handling code which is cleaner,flexible,easier to maintanin and robust.In delegation
event model,java.util.EventObject is the root class for event handling.An event is propagated from
"Source" object(responsible for firing the event) to "Listener" object by invoking a method on the
listener and passing in the instance of the event subclass which defines the event type generated.
A listener is commonly an "adapter" object which implements the appropriate listener/(s) for an
application to handl of events. The listener object could also be another AWT component which
implements one or more listener interfaces for the purpose of hooking GUI objects up to each
other.There can be following types of events:
A low-level event represents a low-level input or window-system occurrence on a visual component on
the screen.
The semantic events are defined at a higher-level to encapsulate the semantics of a UI component's
model.
The low event class hierarchy:
-java.util.EventObject
-java.awt.AWTEvent
-java.awt.event.ComponentEvent (component resized, moved, etc.)
java.awt.event.FocusEvent (component got focus, lost focus)
-java.awt.event.InputEvent
-java.awt.event.KeyEvent (component got key-press, key-release, etc.)
-java.awt.event.MouseEvent (component got mouse-down, mouse-move, etc.)
-java.awt.event.ContainerEvent
-java.awt.event.WindowEvent
The semantics event class hierarchy:
-java.util.EventObject
-java.awt.AWTEvent
-java.awt.event.ActionEvent ("do a command")
-java.awt.event.AdjustmentEvent ("value was adjusted")
-java.awt.event.ItemEvent ("item state has changed")
-java.awt.event.TextEvent ("the value of the text object changed")
The low-level listener interfaces in AWT are as follows:
java.util.EventListener
- java.awt.event.ComponentListener
- java.awt.event.ContainerListener
- java.awt.event.FocusListener
- java.awt.event.KeyListener
- java.awt.event.MouseListener
- java.awt.event.MouseMotionListener
- java.awt.event.WindowListener
The semantic listener interfaces in AWT are as follows:
java.util.EventListener
-java.awt.event.ActionListener
-java.awt.event.AdjustmentListener
-java.awt.event.ItemListener
-java.awt.event.TextListener
There are following Adapter classes in AWT :
java.awt.event.ComponentAdapter
java.awt.event.ContainerAdapter
java.awt.event.FocusAdapter
java.awt.event.KeyAdapter
java.awt.event.MouseAdapter
java.awt.event.MouseMotionAdapter
java.awt.event.WindowAdapter
6. What is a Layout Manager and what are its different types and their advantages?
In Java,a GUI component does not decide about its geometry(location and size) on its own.A layout
manager is an object which is responsible for managing or arranging the size and location of a GUI
component.In AWT there are following layout managers which are supported:
-Flow
-Border
-GridBag
-Card
-GridBag
These layout managers organize components consistently across all windowing platforms. Irrespective
of underlying windowing OS,layouts behave in a regular fashion and show GUI components.A layout
manager also represents an instance of a class which implements LayoutManager interface.The layout
manager is set by setLayout(LayoutManager layoutManager) method.
A GridBagLayout organizes/arranges all GUI controls to a grid. However, these controls are of different
sizes and may occupy more than one row or column of the grid. These rows and columns may have
different sizes as well.It is by far most powerful layout manager and requires good practice and
understanding to use.It can combine the features of Border,Flow and Card layouts and capable of much
more.
GridBag layout divides its container into an array of cells, different cell rows can have different heights,
and different cell columns can have different widths. A component can occupy part or all of a
region.While a region is spanned over a single cell or a rectangle made up of multiple cells.A helper class
called GridBagConstraints is used to hold all the layout position information.The add(Component,
Object) version of the add() method is used for adding a control, passing an instance of
GridBagConstraints as the Object parameter.
8. What are the problems faced by Java programmers in absence of layout managers?
If relevant layout managers are not used while designing a GUI then GUI controls will have
haphazard/inconsistent display across multiple windowing systems.These GUI controls will neglect their
common sizing and positioning that ideally should be same across various windowing systems.
In order to counter this issue, an appropriate layout which is applicable for container object, must be
chosen.
GridLayout class lays all components in a rectangular grid like structure of container. The container is
divided into an equal sized rectangles and each component is placed inside a rectangle.
The GridBagLayout class is a flexible layout manager that aligns components vertically and horizontally,
without requiring that the components be of the same size. Each GridBagLayout object maintains a
dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display
area.
Each component managed by a GridBagLayout is associated with an instance of GridBagConstraints. The
constraints object specifies where a component's display area should be located on the grid and how
the component should be positioned within its display area. In addition to its constraints object, the
GridBagLayout also considers each component's minimum and preferred sizes in order to determine a
component's size.
An applet runs in client side web browser. A class extending java.awt.Applet class which has methods
like init(), start(), stop(), destroy(),paint() overridden.An applet has restriction of accessing client side
resources like network connections, it cannot open socket connections and cannot write to client side
files i.e. hard disk.
An application runs standalone with a support of virtual machine. An application does not have nay
restrictions as Applets have over network and file related activities.They are free to open sockets over a
network read and write to a file.
13. Explain Lifecycle of the Applet and what is the order of method invocation in an applet?
An applet is built up of four methods:init,start,stop and destroy.First of all an instance of applet subclass
will be created and then applet will be initialized.Swing provides a special subclass of Applet, called
javax.swing.JApplet, which should be used for all applets that use Swing components to construct their
GUIs.
Here is a sequence of method calls in an applet :
-init():An applet can initialize itself and does whatever initializations are required to do for an applet.
-start(): This method is automatically called when applet is initialized.When a user comes back to a page
with an applet this method is invoked then too.
-stop(): This method is called when user moves away from the webpage containing applet
-destroy:It is responsible for clean up and is called when browser is shut down normally.
An applet can be initialized and destroyed only once in its lifetime but it can be started and stopped
several times.
What differentiates Beans from typical Java classes is introspection. The tools that recognize predefined
patterns in method signatures and class definitions can "look inside" a Bean to determine its properties
and behavior. A Bean's state can be manipulated at the time it is being assembled as a part within a
larger application. The application assembly is referred to as design time in contrast to run time. In order
for this scheme to work, method signatures within Beans must follow a certain pattern in order for
introspection tools to recognize how Beans can be manipulated, both at design time, and run time.
A trusted applet is one which signed by a trusted authority. The trusted applet is installed on the local
hard disk, in a directory on the CLASSPATH used by the program that you are using to run the applet.
Usually, this is a Java-enabled browser, but it could be the appletviewer, or other Java programs that
know how to load applets.
The applet is signed by an identity marked as trusted in your identity database.By default all applets
downloaded in client browser are untrusted.
-They cannot read or write files to clients' local file system at all.
-They cannot start a program at client's machine.
-They cannot do network operations i.e. cannot do Socket programming.
-They cannot load libraries and use native codes.
16. What is the difference between the paint() and repaint() methods?
The paint() method supports painting via a Graphics object. The repaint() method is used to cause
paint() to be invoked by the AWT painting thread.
18. Which Container method is used to cause a container to be laid out and redisplayed?
validate()
A Canvas object represents a blank,semantics free window upon which you can draw.It is not part of the
hierarchy of applet or frame window.It has general GUI component for drawing images and text on the
screen. It does not support any drawing methods on its own, but provides access to a Graphics object
through its paint() method. Whenever paint() is invoked during creation and update of a canvas , the
Graphics object associated with a Canvas object gets updated.
23.Difference between Canvas class and Graphics class?
A Canvas class is a blank,semantics free window upon which you can draw while Graphics class
encapsulates the graphics context. This context is used by the various output methods to display output
in a window.