Work on Your Swing — Chapter 13

Upulie Handalage
3 min readJul 25, 2022

418–446

Layout Manager objects control the size and location of the widgets in a Java GUI. Swing is an addition to layout managers, which includes widgets. Swing helps it easier to obtain results with layout manager.

A widget is technically a Swing component. Almost every thing you can stick in a GUI extends from javax.Swing.JComponent.

The three layout managers

  1. borderlayout — A BorderLayout manager divides a background component into five r~ions. You canadd only one component per region to a background controlled by a BorderLayout manager. Components laid out by this manager usually don’t get to have their preferred size. 9orderlayout Is the default layout II\Qna~ for Q fromel
  2. flow — A FlowLayout manager acts kind of like a word processor, except with components instead of words. Each component is the size it wants to be, and they’re laid out left to right in the order that they’re added. with ·word-wrap” turned on. So when a component won’t tit horizontally, it drops to the next “line” in the layout. FlowLayout Is the default layout mo~,. for a pone”
  3. box — A BoxLayout manager is like FlowLayout in that each component gets to have its own size, and the components are placed in the order in which they’re added. But. unlike FlowLayout. a BoxLayout manager can stack the components vertically (or horizontally, but usually we’re just concerned with vertically). It’s like a FlowLayout but instead of having automatic ‘component wrapping’, you can insert a Sort of ‘component return key’ and force the components to start a new line. -

Few facts about Swing

  1. Layout managers control the size and location of components nested within other components.
  2. When you add a component to another component (sometimes referred as a background component, the added component is controlled by the layout manager of the background component
  3. A layout manager asks components for their preferred size, before making a decision about the layout. Depending on the layout manager’s policies, it might respect all, some, or none of the component’s wishes.
  4. The BorderLayout manager lets you add a component to one of the five regions. You must specify the region when you add the component, using the following syntax:

add (BorderLayout. EAST, panel);

5. With BorderLayout, components in the north and south get their preferred height but not width. Components In the east and west get their preferred width, but not height. The component in the center gets whatever is left over (unless you use pack () ).

6. The pack() method is like shrink-wrap for the components; It uses the full preferred size of the center component, then determines the size of the frame using the center as a starting point, building the rest based on what’s in the other regions.

7. FlowLayout places components left to right. top to bottom, in the order they were added, wrapping to a new line of components only when the components won’t fit horizontally.

8. RowLayout gives components their preferred size in both dimensions.

9. BoxLayout lets you align components stacked vertically, even if they could fit side-by-side. Unlike FlowLayout, BoxLayout uses the preferred size of the component in both dimensions.

10. BorderLayout is the default layout manager for a frame; FlowLayout Isthe default for a panel.

11. If you want a panel to use something other than flow, you have to call setLayout () on the panel.

--

--

Upulie Handalage

Everything in my point of view. Here for you to read on....