0% found this document useful (0 votes)
83 views8 pages

JFC (Swings/Awt) 1. What Is JFC..?: Java - Lang.Object - Java - Awt.Container - Javax - Swing.Jcomponent

1. JFC provides lightweight Java GUI components and includes Swing components, pluggable look and feel, accessibility API, Java 2D API, and internationalization support. 2. AWT components are platform dependent while Swing components are pure Java and platform independent. Swing components have a more efficient event model and can run more quickly than AWT. 3. All Swing components extend from the JComponent class.

Uploaded by

chakri406k
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views8 pages

JFC (Swings/Awt) 1. What Is JFC..?: Java - Lang.Object - Java - Awt.Container - Javax - Swing.Jcomponent

1. JFC provides lightweight Java GUI components and includes Swing components, pluggable look and feel, accessibility API, Java 2D API, and internationalization support. 2. AWT components are platform dependent while Swing components are pure Java and platform independent. Swing components have a more efficient event model and can run more quickly than AWT. 3. All Swing components extend from the JComponent class.

Uploaded by

chakri406k
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

JFC (Swings/AWT)

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

2. What is the difference between JFC Sing and AWT..?

AWT has following characteristics:


-It has native C specific code 
-Rendering of AWT controls is dependent upon underlying OS.
 JFC Swing is:
-Pure Java based
-Lightweight components
-Pluggable look and feel features
-Appearance of GUI developed using JFC Swing will be consistent across various OS.
-Swing uses a more efficient event model than AWT; therefore, Swing components can run more
quickly than their AWT counterparts.

3. What is the base class for all swing components?

JFC Swing components (like textfields,buttons,checkboxes,lists etc. ) are direct replacements of


corresponding AWT components and it replaces just some section of AWT GUI components, other
aspects of AWT like layout managers remains unchanged. All these Swing components are extended
from the javax.swing.JComponent, which has following hierarchy:
java.lang.Object
- java.awt.Component
- java.awt.Container
- javax.swing.JComponent

4. What are lightweight and heavyweight components?


In AWT, all GUI controls are referred as heavyweight components as they are dependent on underlying
OS (e.g. Windows, Solaris etc.) to provide it(paint it and redraw it).An AWT button control in MacOS is
actually a MacOS button.
All Swing components are lightweight components(except for the top-level ones: JWindow, JFrame,
JDialog, and JApplet) as they do not require underlying OS to provide them.JVM renders Swing
components and hence they are platform independent but they have performance related issues as
compared to heavyweight components which are faster to be rendered due to hardware acceleration.
The lightweight components have transparent pixels unlike heavyweights which have opaque pixels.
Mouse events on a lightweight component fall through to its parent; while on a heavyweight
component it does not. It is generally not recommended to mix heavyweight components with
lightweight components while building the GUI.

5. How can a GUI component handle its own events?

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.

7. How are the elements of a GridBagLayout organized?

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.

9. Where the CardLayout is used?


A CardLayout object is a layout manager for a container. It treats each component in the container as a
card. Only one card is visible at a time, and the container acts as a stack of cards. The first component
added to a CardLayout object is the visible component when the container is first displayed.
The ordering of cards is determined by the container's own internal ordering of its component objects.
CardLayout defines a set of methods that allow an application to flip through these cards sequentially,
or to show a specified card. The addLayoutComponent(java.awt.Component,java.lang.Object) method
can be used to associate a string identifier with a given card for fast random access.

10. What is the difference between GridLayout and GridBagLayout?

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.

11. How will you add a panel to a frame?

Here goes the snippet:


import javax.swing.*;
public class XYZ extends JFrame{
public XYZ(){
JPanel panel =new JPanel();
this.getContentPane().add(panel);
}
public static void main (String args[]){
XYZ obj=new XYZ();
obj.setVisible(true);

12. What is the difference between Application and Applet?

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.

14. What is the difference between Java class and bean?

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.

15. What is difference between trusted and untrusted applet?

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.

17. What are different container classes?

Window, Frame, Dialog, FileDialog, Panel, Applet, or ScrollPane

18. Which Container method is used to cause a container to be laid out and redisplayed?

validate()

19. Name Component subclasses that support painting.

The Canvas, Frame, Panel, and Applet classes support painting.

20. Which containers use a border layout as their default layout?

The following classes use BorderLayout as their default layout:


-Window
-Frame
-Dialog

21. Which containers use a FlowLayout as their default layout?

The classes in java.awt.* package: Panel and Applet


While in javax.swing.* package: JPanel and JApplet use FlowLayout as their default layout.

22.What is the purpose of class Canvas?

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.

You might also like