CS 235 - Week 8 Lecture - 2015-10-14
********
* BorderLayout note --
* we've used the absolute positioning constants:
NORTH SOUTH EAST WEST
...there are also relative positioning constraints:
(based on default container orientations):
PAGE_START PAGE_END LINE_END LINE_START
********
* today:
* BoxLayout
* using NO layout manager
* (and MAYBE talking a little about GridBagLayout)
********
* BoxLayout
* a simple but versatile "special purpose" layout manager
that arranges its children components in a row or
a column
..it adds the ability to add glue ("stretchy space")
and rigid areas to the layout
* BoxLayout can be used with JPanel,
but it is more often used with a new container
called a Box
* in addition to constructors, the Box class has
static methods for creating
a vertical or horizontal box
createHorizontalBox
createVerticalBox
Box topRowBox = Box.createHorizontalBox();
Box lineBeginBox = Box.createVerticalBox();
* these Box instances have an add method,
adds the desired components in the order
they are added
* to create horizontal or vertical glue
instances:
Box.createHorizontalGlue()
Box.createVerticalGlue()
* to create rigid areas, you call
the Box class static method
Box.createRigidArea, with a Dimension argument--
(width and height as integers,
here treated as pixel measures)
Box.createRigidArea(new Dimension(50, 200));
********
* what about having NO layout manager for a container?
* sometimes called "absolute positioning"
* FIRST: need to specify that the container
in question have NO layout manager:
setLayout(null);
* then, for each component you are adding,
add(desiredComponent);
and call a method (of the newly-added component!)
setBounds, that expects 4 integers:
x
y coordinates of the TOP LEFT CORNER of
the component
width
height new SIZE of the component
********
* to write your OWN layout manager?
...write a class implementing the interface
LayoutManager or LayoutManager2!
(and the 5 or 10 methods required therein)
see "Core Java" for an example;
********
* let's START talking about GridBagLayout
* creates a flexible grid in which columns and
rows can have variable sizes and components
can overlap more than one grid cell
* how? using a GridBagConstraints object;
* suggested approach (from the course text)
* sketch out the layout you want, and determine
the underlying grid;
* label the rows and columns starting with 0;
* create an object of type GridBagLayout
(it GUESSES the number of rows and columns!!)
make it the layout manager for the desired
container
* for each component, create an object of
type GridBagConstraints,
and add the component using the call:
add(aComponent, itsConstraints)
* what are the constraints data fields, then?
gridx - the column in which the upper-left corner
of the component will be placed
gridy - the row in which the upper-left corner
of the component will be placed
gridwidth - the number of columns the component occupies
gridheight - the number of rows the component occupies
weightx
weighty
...do you want the components to get wider/narrower
when there is room?
fill
anchor
ipadx
ipady
insets (of type Insets)