Java Swing AWT Interview Preparation Guide
Download PDF

Java Swing frequently Asked Questions in various Swing AWT job Interviews by interviewer. The set of Swing AWT interview questions here ensures that you offer a perfect answer to the interview questions posed to you. Get preparation of Swing AWT job interview

29 Swing AWT Questions and Answers:

Table of Contents

Swing AWT Interview Questions and Answers
Swing AWT Interview Questions and Answers

1 :: Explain the use of JFC in Java Swing?

• JFC stands for Java Foundation Classes that consists of the collection of libraries designed to make the programmers that are creating the enterprise applications using Java Swing. Swing APIs are used to create a suite of library known as JFC. These classes also consist of AWT (Abstract Window Toolkit) that is used to access the API to increase the compatibility and enhance the support for the drag and drop capabilities that are provided by the JFC.
• The AWT that comes under JFC is a toolkit that comes with the Java Development Kit. It is made up of lightweight component.
• JFC consists of accessibility package that is used to provide assistance to the users who are having traditional interfaces. It is used for the devices like text readers and this allows the direct access to be given to the swing components. The accessibility consists of APIs that comes with swing distributions and utilities that are distributed separately.
• JFC consists of drag and drop facilities that provide graphical interfaces by which user can view the object and then it can be moved to another window or frame that is used in the desktop. The API allows the user to implement the elements that are used to transfer the information of Java applications.

2 :: Explain the difference between swing and AWT?

• AWT is a heavyweight component that is used to associate the language with the native component. Whereas, Swing is the lightweight component that doesn't associate, any language with the component that it uses.
• AWT draws the own component on the screen and it uses their own view-port that sends the output to the screen that displays the result. Whereas, Swing doesn't include any writing option to show the result on the screen so it does redirect to the components that makes it.
• AWT components are of the z-ordering and it is not possible to combine any other in the same container. Whereas, Swing doesn't include the z-ordering and it always come below the AWT.
• AWT is less consistent when used on different platforms. Whereas, Swing is more consistent across different platforms.

3 :: Explain the function of lightweight Components used in Swing?

Lightweight component allow the components to be dependent on the native group of other components. These components are simplified types that are used to display themselves on the screen and allow the result to be transparent from others. Each component in this renders itself using the graphics objects and the functions that are provided with it like: drawLine(), fillRect(). They render themselves on the heavyweight level component that they are used to be in. The libraries that are included in this involves: java.awt.Component or java.awt.Container classes. These classes are used to create lightweight components and it doesn't depend on the native group of components. It produce fast results and these components are memory-intensive that allows the user to create the look and feel of the application at run time. Swing component also allow modification to be done in the design easily.

4 :: Why is Model-View-Controller Architecture used in Swing?

Model-View-Controller Architecture is used to show the design of the components that are made up of three elements and these elements show the behavior of components as:
• Model: it consists of the state of individual component. Different models are used for different components for example scrollbar component that consists of the information used to display the current position and the values that are associated with it. Menu, can consists of a simple list containing the menu items that user can select and take actions on. This information doesn't change from component to component and it remains the same in every case. The model remains independent of the visual representation of the component.
• View: it defines the visual representation of the component. The view differs from one window to another window. The application can consist of different user interface platforms and specifications. It consists of all the components related to the visual appearance. It deals with different views of a particular component.
• Controller: provides the way to control the relationship between the model and the view and it allows the interaction to be done with the events. The events can have many forms like mouse click, having focus on some button or some kind of triggers that are on some menu, etc. This is the controller that decides the component that is to be used to react to an event.

5 :: How is multi-threading gets implemented using Swing?

Swing uses multi-threading techniques to utilize the resources that are provided by the Java classes. Swing allows the users to create independent threads that can be used to change the model states of the components. If any changes happens in the component that is reflected on the screen then just updating the model state of the class that is used from the event dispatching queue. This event dispatching queue allows the component to communicate with each other. It also handles the user interface events that are used by the components. To increase the performance it requires the modifications to be refreshed so that the updated data is always received every time the component gets repeated. Multi-threading allows the swing program to run faster. This decreases the delay time and reduces the CPU usage that is used by the programs running on a single thread environment.

6 :: Explain Swing?

Swing is a library of GUI controls. Classes in swing are not OS dependent.
They don’t create peer components, so they are light weight unlike AWT.
They don’t take the look and feel of the target platform so they have a consistent appearance

7 :: Explain AWT?

Abstract Window Toolkit provides a standard Application Programming Interface for writing Graphical User Interfaces in Java.

8 :: Explain the differences between Swing and AWT?

Swing is a library of GUI controls. Classes in swing are not OS dependent.
They don’t create peer components, so they are light weight unlike AWT.
They don’t take the look and feel of the target platform so they have a consistent appearance

9 :: What are heavyweight and lightweight components?

In Java AWT - Abstract Window Toolkit, GUI components created are OS dependent. When they are created, each component forms a peer component due to which they are called heavyweight components.

Swing components are platform independent. No peer components are formed due to which they are called lightweight components.

10 :: What are controls and what are different types of controls in AWT?

Controls are components that allow a user to interact with your application.
A component is a graphical object. A few examples of components are:

Button
Canvas
Checkbox
Choice
Container
Label
List
Scrollbar
TextComponent

11 :: What is JFC and explain the features of JFC?

The Java Foundation Classes are a set of GUI components and services which simplify the development and deployment of commercial-quality desktop and Internet/Intranet applications.

Java Foundation classes are core to the Java 2 Platform.
All JFC components are JavaBeans components and therefore reusable, interoperable, and portable.
JFC offers an open architecture. Third-party JavaBeans components can be used to enhance applications written using JFC.
Truly cross-platform.
Fully customizable.

12 :: Explain difference between Swing and JSF?

JSF applications cannot operate when a network is not present.
Swing applications can operate on local data when the network is not there.

13 :: Explain the difference between AWT and SWT?

Abstract Windows Toolkit (AWT) is the original Java GUI tool kit.
AWT's comes standard with every version of Java technology.
It is very stable.
It doesn’t need to be installed.
AWT is a very simple tool kit with limited GUI components, layout managers, and events.
Lowest-common denominator (LCD) approach is used for for AWT.
Some commonly used components like Tables, Trees, Progress Bars, and others, are not supported.
Applications that need more component types need to be created from scratch.
SWT is based on a peer implementation.
It has an adequate set of controls to make most applications on a host-by-host basis, creating emulated controls for any not supplied by the particular host. Thus, it overcomes the LCD problem faced by AWT.
In SWT, the peers are just wrappers on host controls.
In AWT, peers can provide services to minimize the differences between hosts.
SWT is a good enough portable solution but not as well as Swing.

14 :: Explain the difference between JFC and WFC?

JFC supports robust and portable user interfaces.
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 is easy to develop simple user interfaces.
It is difficult to build complex user interfaces with WFC than JFC.
In WFC, the details of any specific component are hidden within the component.
JFC provides a greater degree of control due to its object-oriented nature.

15 :: What are the different types of layout managers used in Swing?

Layout manager is an object that is used to implement an interface which determines the size and position of the component that is used inside a container. Components can provide the size and the alignments regarding the container's layout. The main layouts that are used in the layout managers are given as:
• BorderLayout: this layout is used by every content panel. It places all the components in all the five areas given top, bottom, left, right, and center. This layout can be created by using the JtoolBar.
• BoxLayout: this layout consists of the components that are represented using the single row or column.
• CardLayout: this layout is used to implement the area that consists of many other components used at different times. This layout is controlled using the combo box.
• FlowLayout: it is the default layout for every Jpanel and it displays the components in a single row that starts with a new row.
• GridBagLayout: it allows the component to be placed and aligned by keeping them in a grid of cells.
• GridLayout: it allows the components to be of equal size and display equal number of rows and columns.
• GroupLayout: it uses the user interface builder tools that allows the work can be done by using the vertical and horizontal layouts. The dimensions are defined independently.

16 :: What is the process of setting the layout manager?

The layout managers uses Jpanels and content panes to initializes the object that is to be used with the FlowLayout by default and it can be changed by defining a different layout. Content panes uses border layout by default and it uses appropriate tools that can be used to code the appropriate object that has to be used. The panel layout manger can be setup by using the Jpanel constructor and it can be shown by an example given as:

JPanel panel = new JPanel(new BorderLayout());

The setLayout method is used to set the layout manager after the container is being created.

Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());

The container's layout property can be changed to NULL to allow the absolute positioning to take place where the specification is given in the form of size and positon of each component that is used inside the container.

17 :: What are the different components that are used to show a container?

There are few components that are used to show the container used to define the visual properties of it. The components are given below:
• The layout manager: it is the manager of all the layouts or the structure that automatically provide the functionality in between the components. This is the only manager that does that. It also provides the spacing between each component and provides support for it as well.
• Invisible components: This is used to create lightweight components that allow performing no user interface functionality but it provides the space in the graphical user interface. This component is contained or controlled by the BoxLayout.
• Empty borders: the space between the components can be affected by adding the empty borders to the components that are defined within the block. This uses the borders that are having no border at all like panels and labels.

18 :: What is the function of Internal Frame in Swing?

Internal frame allow one window to be displayed within another window. Adding of internal frame can be used to the desktop pane or the content pane. The user interface allows choosing the base of the application that is surrounded around the frames. The frames can be switched and other frames can be chosen over the other one. This internal frame has the same function that is provided in the normal Frame object, this object is confined inside the visible area of the container. The internal frame can have icons that can be used inside the application frame and have the properties of maximize and minimize so that the frame consists of main application frame. It closes the frame using the standard controls that is given for a popup window. Internal frame can be given in a layer that uses the display function to display the relative to the other internal frames.

19 :: How to include the Internal Frame in Swing?

Internal frame is the frame that keeps one window inside another window and allows them to switch from one frame to another frame. The program shows the display of the internal frame:
To set the internal frame their need to be set up the layered pane.
The desktop pane is setup and then command is given to create a new internal frame using the properties. The JinternalFrame method is used as a constructor to take some parameters and provide the look and feel as well as the functionality of the frame.

JDesktopPane desktop = new JDesktopPane();
add(desktop, BorderLayout.CENTER);
public JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable);
internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
internalFrame.setBounds(50, 50, 200, 100);
desktop.add(internalFrame, new Integer(1));

desktop.add() method allows the call to be done to the internal frame to allow the layer frame to be called for the frame it belongs to. Layers are used as an integer objects that determine the order of the layer and shows the functionality on top of the frame.

20 :: What is the purpose of Transfer Handler Class?

TransferHandler class is used for data transfer and it provides an easy way to transfer the data from the Jcomponent. All the details of the data are contained in the class and it support the classes as well. Components are provided with the default handler that can create own handler of their own. There are three methods that can be used to use theTransferHandler on a component and these are as follows:
• setDragEnabled(boolean)- this turn the drag support that is given as false and it defines the component that support the drag facility that links to the list given by Jlist.
• setDropMode(DropMode)- is used to configure the drop locations and determine the use of Jlist, Jtable, Jtree.
• setTransferHandler(TransferHandler)- is used to allow the plug in data to be imported or exported by using the Jcomponent that is defined for it.

21 :: What are the steps required in creating an empty table drop?

The dragging and dropping for an empty table allows it to be portable and the data can be moved with ease. The steps that are followed for this is:
• The creation of an empty table that is used to get some data and the insertion gets performed in using the database techniques.
• After creation of an empty table then the creating and attaching of the TransferHandler class gets included that allow the proper transfer of the data from one place to another. It also handles the interrupts that may occur while transferring the data.
• Enable the data transfer to allow the calling of the object setDragEnabled(true). When this value is set to true then the table can be draggable and the data can be moved.
• Creation of the scroll pane takes place to allow the adding of the table so that it can be dragged from one pane to another pane with ease and comfort.

22 :: Write a program to paint the off-screen buffer in swings?

To paint the off-screen buffer in swings requires the paint component that is using an image allowing easy to create set of images and its components in different states. This image that is stored, remain useful for automated regression testing that is also known as the golden image approach that is used to compare the images with the current and old images that uses the last stable build of the image. The code that displays it is as follows:

JButton b = new JButton("Image buffer");
button.setSize(100, 50);
// This set the initial size of the image.
BufferedImage image = new BufferedImage(b.getWidth(), b.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
button.paint(g);
ImageIO.write(image, "png", new File("image.png"));

23 :: How to Parse HTML used Swing?

JEditorPane class is used to display the HTML content that is being written using the classes. When there is a file consisting of the HTML text gives the request to read the file identify the method to read the HTML text using the read() method that is given in HTMLEditorKit. This kit provides lot of tools that delegates the file to a separate parser object. This kid also consists of tools and the abstract inner class consisting of the method as:

public abstract void parse(Reader r, ParserCallback cb) throws IOException

The read() method calls the getParser() method allowing the subclasses to parse the object and use the HTML text. The parser currently uses the package javax.swing.text.html.parser that allow the reading of the file and parse the HTML code. The parameter of the type ParserCallback is used to communicate the progress of the process that is parsing the HTML file.
In the parse() method we listed earlier, you'll notice a very important parameter of type
ParserCallback . This is how the parser communicates its progress.

24 :: How does Accessibility works in Swings?

The feature that is used in swing consists of accessibility technology that allows the class archives like JAR file. This file resides on separate machine and can not be accessed without the permissions given from the owner. Java is used to load the technology in the virtual machine that is used as an application. The technologies allow the replacement to be done using the virtual machine. It uses the queue method and the class as java.awt.EventQueue. The properties are used using the configuration file that is stored in awt.properties and it consists of all the specialized classes. Virtual machine in Java accesses the configuration file accessibility.properties that is contained in the jre/lib directory to allow the namespaces to be fetched to execute the process. The format is given of awt.properties file as:

AWT.alt=Alt
AWT.meta=Meta
# Assistive technologies
AWT.assistive_technologies=SpeechRecognition
AWT.EventQueueClass=com.sun.java.accessibility.util.EventQueueMonitor
# Key names
AWT.enter=Enter
AWT.backSpace=Backspace

25 :: What is the purpose of Serialization in Swings?

Serialization is used when an object extends the Jcomponent class. The object's data in this case points out to the stream that is written or serialized and gives an output stream. This output stream can be sent through then network and it can be saved in the file if required. The serialized file can be de-serialized in the memory. This will allow the operation to continue to perform the original state functions that were getting used. The serialization used in object allows easy retrieval of the object data and easy transmission of state data from one place to another. This can also be placed in custom made storage files that provide the transfer of the components to be fast and it uses the concept of virtual machine that transfers from one machine to another machine. It uses the remote method invocation to allow the use of distributed computing that provide with some exceptions to the use of transient keyword. The components can be serialized using the classes that can pass the reference to an object using the writeObject() method of the ObjectOutputStream object this way it calls the serialization function recursively.