Java Awt and Swing Interview Questions and Answers - 1

Question: 1

What is a layout manager and what are different types of layout managers available in Java Swing?

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 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: 2

Does Swing is thread safe?

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 few like repaint() and revalidate().

Question: 3

Why swing is not thread safe?

The Swing API was designed to be powerful, flexible and easy of use.

In particular, we wanted to make it easy for programmers to build new Swing components, whether from scratch or by extending 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: 4

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.

Question: 5

What do heavy weight components mean?

Heavy weight components like Abstract Window Toolkit (AWT) depend on the local windowing toolkit. For example, java.awt.

Button is a heavy weight component.

Error Report!

Related Questions