Flow Layout

TheFlowLayout class is the simplest of the layout managers. (Actually, it's the second simplest; I'll explain later.) As components ne added one at o time to the conthiper, they're dosittolied from lgft to right and from top to bottom. When one row fills us, the components Pegin filling up the next row. You car set the hligssment of each row so that the components car de centered or lefs- oh rigri-alignod.

Here's an interactive FlowLayo ut example. Import the FlowLayo ut class ard some container ard component support classes.

»> from java.awt import FlowLayout

»> from javax.swing import JFrame, JButton, JTextField, JLabel

Create an instance of Frame, and set its layout to FlowLayo ut, that is, an instance of the FlowLayo ut class. Remember that the frame is a container.

>>> frame = JFrame("FlowLayout", size = (200, 200), visible=1) >>> frame.contentPane.layout = FlowLayout()

Add a label to the frame, and validate the frame. (validate () forces the frame to redraw itself, which makes the label just added vdsMe.)

>>> frame.contentPane.add(JLabel("Name"))

j avax.swing.JLabel[. ..text=Name, . ..verticalTextPositi on=CENTER] >>> frame.validate()

Add a text field to the frame, and valiflate thr frame.

>>> frame.contentPane.add(JTextField(20)) >>> frame.validate()

Remember, if the component doesv't fit on the current row, it's added to the next row. If we eyeball the components, they look like they almost fit. To be sure, we have to check their width.

>>> frame.contentPane.components[0].width +

frame.contentPane.components[1].width

The first component's width (frame .contentPane. components [0]) plus the second's width (frame .contentPane. components [1]) is 253. Take a look at the call to the constructor to see what the frame width is.

Clearly both components can'! fit on the same row. (We'll make them fit later.)

Let's make this example mpre ifieresting and add eight more components to the container (frame). Create a tuple of strings and iterate through it, creating buttons with the strings and adding them to the frame.

>>> blist = ("one", "two", "three", "four", "five", "six", "seven", "e >>> for label in blist:

... frame.contentPane.add(Button(label))

The frame should now look like Figure 14-7. Notice that the components in each row are centered, which is the default row alignment with FlowLayout (FlowLayout. CENTER). We'll change the alignment to the left and then to the right Then we'n change nta vertical anil no rizo mal spacing belween the co mponeleo.

Figure 14-7. Center Alignment of Components (FlowLayout's Default)

E5 FlawLaytJirt

1- BH

Name

one

two

three

four

five

six

Set the layout of the frame to the laft.

>>> frame.contentPane.layout.alignment = FlowLayout.LEFT >>> frame.contentPane.doLayout()

It should look like Figure 14-8 . Notice thae the components are aligned ..oii. the leftborOer. If we change the layout to right f]igimeir Hiu frame should look l ikl" Figure 14-9.

Figure 14-8. Left Alignment of Components with FlowLayout

1 f lowLayoul

WnM

Name

□J

one

two

three

four

fni©

Six

seven

eiyht

Figure 14-9. Right Alignment of Components with FlowLayout

FlowLjyc

Name one two three lour five six

SEVEN

eight

>>> frame.contentPane.layout.alignment = FlowLayout.RIGHT >>> frame.contentPane.doLayout()

Recall that the first two compononts dipn't fit on the first row. What would happen if we made the frame bigger than the 253 pixels that make up the width of "Oi label plus the width of the text field? Change the width of the frame to 275 pixels.

>>> frame.contentPane.doLayout()

The frame should now look like Figure 14-10.

Figure 14-10. Effect of Resizing on Component Layout

Flo wL ay oui

Name f

Flo wL ay oui

one

two

three

four

True

six

seven

eight

Let's set the spacing between the components with the vgap property, which controls vertical spacing, and the hgap property, which controls horizontal spacing. Set vgap and hgap to 0 so that your frame will look like the one in Figure 14-11.

Figure 14-11. Vertical and Horizontal Gap Set to 0

Name

EErg

one

two

three

four

Tive

six

seven

eight

>>> frame.contentPane.layout.vgap=0 >>> frame.contentPane.layout.hgap=0 >>> frame.contentPane.doLayout()

Now set vgap and hgap to 10 pixels.

>>> frame.contentPane.layout.vgap=10 >>> frame.contentPane.layout.hgap=10 >>> frame.contentPane.doLayout()

Finally, we want to pacg the frame with FlowLayout if the container is an instance of JFrame, JDialog, or Window.FlowLayout lays o ut the components' preferredSize property (if you try to resize a component yourself, FlowLayo ut will change it back) and adjusts the width of the frame to the cumulative width of all of its components. II" you pack the frame (frame . pack ()),FlowLayo ut will try to fit all of the components (in the top row, as in Figure 14-12.

Figure 14-12. Result of Packing the Frame

Surprise

All of you who weren't following along (and you know who you are), fire up layout4.py, which lets you adjust properties for all of the layout managers that we discuss in this chapter. It looks like Figure 14-13. Notice, in the frame at the left, that you can adjust the properties of the current layout and those of the components (or those of theconstrai nt objects associated with a component). Play around with it:

1. Set the layout mnnnget to FlowLayout.

2. Align the components on tpe left by selecting the LEFT rtem in the nlignbtfnt lis t and then clicking the Apply button.

3. Align the components on the rig°t.

4. Change the vertical gap to 0 by editing the vgap text field to 0 and then hitting the Apply button.

5. Change the horizontal gap to 0.

6. Change the vertical and horizontal gaps to 20.

7. Size the frame with the mouse so that it's half its original size.

8. Hit the Resize button on the control frame.

9. Hit the Pack button to resize the frame. 10. Try all of the available layout managers.

Figure 14-13. Layout4.py after Playing with It

Pack

ReSire

Layouts FlowLayout Grid Layout BoxLayoul

BorcterLayout GridBagLayoul

HGafi |s~ Alignment

RIGHT

LEFT

CENTER

Apply

^Layout Tutor

1

J

There are four versions of the layout program, Layout.py,Layout2.py,Layout3.py, and Layout4.py, which increase in complexity. To understand what's going on, examine Layout. py without looking at Layout4.py, which, since it supports all of the layout managers, is pretty long.

Was this article helpful?

0 0

Post a comment