Java Swing Interview Questions

What is the difference between Swing and AWT components?

AWT components are heavy-weight, whereas Swing components are lightweight. Hence Swing works faster than AWT. Heavy weight components depend on the local windowing toolkit. For example, java.awt.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.

Name the containers which use Border Layout as their default layout?

window, Frame and Dialog classes.

Name Container classes.

Window, Frame, Dialog, FileDialog, Panel, Applet, or ScrollPane

How can a GUI component handle its own events?

A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener.

What is the difference between the paint() and repaint() methods?

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.

Which package has light weight components?

javax.Swing package contains light weight components. All components in Swing, except JApplet, JDialog, JFrame and JWindow are lightweight components.

What are peerless components?

The peerless components are called light weight components.

What is a Container in a GUI?

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.

How are the elements of a GridBagLayout organized?


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


How are the elements of different layouts organized?

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.

What advantage do Java’s layout managers provide over traditional windowing systems?

Java uses layout managers to lay out 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.

What method is used to specify a container’s layout?

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

Which Container method is used to cause a container to be laid out and redisplayed?


Name Component subclasses that support painting.

The Canvas, Frame, Panel, and Applet classes support painting.

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.

What is the difference between a Window and a Frame?

The Frame class extends Window to define a main application window that can have a menu bar.

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.

What is the difference between a Scrollbar and a ScrollPane?

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.

What is the preferred size of a component?

The preferred size of a component is the minimum component size that will allow the component to display normally.

Which containers use a FlowLayout as their default layout?

The Panel and Applet classes use the FlowLayout as their default layout.

Like us on Facebook