Introduction |
In this lecture we will examine four different layout managers; each manages
the layout (relative sizes and positions) of any components added into the
view of a GUI application (every layout manager is associated with one
JPanel).
The four clases are FlowLayout (the simplest of the four, but not used
much in fancier GUIs),
BoxLayout (for stacking components horizontally or vertically),
GridLayout (for making 2-d grids of equal-sized component -often
buttons),
and BorderLayout (the default for the content pane of a
JFrame, with positioning in the center, and the four compass
directions).
We will also discuss how to create a variety of control components, change their visual attributes, and place them in the view. We will not yet explore how to associate action with these components: we will use these components in views for their visual properties only. In addition, we will explore layout managers in more detail by discussing how to write our own simple layout managers. Finally, we will examine the use of structuring nested JPanels, each with layout managers, to build a hierarchical view of arbitrary complexity. There is a Layout Manager Demos that we can use to explore all of these concepts. We can also easily change the code it contains to experiment with other layout manager combinations. Please download and unzip this file. |
Viewing Control Components |
Control components have two aspects: their visual aspect -how they appear on
the screen (colors, fonts, etc)- and their control aspect -how we can
process events linked to them (pressing buttons, etc).
For example, it is very easy to construct JButton which appears on the
screen as a button icon with the appropriate (optional inner icon) and text.
The background of the button can be set to any color; the text can be written
in any color, with any font.
The button can be disabled: it shows in a slightly washed out color, and
cannot be pressed (doesn't process the pressed event); it can enabled at
any later time, and disabled again, etc.
In this lecture we will be concerned solely with the visual aspect of a control component; in the next lecture we will focus on their control aspect. This final GUI testbed application automatically constructs and allows us to use a variety of control components.
Each has many constructors. The most useful constructor for each has a single String parameter, which is the text shown in/with the control; actually, the most useful constructor for JTextField specifies only an int, specifying an empty text field with that many columns (spaces for characters). Just as in the paintComponent method that we explored in the previous lecture, we can set various visual aspects of most of these control components: their foreground and background color, their font (if they are textual), and more specialized information (such as whether a JTextField is editable). As a quick sidenote, an object from the ButtonGroup class can have a variety of control components (like JCheckBox and JRadioButton) added to it. It implements an EXCLUSIVE selection mechanism: whenever one of these control components is set, the others are automatically unset. The reason this class is explored here is that we can see the visual aspect of this grouping. |
Stucturing Complex Views with Recursive JPanels |
Besides adding control components to containers, we can also add other
containers to containers (because Container extends Component).
Thus, we can design a GUI to consist of JPanels (each containing a
layout manager for the control components added to it) nested inside other
JPanels (containg their own layout managers), making the GUI look any
way we wish.
When Java calls a container's (e.g., JPanel's) paint method, it
automatically calls the paint methods of all the components it
contains (under the sizing/placement control of its layout manager).
For example, examine the following complicated GUI (ignore the special background/color/font information) taken from the program to download for this lecture.
At the top level, its content pane is composed of a BorderLayout that has information added to its north, center, and south (nothing has been added on its east or west).
|
The Recursive Layout Project | We will go over in class the code for the GUI above; we will also see how we can make changes to it and generally experiment with the components supplied. It is in the Layout Manager Demos, in the project folder named recursive layout |
Writing the TwoColumnLayout Manager |
To explore layout managers further, let's examine one that handles the layout
for containers that have two columns: the left column contains labels and
the right contains text fields (they should always be added in pairs).
The labels should be right justified, and the text fields should be left
justified.
An example use of this layout manager appears below.
Basically, this manager computes a preferred size as follows. Each JLabel and JTextField has its own preferred size. The manager gets all of the components that is in the container that it manages and then loops through each of these components, accumulating the widest JLabel and widest JTextField: their sum is the preferred width of the entire container (with the horizontal gap added in as well). It also accumulates the height of the container, by adding together the biggest height of each label-text field pair (also adding the vertical gap between each pair). Besides returning the Dimension, it stores into its instance variables the widest JLabel and widest JTextField. It lays out all these components using a very similar method. First it calls preferredLayoutSize to ensure the widest JLabel and widest JTextField are computed and stored into instance variables. Then it loops through each of its components, keeping track of where the top-left corner of each label-text field pair should appear; with these numbers it computes the eact positions of the label (right justifying it) and the field (left justtifying it). The mathematics is a bit convluted, but not too complicated. We will go over in class the code for this class and its driver. It is in the Layout Manager Demos, in the project folder named TwoColumn Layout. |
Problem Set |
To ensure that you understand all the material in this lecture, please solve
the the announced problems after you read the lecture.
If you get stumped on any problem, go back and read the relevant part of the lecture. If you still have questions, please get help from the Instructor, a CA, or any other student.
|