Q6 – Q11 Swing interview questions and answers

Extends Q1 to Q5 Swing interview questions and answers.

Q6. If you add a component to the CENTER of a border layout, which directions will the component stretch?
A6. The component will stretch both horizontally and vertically. It will occupy the whole space in the middle.

Q7. What is the base class for all Swing components? What design pattern does this follow?
A7. Design pattern: As you can see from the diagram below, containers collect components. Sometimes you want to add a container to another container. So, a container should be a component. For example container.getPreferredSize() invokes getPreferredSize() of all contained components. Composite design pattern is used in GUI components to achieve this. A composite object is an object, which contains other objects. Composite design pattern manipulates composite objects just like you manipulate individual components.

Composite design pattern

Composite design pattern

All the Swing components start with ‘J’. The hierarchy diagram is shown below. JComponent is the base class.

Swing components

Swing components

Q8. Explain the Swing event dispatcher mechanism?
A8. Swing components can be accessed by the Swing event dispatching thread. A few operations are guaranteed to be thread-safe but most are not. Generally the Swing components should be accessed through this event-dispatching thread. The event-dispatching thread is a thread that executes drawing of components and event-handling code. For example the paint() and actionPerformed() methods are automatically executed in the event-dispatching thread. Another way to execute code in the event-dispatching thread from outside event-handling or drawing code, is using SwingUtilities invokeLater() or invokeAndWait() method. Swing lengthy initialization tasks (e.g. I/O bound and computationally expensive tasks), should not occur in the event-dispatching thread because this will hold up the dispatcher thread. If you need to create a new thread for example, to handle a job that’s computationally expensive or I/O bound then you can use the thread utility classes such as SwingWorker or Timer without locking up the event-dispatching thread.

  • SwingWorker – creates a background thread to execute time consuming operations.
  • Timer – creates a thread that executes at certain intervals.

However after the lengthy initialization the GUI update should occur in the event dispatching thread, for thread safety reasons. We can use invokeLater() to execute the GUI update in the event-dispatching thread. The other scenario where invokeLater() will be useful is that the GUI must be updated as a result of non-AWT event.

Q9. What do you understand by MVC as used in a JTable?
A9. MVC stands for Model View Controller architecture. Swing “J” components (e.g. JTable, JList, JTree etc) use a modified version of MVC. MVC separates a model (or data source) from a presentation and the logic that manages it.

Screen shot 2015-03-01 at 10.39.38 AM

  • Component (e.g. JTable, JTree, and JList): coordinates actions of model and the UI delegate. Each generic component class handles its own individual view-and-controller responsibilities.
  • Model (e.g. TableModel): charged with storing the data.
  • UIDelegate: responsible for getting the data from model and rendering it to screen. It delegates any look-and-feel aspect of the component to the UI Manager.

Q10. Explain layout managers? What design pattern does it use?
A10. Layout managers are used for arranging GUI components in windows. The standard layout managers are:

  • FlowLayout: Default layout for Applet and Panel. Lays out components from left to right, starting new rows if necessary.
  • BorderLayout: Default layout for Frame and Dialog. Lays out components in north, south, east, west and center. All extra space is placed on the center.
  • CardLayout: stack of same size components arranged inside each other. Only one is visible at any time. Used in TABs.
  • GridLayout: Makes a bunch of components equal in size and displays them in the requested number of rows and columns.
  • GridBagLayout: Most complicated but the most flexible. It aligns components by placing them within a grid of cells, allowing some components to span more than one cell. The rows in the grid aren’t necessarily all the same height, similarly, grid columns can have different widths as well.
  • BoxLayout: is a full-featured version of FlowLayout. It stacks the components on top of each other or places them in a row.

Complex layouts can be simplified by using nested containers for example having panels within panels and each panel can use its own LayoutManager. It is also possible to write your own layout manager or use manual positioning of the GUI components. Note: Further reading on each LayoutManagers is recommended for Swing developers.

Design pattern: The AWT containers like panels, dialog boxes, windows etc do not perform the actual laying out of the components. They delegate the layout functionality to layout managers. The layout managers make use of the strategy design pattern, which encapsulates family of algorithms for laying out components in the containers. If a particular layout algorithm is required other than the default algorithm, an appropriate layout manager can be instantiated and plugged into the container. For example, panels by default use the FlowLayout but it can be changed by executing:

This enables the layout algorithms to vary independently from the containers that use them. This is one of the key benefits of the strategy pattern.

Q11. Explain the Swing delegation event model?
A11. In this model, the objects that receive user events notify the registered listeners of the user activity. In most cases the event receiver is a component.

  • Event Types: ActionEvent, KeyEvent, MouseEvent, WindowEvent etc.
  • Event Processors: JButton, JList etc.
  • EventListeners: ActionListener, ComponentListener, KeyListener etc.

Screen shot 2015-03-01 at 10.49.59 AM

300+ Java Developer Interview Q&As

800+ Java Interview Q&As