Java Swing Lecture 3 Layout Management Boriana Koleva
Java Swing - Lecture 3 Layout Management Boriana Koleva (bnk@cs. nott. ac. uk) 1
Where are we up to? § Last Time: • Containers (Frames and Dialogs) § Hierarchy § Root Panes § Layered Panes § Content Panes § Glass Panes • Components § Lots of ‘em… § Today • The exciting topic of “Layout Management” 2 G 5 BUID - Java Swing - 2005
Laying out components • Manage realized components • Determine size and position • Each container has a layout manager w (usually) 3 G 5 BUID - Java Swing - 2005
Layout managers – general aspects • • • Creating a layout manager Consulting managers Types of managers Choosing managers Other features of component layout • All Covered very well here: w http: //java. sun. com/docs/books/tutorial/uiswing/layout/using. html 4 G 5 BUID - Java Swing - 2005
Creating a layout manager • Default layout managers w JFrame, JDialog, JApplet have Border. Layout w JPanel has Flow. Layout § Except when used as a Content Pane (Border Layout) • Setting the layout manager for a container w JFrame frame = new JFrame(); frame. set. Layout(new Flow. Layout()); w JPanel content. Pane = new JPanel(); content. Pane. set. Layout(new Border. Layout()); 5 G 5 BUID - Java Swing - 2005
Consulting layout managers (1) • Consulted automatically when container may need to change its appearance. • These methods result in consultation, but DON’T trigger new layout w add(), remove. All() w get. Alignment. X(), get. Alignment. Y() w get. Preferred. Size(), get. Minimum. Size(), get. Maximum. Size() 6 G 5 BUID - Java Swing - 2005
Consulting layout managers (2) • These methods actually result in the manager performing layout. w JFrame. pack(); § Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. w JFrame. show() & JFrame. set. Visible(true); § Shows the component w JComponent. revalidate(); § This method will automatically be called on this component when a property value changes. Looks for all dependent components and calls validate() on them. Validate() causes a container to lay out its subcomponents again 7 G 5 BUID - Java Swing - 2005
Layout managers - types • • • Border. Layout Box. Layout Flow. Layout Grid. Bag. Layout Card. Layout 8 G 5 BUID - Java Swing - 2005
Border. Layout • Five areas w w NORTH, SOUTH, EAST, WEST and CENTER Not all areas must be used Do not assume a default area for components Centre gets as much area as possible • Specify location as argument of add method w pane. set. Layout(new Border. Layout()); w pane. add(new JButton(“Button 1 (NORTH)”), Border. Layout. NORTH); • Setting gaps between components (default = 0) w Border. Layout. set. Hgap(int gap); w Border. Layout. set. Vgap(int gap); w Border. Layout(int horizontal. Gap, int vertical. Gap) - Constructor 9 G 5 BUID - Java Swing - 2005
Box. Layout (1) • Components on top / next to each other w Direction is your choice • Tries to size components at preferred height for Y_AXIS or width for X_AXIS • Width as largest component width w See above picture 10 G 5 BUID - Java Swing - 2005
Box. Layout (2) • Space fillers w Rigid - fixed-size space between two components w Glue - taking up no space unless you pull apart the components that it's sticking to. Helps reposition extra space (default is at end) w Custom - Use this to specify a component with whatever minimum, preferred, and maximum sizes you want 11 G 5 BUID - Java Swing - 2005
Box. Layout (3) • Component sizes w Respect Max, Min and Preferred Sizes of components • Alignment w Comes into play when not all components are the same width w Can specify Left (0), Centre (0. 5) or Right (1). Or Top Middle Bottom • If you are having layout problems, first treat as an Alignment issue, then examine sizes. 12 G 5 BUID - Java Swing - 2005
Flow. Layout • • Very simple - JPanel’s default Components in row(s) At preferred size Alignment w Flow. Layout. LEFT w Flow. Layout. CENTRE w Flow. Layout. RIGHT • Gaps w Default = 5 w Specifying - setter h. Gap v. Gap methods or via constructor 13 G 5 BUID - Java Swing - 2005
Grid. Layout • Grid of cells - all same size • Components take all space in a cell • Gaps w default = 5 w use setter methods h. Gap and v. Gap w or via arguments to constructor • Re-sizing w Cells resize to be as large as possible in given window / container 14 G 5 BUID - Java Swing - 2005
Grid. Bag. Layout (1) • • Very flexible (and complex!) Rows can have different heights Columns can have different lengths Uses cells in a grid Grid. Bag. Layout gridbag = new Grid. Bag. Layout(); Grid. Bag. Constraints c = new Grid. Bag. Constraints(); JPanel pane = new JPanel(); pane. set. Layout(gridbag); //--- For each component to be added to this container: //---. . . Create the component. . . //---. . . Set instance variables in the Grid. Bag. Constraints instance. . . gridbag. set. Constraints(the. Component, c); 15 pane. add(the. Component); G 5 BUID - Java Swing - 2005
Grid. Bag. Layout (2) • Constraints w w w w w set in an instance of a grid. Bag. Constraints Object gridx and gridy - The row and column of the upper left of the component Anchor - Where to display within cell when component is smaller than it fill - How to size component when cell is larger than components requested size insets - External padding - min space between component and cell edges ipadx, ipady - Internal padding - What to add to min size of components weightx and weighty - How to distribute extra space (padding) gridwidth and gridheight - Number of columns or rows the component uses More explanation here: § http: //java. sun. com/docs/books/tutorial/uiswing/layout/gridbag. Constraints. html • Example explained very well here: w http: //java. sun. com/docs/books/tutorial/uiswing/layout/gridbag. Example. html 16 G 5 BUID - Java Swing - 2005
Card. Layout • Manages objects (usually JPanels) in sets • Works much like tabbed pane • Choose cards by w Asking for card in order added to container w Going backwards or forwards w Specifying card by name 17 G 5 BUID - Java Swing - 2005
Choosing layout managers (1) • In order to display a component in as much space as it can get, consider: w Border. Layout § Component in centre w Grid. Bag. Layout § fill=Grid. Bag. Constraints. BOTH w Box. Layout § Component specifies very large preferred/maximum sizes 18 G 5 BUID - Java Swing - 2005
Choosing layout managers (2) • To display a few components in a compact row: w JPanel’s default Flow. Layout w Box. Layout • Display a few components of the same size in rows and columns w Grid. Layout 19 G 5 BUID - Java Swing - 2005
Choosing layout managers (3) • Display a few components in a row or column, with different spacing between them and custom component sizes w Box. Layout • Display a complex layout that has many components w Grid. Bag. Layout w Using JPanel grouping and hierarchies 20 G 5 BUID - Java Swing - 2005
Layout managers - other layout features • Absolute positioning of components w When w How • Customising layout managers w When w How 21 G 5 BUID - Java Swing - 2005
Absolute positioning (1) • Don’t do it; unless… w component size isn’t affected by container size or font & look’n’feel changes § e. g. desktop panes containing internal frames w custom container performs size & position calculations particular to container § e. g. split panes 22 G 5 BUID - Java Swing - 2005
Absolute positioning (2) • Key points from None. Window. java w Instruct window to use no Layout: §. content. Pane. set. Layout(null); w Set components size and position with § XYZ. set. Bounds(x, y, width, height); w Set window size with: § window. set. Size(x, y); 23 G 5 BUID - Java Swing - 2005
Custom layout managers (1) • Ensure no existing manager does the job w Grid. Bag. Layout / Box. Layout w Layout manager downloads § If your trying to do it, chances are someone else has done it already… • DECLARE use of external code in coursework 24 G 5 BUID - Java Swing - 2005
Custom layout managers (2) • Create class which implements Layout Manager interface w e. g. public class my. Manager implements Layout. Manager • Must have 5 methods required by interface w w w • void add. Layout. Component(String, Component) void remove. Layout. Component(Component) Dimension preferred. Layout. Size(Container) Dimension minimum. Layout. Size(Container) void layout. Container(Container) See below URL for more documentation w http: //java. sun. com/docs/books/tutorial/uiswing/layout/custom. html 25 G 5 BUID - Java Swing - 2005
Summary • Creating a layout manager • Consulting managers • Types of managers w Border. Layout w Box. Layout w Flow. Layout • • Grid. Layout Grid. Bag. Layout Card. Layout Choosing managers Absolute positioning Custom layout managers Next time: Event handling and event listeners 26 G 5 BUID - Java Swing - 2005
- Slides: 26