JFC SWINGAWT

download JFC SWINGAWT

of 8

Transcript of JFC SWINGAWT

  • 8/7/2019 JFC SWINGAWT

    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 thejavax.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?

  • 8/7/2019 JFC SWINGAWT

    2/8

    In AWT, all GUI controls are referred as heavyweightcomponents 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 lightweightcomponents(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 itsown 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 manyclasses.

    b) All events or a subset for an entire hierarchy for handling a particular container; results incontainer'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 whichimplements 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.

  • 8/7/2019 JFC SWINGAWT

    3/8

    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.ComponentAdapterjava.awt.event.ContainerAdapter

    java.awt.event.FocusAdapter

    java.awt.event.KeyAdapter

    java.awt.event.MouseAdapter

    java.awt.event.MouseMotionAdapter

    java.awt.event.WindowAdapter

  • 8/7/2019 JFC SWINGAWT

    4/8

  • 8/7/2019 JFC SWINGAWT

    5/8

    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?

    GridLayoutclass 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 GridBagLayoutclass is a flexible layout manager that aligns components vertically and horizontally,

    without requiring that the components be of the same size. EachGridBagLayoutobject maintains a

    dynamic, rectangular grid of cells, with each component occupying one or more cells, called its displayarea.

    Each component managed by a GridBagLayoutis associated with an instance ofGridBagConstraints. 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

    GridBagLayoutalso 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

  • 8/7/2019 JFC SWINGAWT

    6/8

    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 pagewith 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.

  • 8/7/2019 JFC SWINGAWT

    7/8

  • 8/7/2019 JFC SWINGAWT

    8/8

    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.