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 (e.g. panels by default uses the FlowLayout but it can be changed by
executing -> panel.setLayout(new GridLayout(4,5))). This enables the layout algorithms to vary independently from the containers that uses them, this is one of the key benefits of the strategy pattern.
No comments:
Post a Comment