Page 763 - Introduction to Programming with Java: A Problem Solving Approach
P. 763
17.15 Job Application Example
In this section, we put into practice what you’ve learned in the previous three sections. We present a com- plete program that uses check boxes, radio buttons, and a combo box. The program implements a job ap- plication form. If the user enters values that are indicative of a good employee, the program displays an encouraging message (“Thank you for your application submission. We’ll contact you after we process your information.”). Study the sample session in Figure 17.16 to get a better idea of what we’re talking about.
See the JobApplication program listing in Figures 17.17a, 17.17b, and 17.17c. You should peruse the en- tire program on your own, particularly the listener code, but we’ll focus only on the most difficult part—the layout design.
We spent quite a bit of time on the JobApplication’s layout in order to get things to look right. Ini- tially, we thought a simple one-column GridLayout manager would work. We added one component per cell, and we added three filler components (empty JLabels) to create gaps between the four different input areas. We thought that plan would yield the layout shown in Figure 17.18’s left picture. Unfortu-
nately, when we entered the code, the actual program yielded the layout shown in Figure 17.18’s
right picture. There are three problems with the actual layout—the Submit button is too wide,
the top two gaps are missing, and the components are touching the left boundary. We’ll now
discuss how to fix those problems.
Problem 1: Submit Button is Too Wide
As you may recall from earlier in the chapter, buttons expand if they’re added directly to a GridLayout cell.
17.15 Job Application Example 729
If at first you don’t succeed, try again.
That explains the wide Submit button. You can fix the problem by embedding a FlowLayout Apago PDF Enhancer
panel into the Submit button’s area, and then adding the Submit button to the FlowLayout panel. With a FlowLayout manager, buttons don’t expand; they keep their natural size.
Problem 2: Top Two Gaps are Missing
In our first cut of the program, we used this code to implement the filler components:
JLabel filler = new JLabel();
...
add(filler);
...
add(filler);
...
add(filler);
Embed another manager.
We instantiated only one label and reused it three times. You like to reuse, right? Well, the layout manager doesn’t. The layout manager sees only one object and so it makes only one cell. It does not
make cells for the first two add(filler) calls; it only makes a cell for the last add(filler) call. You can fix the problem by using three anonymous JLabel objects like this:
add(new JLabel());
...
add(new JLabel());
...
Use separate objects.
add(new JLabel());