12.5 Text Fields and Command Buttons

Let’s illustrate event handling and the Model-View-Controller architecture with an example that allows a user to square or cube a number. We provide a TextField for entering the number; two Buttons, one for squaring the number and the other for cubing the number; and Labels for displaying a prompt and for displaying the squared or cubed result.

The interface we want to create is shown in Figure 12.5A. Figure 12.5B shows the window after the user entered “3” into the TextField and clicked the “Cube” button.

For this application, our Model, shown in Example 12.5, is minimal. It provides two static methods, square (lines 7–14) and cube (lines 16–23), to perform the calculations.

Figure 12.5(A) The Initial ...

Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.