Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Magercise 4:
Using Toggles

[Help | Solution | API Docs | Short Course | Magercises]

In this magercise, you will explore the JRadioButton, JCheckbox, and JScrollPane classes. Here, you'll create a basic pizza ordering program. The pizza parlor has three possible sizes (Small, Medium, and Large), as well as three possible toppings (Green Peppers, Pineapple, and Pepperoni). It's your job to show the options to the customer and let them select their order. Once an order is placed, a picture of the pizza ordered will be shown.

Magercise 4 Prerequisites

Buttons With Icons

Skeleton Code

Order.java

Resources

Image JAR File

The skeleton code contains a class to use as an Icon for the toppings, as well as the appropriate color definitions. It also handles the pizza ordering.

Tasks

  1. Save image JAR File to your working directory.

  2. Starting with the skeleton, create a ButtonGroup named sizeGroup with three JRadioButton objects in it. For the included event handling code to work without modifications, the sizeGroup variable needs to be final, while the button labels need to be "Small", "Medium", and "Large". Have the "Large" toggle initially selected. For each of the buttons, associate an ActionCommand of the text label. This will allow you to ask the ButtonGroup which JRadioButton is selected.

  3. Place the radio buttons within a vertical Box in the west area of the screen. Add a "Size" label to the top of the box. Have the box appear in the vertical-middle of the area with 5 pixels separating the items.

    The content pane for the JFrame can be referenced by the local variable content.

  4. For the toppings, create three JCheckbox items, named peppers, pepperoni, and pineapple with appropriate text labels for the three toppings. They should all start out unselected, and the variables all need to be final so the event handling code works unchanged.

  5. For each of the checkboxes, instead of using the default toggle box, create icons in the appropriate color for both the selected and unselected states. There is a support class provided, ColoredToggle to use to create each icon. Its constructor requires a color and the toggle state. There are three predefined colors that you can use:
    • pepperColor
    • pepperoniColor
    • pineappleColor

  6. Associate the icons to the appropriate JCheckbox objects. Use the setIcon() method to associate the icon for the unselected state and setSelectedIcon for the selected state icon.

  7. Place the checkboxes within a vertical Box in the east area of the screen. Add a "Toppings" label to the top of the box. Have the box appear in the vertical-middle of the area with 5 pixels separating the items.

  8. Create a JScrollPane with a JLabel for the center part of the screen. The label will be used to display the pizza image. It needs to be named imageLabel and be a final variable.

  9. The pizza ordering button is already added to the screen for you in the South quadrant, so save everything and compile the program. Then run it to see the results. Be sure to try all the different toppings since the pizza is free of calories.

    As a precaution, the Save command appends a "1" to the end of the filename you want to save. This can help prevent you from accidently overwriting your source code.

[Result.gif]

Where help exists, the task numbers above are linked to the step-by-step help page.

Pizza images used with permission from The Internet Pizza Server.

The following JavaTM source file represents a solution to this Magercise.

Solution

Magercise 5

Short Course

About This Short Course

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 19-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.