Misc. Topics in Java


Earlier, we discussed how to draw graphics, handle events, and use GUI components. In doing so, we ignored an important issue: How to place our GUI components in our windows to make an attractive and useful UI. This process is called GUI layout and Java provides several alternative strategies.

The key problem we are trying to solve is to keep the UI attractive and useful for a variety of window sizes and shapes. If we do not allow the user to resize the window (or resize it for different screen sizes), we can simply specify the exact location and size for each component (we will discuss how later).

Containers and Layouts

Flow Layout

The flow layout is the default layout for JPanel. The flow layout arranges the widgets in rows, according to the order of addition. Components are added to each row until no-more can fit, then another row is started. Rows are filled from top to bottom. The components within a row are center aligned by default. When the frame window is resized, the flow latout recomputes the position of the components based on this algorithm.

Border Layout

Grid Layout

Box Layout

GridBag Layout

null Layout


JAR files