Wednesday, September 26, 2007

AWT and Swing FAQs

What does AWT stand for?
AWT stands for Abstract Window ToolKit. AWT enables programmers to develop Java applications with GUI components, such as windows, and buttons.

Pointers regarding Swing.
•100% Java implementation of components
•Pluggable Look & Feel
•Lightweight components
•Uses Model View Control (MVC) Architecture

What is the difference between AWT and Swing?
AWT is heavy-weight components, but Swing is light-weight components. AWT is OS dependent because it uses native components, but Swing components are OS independent. We can change the look and feel in Swing which is not possible in AWT. Swing takes less memory compared to AWT. For drawing AWT uses screen rendering where Swing uses double buffering.

What is a “heavyweight” component?
A heavyweight component is one that is associated with its own native screen resource (commonly known as a peer).

What is a “lightweight” component?
A lightweight component is one that “borrows” the native screen resources of an ancestor (which means it has no native resources of its own – peerless).

Just what is a button really?
It is simply a wrapper class inheriting from JComponent that holds the DefaultButtonModel object, some view data (such as the button label and icons), and a BasicButtonUI object that is responsible for the button views.

Why won’t the JVM terminate when all application windows are closed?
The AWT event dispatcher thread is not a daemon thread. You must explicitly call System.exit() to terminate the JVM.

Which Swing methods are thread-safe?
The only thread-safe methods are repaint(), revalidate(), and invalidate().

What class is at the top of the AWT event hierarchy?
java.awt.AWTEvent.

What does “heavy weight components” mean?
Heavy weight components like Abstract Window Toolkit (AWT) depend on the local windowing toolkit. For example, java.awt.Button is a heavy weight component.

Name the container classes which use Border Layout as their default layout?
Window, Frame and Dialog classes.

Name Container classes.
Window, Frame, Dialog, FileDialog, Panel, Applet, and ScrollPane.

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.

Which package has light weight components?
javax.Swing package contains light weight components. All components in Swing, except JApplet, JDialog, JFrame and JWindow are lightweight components.

What is JFC?
JFC stands for Java Foundation Classes. The Java Foundation Classes (JFC) are a set of Java class libraries provided as part of Java 2 Platform, Standard Edition (J2SE) to support building graphics user interface (GUI) and graphics functionality for client applications that will run on popular platforms such as Microsoft Windows, Linux, and Mac OSX.

What is an event?
Changing the state of an object is an event.

What is an Event Handler?
An Event Handler is part of a program which tells the program how to act in response to a specific event.

What is a layout manager?
A layout manager is an object used to organize components in a container.

Which container classes use BorderLayout manager as their default layout?
Window, Frame and Dialog classes.

Which container classes use FlowLayout manager as their default layout?
Panel and Applet classes.

What method of the Container class is used to set the position and size of the component?
The setBounds() method.

What method is used to specify a container's layout?
The setLayout() method is used to specify a container's layout. For example, setLayout(new FlowLayout()); will set the layout as FlowLayout.

How are the elements of different layouts organized?
A layout manager is an object that is used to organize components in a container. The different layouts available are FlowLayout, BorderLayout, CardLayout, GridLayout and GridBagLayout.

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, 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 may be different sizes and may occupy more than one row or column of the grid. In addition, the rows and columns may have different sizes.

Which Container method is used to cause a container to be laid out and redisplayed?
validate()

Name Component subclasses that support painting.
The Canvas, Frame, Panel, and Applet classes support painting.

What is the difference between a Scrollbar and a ScrollPane?
A Scrollbar is just a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.

What is the preferred size of a component?
The preferred size of a component is the minimum component size that will allow the component to display normally.

How can a GUI component handle its own events?
A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener.

What are peerless components?
The peerless components are called light-weight components.

What is the purpose of the enableEvents() method?
The enableEvents() method is used to enable an event for a particular component. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents() method is used by objects that handle events by overriding their event-dispatch methods.

No comments: