Java Swing Interview Questions in Java

List of topic-wise frequently asked java interview questions with the best possible answers for job interviews.

Java Swing Interview Questions in Java

Question 1.
Why swing is not thread-safe?
Answer:
The Swing API was designed to be powerful, flexible, and easy to use. In particular, we wanted to make it easy for programmers to build new Swing components, whether from scratch or by extending the components that we provide. For this reason, we do not require Swing components to support access from multiple threads. Instead, we make it easy to send requests to a component so that the requests run on a single thread.

Question 2.
Which package has lightweight components?
Answer:
javax.Swing package. All components in Swing, except JApplet, JDialog, JFrame, and JWindow are lightweight components.

Question 3.
What are peerless components?
Answer:
The peerless components are called lightweight components.

Question 4.
What is the difference between the Font and FontMetrics classes?
Answer:
The FontMetrics class is used to define implementation-specific properties, such as ascent and descent, of a Font object.

Question 5.
What is the difference between Swing and AWT components?
Answer:
AWT components are heavy-weight, whereas Swing components are lightweight. Hence Swing works faster than AWT. Heavyweight components depend on the local windowing toolkit. For example, java.awt.The button is a heavy-weight component. Pluggable look and feel possible using java Swing. Also, we can switch from one look and feel to another at runtime in swing which is not possible in AWT.

Question 6.
Name the containers which use Border Layout as their default layout?
Answer:
window, Frame, and Dialog classes.

Question 7.
Name Container classes.
Answer:
Window, Frame, Dialog, FileDialog, Panel, Applet, or ScrollPane

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

Question 9.
What is the difference between the paint() and repaint( ) methods?
Answer:
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.

Question 10.
Which package has lightweight components?
Answer:
javax.The swing package contains lightweight components. All components in Swing, except JApplet, JDialog, JFrame and JWindow are lightweight components.

Question 11.
What are peerless components?
Answer:
The peerless components are called lightweight components.

Question 12.
What is a Container in a GUI?
Answer:
A Container contains and arranges other components (including other containers) through the use of layout managers, which use specific layout policies to determine where components should go as a function of the size of the container.

Question 13.
How are the elements of a GridBagLayout organized?
Or
What is a layout manager and what are the different types of layout managers available in java Swing?
Or
How are the elements of different layouts organized?
Answer:
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.

Question 14.
What advantage do Java’s layout managers provide over traditional windowing systems?
Answer:
Java uses layout managers to layout 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.

Question 15.
What method is used to specify a container’s layout?
Answer:
The setLayout( ) method is used to specify a container’s layout. For example, setLayout(new FlowLayout( )); will be set the layout as FlowLayout.

Question 16.
Which Container method is used to cause a container to be laid out and redisplayed?
Answer:
validate( )

Question 17.
Name Component subclasses that support painting.
Answer:
The Canvas, Frame, Panel, and Applet classes support painting.

Question 18.
What is the purpose of the enableEvents() method?
Answer:
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.

Question 19.
What is the difference between a Window and a Frame?
Answer:
The Frame class extends Window to define the main application window that can have a menu bar.

Question 20.
What do heavy-weight components mean?
Answer:
Heavyweight components like Abstract Window Toolkit (AWT) depend on the local windowing toolkit. For example, java.awt .The button is a heavy-weight component.

Question 21.
What is the difference between a Scrollbar and a ScrollPane?
Answer:
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.

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

Question 23.
Which containers use a FlowLayout as their default layout?
Answer:
The Panel and Applet classes use the FlowLayout as their default layout.

Question 24.
What is Event-Driven-Thread (EDT) in Swing?
Answer:
Event-Driven-Thread or EDT is a special thread in Swing and AWT. Event-Driven Thread is used to draw graphics and listen for events in Swing. You will get a bonus point if you are able to highlight that time-consuming operation like connecting to the database, opening a file, or connecting to the network should not be done on the EDT thread because it could lead to freezing GUI because of blocking and time-consuming nature of these operations instead of the,’ should be done on a separate thread and EDT can just be used to spawn those thread on a button click or mouse click.

Question 25.
Does Swing is thread-safe? What do you mean by swing is not thread-safe?
Answer:
Since Swing components are not thread-safe it means you cannot update these components in any thread other than Event-Driven-Thread. If you do so you will get unexpected behavior. Some time interviewer will also ask what are thread-safe methods in swing which can be safely called from any thread only a few like repaint( ) and revalidate( )

Question 26.
What are the differences between Swing and AWT?
Answer:
There is a couple of differences between swing and AWT:

  1. AWT components are considered to be heavyweight while Swing components are lightweights
  2. Swing has a pluggable look and feel.
  3. AWT is platform depended same GUI will look different on the different platforms while Swing is developed in Java and is platform-dependent.

Question 27.
Why Swing components are called lightweight components?
Answer:
AWT components are associated with native screen resources and called heavyweight components while Swing components use the screen resource of an ancestor instead of having their own and that’s why called lightweight or lighter components.

Question 28.
What is the difference between invokeAndWait and invokeLater?
Answer:
Sometimes the question is how do you update the swing component from a thread other than EDT, for such kind of scenario we use SwingUtilities. invokeAndWait(Runnable r) and SwingUtilities.invoke setter(Runnable r) though there are quite a few differences between these two, the major one is invokeAndWait is a blocking call and wait until GUI updates while invokeLater is a nonblocking asynchronous call. In my opinion, these question has its own value and every swing developer should be familiar with these questions or concept not just for interview point of view but on application perspective, you can read more on my post How InvokeAndWait and InvokeLater works in Swing

Question 29.
Write code for JTable with custom cell editor and custom cell Tenderer?
Answer:
JTable is one of the favorite topics of all Swing interviews and the most popular questions on swing interviews are from JTable why? Because here interviewer will directly ask you to write code another reason is JTable heavily used in all Electronic TRADINGS GUI. GUI used for online stock TRADINGS uses JTable to show data in tabular format so an in-depth knowledge of JTable is required to work on ONLINE TRADING GUI developed in Swing. While this question is just an example questions around JTable are mostly centered around updating the table, how do you handle a large volume of data in the table, using customize cell Tenderer and editor, sorting table data based on any column, etc. so just make sure you have done quite a few handsome exercises on JTable before appearing for any java swing interview in IB.

Question 30.
Write code to print following layout (mainly focused on GridBag layout)?
Answer:
GridBagLayout in the swing is the most powerful but at the same time, most complex layout and a clear-cut experience and expertise around GridBagLayout is desired for developing Swing GUI for trading systems. No matter whether you are developing GUI for equities trading, futures, or OPTIONS TRADING or forex trading you always required GridBagLayout. Swing interview question on GridBagLayout will be mostly on writing code for a particular layout just like an example shown here. In which six buttons A, B, C, D, E, and F are organized in a certain fashion.

Question 31.
How do you handle the opening of a database, file, or network connection with a click of a button?
Answer:
You should not do this operation in the EDT thread instead spawn a new thread from the actionListener or button and disable the button until the operation gets completed to avoid resubmitting the request. The only condition is that your GUI should always be responsive no matter what happens on a network connection or database connection because these operations usually take time.