Copyright Curt Hill GridBag Layout Manager A flexible but complicated layout
Copyright Curt Hill Is this the best or worst? Very flexible and complicated Allows different sized components Each component has constraints which determine its positioning It allows merging of cells Most of the work is done by an object of GridBagConstraints When it does not work it is seldom clear why
Copyright Curt Hill GridBag Has only default constructor The number of rows and columns is determined by the constraints of the added components Each component occupies a rectangle of cells The adds are somewhat different than expected
Copyright Curt Hill Adding to the layout manager Three steps: Establish the constraints –This is most of the work Use the setConstraints –Connects the constraints and component Do the add –Add has only parameter: a component
Copyright Curt Hill GridBagConstraints An object that effectively describes the formatting of the component Specifies –x,y positions –sizes –Alignment –filling
Copyright Curt Hill GridBagConstraints properties Unlike very many objects most properties are public, rather than private or protected Thus we can merely do assignments on them before executing the setConstraints method The properties usage follows
Copyright Curt Hill Position Properties int gridx –Position of left portion –Zero is leftmost int gridy –Position of top –Zero is top These determine the upper left corner of the component’s rectangle
Copyright Curt Hill Size Properties int gridwidth and gridheight –Determines the number of rows and columns in this component fill – enlarge to limits of the rectangle –Set to one of four constants HORIZONTAL VERTICAL NONE – no filling BOTH – fill in both directions
Copyright Curt Hill Other padding properties insets, a class of four integers This specifies external padding This is the padding between the component and its rectangular boundary The Inset has the following constructor parameters: –(int top, int left, int bottom, int right)
Copyright Curt Hill Alignment property Anchor –How to place an item that is smaller than its rectangle May be one of several constants: –CENTER, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, and NORTHWEST