Java Swing Interview Questions

  1. What is the difference between AWT and Swing ?
  2. Explain the Swing Action architecture
  3. If you add a component to the CENTER of a border layout, which directions will the component stretch?
  4. Explain Swing hierarchy
  5. Explain the Swing event dispatcher mechanism
  6. What do you understand by MVC as used in a JTable ?
  7. Explain layout managers
  8. Explain the Swing delegation event model

Fast Track - Java Swing Interview

What is Java Swing ?

Swing is a GUI toolkit for Java. It is one part of the Java Foundation Classes (JFC). Swing includes graphical user interface (GUI) widgets such as text boxes, buttons, split-panes, and tables.

Swing widgets provide more sophisticated GUI components than the earlier Abstract Window Toolkit. Since they are written in pure Java, they run the same on all platforms, unlike the AWT which is tied to the underlying platform's windowing system. Swing supports pluggable look and feel – not by using the native platform's facilities, but by roughly emulating them. This means you can get any supported look and feel on any platform. The disadvantage of lightweight components is slower execution. The advantage is uniform behavior on all platforms.

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 AWT ?

AWT stands for Abstract Window Toolkit. AWT enables programmers to develop Java applications with GUI components, such as windows, and buttons. The Java Virtual Machine (JVM) is responsible for translating the AWT calls into the appropriate calls to the host operating system.

Difference between Swing and AWT



Light-weight component

Heavy-weight component

Does not depend on the OS

Depends on the OS

Does not use native API

Uses native API

Uses double buffering for drawing

Uses screen rendering for drawing

Uses less memory

Uses more memory when compared to swing

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 are Heavy-weight components ?

A heavyweight component is one that is associated with its own native screen resource, i.e associated with the OS (commonly known as a peer).

What are peerless components?

The peerless components are called light weight components.

What is double buffering ?

One of the most notable features of Swing is that it builds support for double-buffering right into the toolkit. It does the by providing a "doubleBuffered" property on javax.swing.JComponent:

     public boolean isDoubleBuffered()     public void setDoubleBuffered(boolean o)

By default, this property is set to true for all Swing components.

Double buffering is the process of use of two buffers rather than one to temporarily hold data being moved to and from an I/O device. Double buffering increases data transfer speed because one buffer can be filled while the other is being emptied.

The default paint(Graphics g) implemented in JPanel will perform double buffering and call your paintComponent(Graphics g) when necessary.

What is a Container in a GUI?

A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT components.

Components added to a container are tracked in a list. The order of the list will define the components' front-to-back stacking order within the container. If no index is specified when adding a component to a container, it will be added to the end of the list (and hence to the bottom of the stacking order).

Name Container classes.

Window(Frame, Dialog), Panel, ScrollPane, Jcomponent

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

What is preferred size of a component?

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

What is a layout manager and what are different types of layout managers available in java Swing? 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.

Name the containers which use Border Layout as their default layout?

Window, Frame and Dialog classes.

Which containers use a FlowLayout as their default layout?

The Panel and Applet classes use the FlowLayout as their default layout.

What advantage do Java’s layout managers provide over traditional windowing systems?

Java uses layout managers to lay out components in a consistent manner across all windowing platforms. Since Java’s layout managers aren’t tied to absolute sizing and positioning, they are able to accommodate platform-specific differences among windowing systems.

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 be set the layout as FlowLayout.

Which method of the Component class is used to set the position and size of a component?

setBounds() Method of the Component class is used to set the position and size of a component.

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

validate() Validates this container and all of its subcomponents.

The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

Name Component subclasses that support painting.

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

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.

What is the difference between a Scrollbar and a ScrollPane?

A JScrollPane basically consists of JScrollBars, a JViewport, and the wiring between them.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.

What are JinternalFrames ?

Internal frames are not windows or top-level containers, however, which makes them different from frames. For example, you must add an internal frame to a container (usually a JDesktopPane); an internal frame cannot be the root of a containment hierarchy. Also, internal frames do not generate window events. Instead, the user actions that would cause a frame to fire window events cause an internal frame to fire internal frame events.

What are Layered Panes ?

A layered pane is a Swing container that provides a third dimension for positioning components: depth, also known as Z order. When adding a component to a layered pane, you specify its depth as an integer. The higher the number, closer the component is to the "top" position within the container. If components overlap, the "closer" components are drawn on top of components at a lower depth. The relationship between components at the same depth is determined by their positions within the depth.

What is an event and what are the models available for event handling?

Changing the state of an object is called an event. An event is an event object that describes a state of change. In other words, event occurs when an action is generated, like pressing a key on keyboard, clicking mouse, etc. There different types of models for handling events are event-inheritance model and event-delegation model

What are the advantages of the event-delegation model over the event-inheritance model

Event-delegation model has two advantages over event-inheritance model. a)Event delegation model enables event handling by objects other than the ones that generate the events. This allows a clean separation between a component's design and its use. b)It performs much better in applications where many events are generated. This performance improvement is due to event-delegation model does not have to be repeatedly process unhandled events as is the case of the event-inheritance.


  1. useful QA's..

  2. I am really enjoying the theme/design of your weblog. Do you
    ever run into any internet browser compatibility issues?
    A small number of my blog visitors have complained about
    my site not working correctly in Explorer but looks great in Safari.
    Do you have any solutions to help fix this

    my blog -