Swing interview questions and answers for freshers and experienced candidates. These interview questions and answers on Swing will help you strengthen your technical skills, prepare for the interviews and quickly revise the concepts. Many candidates appear for the interview for one role - many of the them give the right answers to the questions asked. The one who provides the best answer with a perfect presentation is the one who wins the interview race. The set of Swing AWT interview questions here ensures that you offer a perfect answer to the interview questions posed to you.
What is 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 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) is used to access the API to increase the compatibility and enhance the support for the drag and drop capabilities provided by the JFC.
- The AWT 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 and 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 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.
What is 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.
What is the function of lightweight Components used in Swing?Lightweight component allows 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 produces 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 allows modification to be done in the design easily.
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:
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.
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.
It 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.
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.
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:
- 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.
- This layout consists of the components that are represented using the single row or column.
- 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.
- It is the default layout for every Jpanel.
- It displays the components in a single row that starts with a new row.
- It allows the component to be placed and aligned by keeping them in a grid of cells.
- It allows the components to be of equal size and display equal number of rows and columns.
- 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.
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();
- 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.
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:
- It is the manager of all the layouts or the structure that automatically provides the functionality in between the components.
- It also provides the spacing between each component and provides support for it as well.
- 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.
- 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.
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.
Write a program 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();
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));
- The 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.
What is the purpose of action interface in Swing?Action is performed on a state to allow it to change. It defines the interface that it is implementing. The library that is used for the action interface is javax.swing.Action. This action interface extends the ActionListener interface class that is being provided from the AWT. Action interface allow the concrete classes to implement the actionPerformed() method and provide the action that is associated with it. The use of actionPerformed() method allow the implementation of the behavior that is desired in the programming and allow it to show the functionality of the action class. The action can be added to the container class that accepts the parameters on an event like JMenu, JPopupMenu, or JtoolBar. The container used in this automatically registers the action that is taken on the event and it acts as an ActionListener of the user interface.
What is the function of AbstractAction class?
AbstractAction class allows the implementation of the abstract functions and the action interfaces. AbstractAction provides with the default functionality that are provided with the methods in the Action interface. The action can extend the class to create some specific actions for the users. The methods that is provided to implement the actionPerformed() method and it is used to provide the overall functionality for the action. ActionEvent is used to show the action command that can be used to add some features of the contents and provide ready to use action functionality on the event that can be carried for all the user interface fields.
class MyAction extends AbstractAction
public MyAction(String text, Icon icon)
public void actionPerformed(ActionEvent e)
What is the use of JComponent Class in Swing?Jcomponent is used as an abstract class that extends the functionality of the swing components. It allows the common swing component library to provide the functionality and the visual appearance. The class that serves this purpose is : java.awt.Component class. This class also provides the framework for AWT components.
What is the use of double buffering in Swing? Double-buffering is the process of drawing graphics into an off-screen image buffer and then copying the contents of the buffer to the screen all at once. For complex graphics, using double-buffering can reduce flickering.
What is the purpose of Serialization in Swing?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.
How does Accessibility work in Swing?
The feature used in swing consists of accessibility technology that allows the class to archive 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. 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 and is stored in awt.properties consisting of all the specialized classes. Virtual machine in Java accesses the configuration file accessibility.properties contained in the jre/lib directory to allow the namespaces to be fetched to execute the process. The format of awt.properties file is as follows:
# Assistive technologies
# Key names
How is Parsing HTML used in Swing?
JEditorPane class is used to display the HTML content. When a file consisting of the HTML text gives the request to read the file, it identifies 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 kit 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 allows reading of the file and parses the HTML code. The parameter of the type ParserCallback is used to communicate the progress 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.
Write a program to paint the off-screen buffer in swings.
To paint the off-screen buffer in swings requires the paint component. Using an image allows easy creation of set of images and its components in different states. This image is stored, remain useful for automated regression testing and is also known as the golden image approach. It is used to compare the images with the current and old images that uses the last stable build of the image. The code displays it is as follows:
JButton b = new JButton("Image buffer");
// This set the initial size of the image.
BufferedImage image = new BufferedImage(b.getWidth(), b.getHeight(),
Graphics g = image.getGraphics();
ImageIO.write(image, "png", new File("image.png"));
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 followed for this is:
- The creation of an empty table is used to get some data and the insertion is performed using the database techniques.
- After creation of an empty table, creating and attaching of the TransferHandler class is included that allows the proper transfer of the data from one place to another. It also handles the interrupts that may occur while transferring the data.
- Enables 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.
What is the purpose of TransferHandler 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 supports the classes as well.
- The Components are provided with the default handler that can create own handler on their own.
- There are three methods of TransferHandler class and these are as follows:
- This turns the drag support on and it defines the component to support the drag facility and links to the list given by Jlist.
- It is used to configure the drop locations and determines the use of Jlist, Jtable, Jtree.
- It is used to allow the plug in data to be imported or exported by using the Jcomponent.