Working with Nested Containers and Layouts

Most UI designs in Java use more than one type of layout to get the desired component positions by nesting multiple panels with different layouts in the main container. You can also nest panels within other panels to gain more control over the placement of components. By creating a composite design, and by using the appropriate layout manager for each panel, you can group and arrange components in a way that is both functional and portable.

For example, the following sketch demonstrates the use of nested panels with different layouts. The solid gray objects are the buttons and other visible components in the UI which are nested in various levels of panels.

nested layout sample

To create nested panels:

  1. Add a panel to the UI design of your application.

  2. In the Property Inspector, change the Layout property to null.

    When you add a panel to the UI design, it uses FlowLayout. Change the layout to null initially because it is the easiest layout to work with during the design process. After you have all the panels and other components in place in your UI, you will set it to its final layout.

    To locate a specific panel in the Java Visual Editor, you can select the panel in the Structure window.

  3. Place panels within panels to logically group components.

  4. Add components to the panels.

  5. Set the layout property for each panel to the appropriate layout.