Page 748 - Introduction to Programming with Java: A Problem Solving Approach
P. 748
714 Chapter 17 GUI Programming—Component Layout, Additional GUI Components
cell’s container uses a center-aligned FlowLayout manager. The right cells’ containers use right-aligned FlowLayout managers. Voila, layout managers inside a layout manager. Pretty cool, eh?
When you have a non-trivial window, it’s very common to have embedded layout managers. And when that happens, it can take a considerable amount of tweaking to get your windows to look right. Despite the tweaking, using embedded layout managers is still a lot easier than having to manually position components with pixel values like in the old days. The next section provides details on the containers for the embedded layout managers.
17.9 JPanel class
Before continuing with the implementation of the math-calculator program, we need to discuss the JPanel class. A JPanel container object is a generic storage area for components. If you have a complicated window with many components, you might want to compartmentalize the components by putting groups of components in JPanel containers. JPanel containers are particularly useful with GridLayout and BorderLayout windows because each compartment in those layouts can store only one component. If you need a compartment to store more than one component, let that one component be a JPanel container, and put multiple components into the JPanel container.
Implementation
As you may recall, GUI classes that begin with J come from the javax.swing package. So that’s where the JPanel container class comes from, and you need to import the javax.swing package in order to
Apago PDF Enhancer
use Jpanel.
To instantiate a JPanel container, use this syntax:
JPanel <JPanel-reference> = new JPanel(<layout-manager>);
The layout-manager argument is optional. If it’s omitted, then the default is to have a center-aligned FlowLayout manager.
So the JPanel container’s default layout manager is FlowLayout. Quick quiz: Do you remember the JFrame container’s default layout manager? It’s BorderLayout. That should make sense when you real- ize that JFrame containers are designed to handle the window as a whole. For the window as a whole, the default BorderLayout scheme works well because its report-oriented regions (north for a header, south for a footer, center for a main body) match the needs of many program windows. On the other hand, JPanel containers are designed to handle compartments within a window. For such compartments, the default FlowLayout scheme works well because its free-form flow matches the needs for many compartments.
Adding Components to a JPanel
After instantiating a JPanel, you’ll want to add components to it. Adding components to a JPanel is the same as adding components to a JFrame. Call the add method. As you know, the add method works differently for the different layout managers. If your JPanel uses a FlowLayout manager or a GridLayout manager, call the add method like this:
<JPanel-reference>.add(<component>);
If your JPanel uses a BorderLayout manager, you should add a second argument to specify the com- ponent’s region: