Swing Interview Questions

1 - What are the differences between Swing and AWT?
AWT [Abstract Windowing ToolKit]
A rich set of user interface components. All the features of AWT.
A robust event-handling model. 100% Pure Java certified versions of the existing AWT component set (Button, Scrollbar, Label, etc.).
Graphics and imaging tools, including shape, color, and font classes. A rich set of higher-level components (such as tree view, list box, and tabbed panes).
Layout managers, for flexible window layouts that don't depend on a particular window size or screen resolution. Pure Java design, no reliance on peers.
Data transfer classes, for cut-and-paste through the native platform clipboard. Pluggable Look and Feel.

2 - What are the differences between Swing and JSF?
JSF runs on server like Tomcat or WebLogic, glassfish and WebSphere. It displays HTML to the client. But Swing program is a java stand alone program.
3 - Why Swing components are called lightweight component?
Swing is a light weight component because swing implementing the look-and-feel of the component in Java rather than delegating the look-and-feel to a native peer. Lightweight components can be more efficient at utilizing system resource, they can be transparent.
4 - What do you understand by MVC as used in a JTable?
AS JTable is use to show data in table form, MVC [model view controller] separates a model (or database) from a presentation and the logic that manages it. In MVC JTable is a view, controller is a class that control the input from view and control output from database or data source.
5 - Explain the Swing event dispatcher mechanism?
The event-dispatchingthread is a thread that executes drawing of components and eventhandling code. Swing components should be accessed through this event dispatching thread. The paint() and actionPerformed() methods are automatically executed in the eventdispatching thread. Another way to execute code in the event-dispatching thread from outside event-handling or drawing code, is using SwingUtilities invokeLater() or invokeAndWait() method.
6 - Explain the Swing delegation event model?
In the event-delegation model, specific objects are designated as event handlers for GUI components. These objects implement event-listener interfaces.
  • It enables the handling of events by objects
  • They perform better where many events are generated in application.

7 - What is Canvas?
Canvas is a Component subclass used for drawing and painting.
8 - What is the difference between a Canvas and a ScrollPane?
Canvas is a Component subclass has a rectangular area where the application can draw or paint. ScrollPane is a container that implements horizontal and vertical scrolling.
9 - Explain layout managers?
Layout Managers allow you to arrange GUI components on a container for presentation purposes. There are several layout manager:
  • BorderLayout
  • BoxLayout
  • CardLayout
  • FlowLayout
  • GridBagLayout
  • GridLayout

10 - What is JFC?
JFC [Java Foundation Classes] is a set of java class libraries provided in J2SE to support to building GUI for client applications.
11 - Which Swing methods are thread-safe?
repaint(), revalidate(), and invalidate() methods of swing are thread-safe.
12 - Can a class be it's own event handler? Explain how to implement this?
Yes, example could be a class that extends Jbutton and implements ActionListener. In the actionPerformed method, put the code to perform when the button is pressed.
13 - Why does JComponent have add() and remove() methods but Component does not?
JComponent have add() and remove() methods but Component does not because JComponent is a subclass of Container, and can contain other components and jcomponents.
14 - What is the default layout for a ContentPane in JFC?
BorderLayout is the default layout for a ContentPane in JFC
15 - What does 'x' mean in javax.swing?
In javax.swing 'x' means extension of java .
16 - Why won't the JVM terminate when I close all the application windows?
The AWT event dispatcher thread is not a daemon thread. You must call System.exit(0) to terminate the JVM explicitly.
17 - Why should the implementation of any Swing callback (like a listener) execute quickly?
Implementation of any Swing callback execute quickly because callbacks are invoked by the event dispatch thread which will be blocked processing other events for as long as your method takes to execute.
18 - Why would you use SwingUtilities.invokeAndWait or SwingUtilities.invokeLater?
SwingUtilities.invokeAndWait(Runnable r) or SwingUtilities.invokeLater(Runnable r) is use to execute code in the event-dispatching thread from outside event-handling or drawing code.
  • SwingUtilities.invokeLater(Runnable r) will add the task to a queue and will still be able to interact with your application.
  • SwingUtilities.invokeAndWait(Runnable r) adds the task to a queue, but will wait until that task ends.

19 - What are the difference between JFC & WFC?
  • JFC supports robust and portable GUI, WFC runs only on the Windows (32-bit) user interface, and uses Microsoft extensions to Java for event handling and ActiveX integration.
  • WFC performance is better than JFC.
  • Memory requirements for WFC are lower than for JFC.
  • WFC develop simple user interfaces easily
  • JFC build complex user interfaces but WFC not.
  • In WFC, the details of any specific component are hidden within the component.
  • JFC gives better control due to its object-oriented nature.

20 - What are the differences between AWT and SWT?
  • AWT [abstract windowing toolkit] develop by sun whereas SWT [standard widget toolkit] develop by IBM.
  • AWT and SWT delegate widget control to the native components on the user's machine.
  • SWT looks good, performs well and is pretty consistent across platforms in comparison to AWT.
  • The AWT library was not very flexible and some components were missing. SWT provides more flexibility then AWT.