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
 

Help is available for each task, or you can go straight to the solution source code.

Task 1

Save image JAR File to your working directory.

The file must be in the same directory as your program.

Task 2

Starting with the skeleton, create a ButtonGroup named sizeGroup with three JRadioButton objects in it. So the included event handling code works 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.
final ButtonGroup sizeGroup = new ButtonGroup();
JRadioButton small = 
  new JRadioButton("Small", false);
small.setActionCommand("Small");
sizeGroup.add (small);
JRadioButton medium =
  new JRadioButton("Medium", false);
medium.setActionCommand("Medium");
sizeGroup.add (medium);
JRadioButton large = 
  new JRadioButton("Large", true);
large.setActionCommand("Large");
sizeGroup.add (large);

Task 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.

Box sizes = Box.createVerticalBox();
sizes.add(Box.createGlue());
sizes.add (new JLabel ("Size:"));
sizes.add (Box.createVerticalStrut (5));
sizes.add (small);
sizes.add (Box.createVerticalStrut (5));
sizes.add (medium);
sizes.add (Box.createVerticalStrut (5));
sizes.add (large);
sizes.add(Box.createGlue());
content.add (sizes, BorderLayout.WEST);

Task 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.
final JCheckBox peppers = new JCheckBox(
             "Green Peppers", false);
             
final JCheckBox pepperoni = new JCheckBox(
             "Pepperoni", false);
             
final JCheckBox pineapple = new JCheckBox(
             "Pineapple", false);

Task 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
Icon peppersUnselected   = new ColoredToggle (
                               pepperColor, 
                               false);
Icon peppersSelected     = new ColoredToggle (
                               pepperColor, 
                               true);
Icon pepperoniUnselected = new ColoredToggle (
                               pepperoniColor, 
                               false);
Icon pepperoniSelected   = new ColoredToggle (
                               pepperoniColor, 
                               true);
Icon pineappleUnselected = new ColoredToggle (
                               pineappleColor, 
                               false);
Icon pineappleSelected   = new ColoredToggle (
                               pineappleColor, 
                               true);

Task 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.
peppers.setIcon(peppersUnselected);
peppers.setSelectedIcon(peppersSelected);
pepperoni.setIcon(pepperoniUnselected);
pepperoni.setSelectedIcon(pepperoniSelected);
pineapple.setIcon(pineappleUnselected);
pineapple.setSelectedIcon(pineappleSelected);

Task 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.

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

Box toppings = Box.createVerticalBox();
toppings.add(Box.createGlue());
toppings.add (new JLabel ("Toppings:"));
toppings.add (Box.createVerticalStrut (5));
toppings.add (peppers);
toppings.add (Box.createVerticalStrut (5));
toppings.add (pepperoni);
toppings.add (Box.createVerticalStrut (5));
toppings.add (pineapple);
toppings.add(Box.createGlue());
content.add (toppings, BorderLayout.EAST);

Task 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.

Place the label in the scroll pane with the JScrollPane constructor.

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

final JLabel imageLabel = new JLabel();
JScrollPane imagePanel = new JScrollPane(imageLabel);
content.add (imagePanel, BorderLayout.CENTER);

Task 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.

javac Order.java
java Order

If you are using Swing with JDK 1.1.+, make sure your CLASSPATH environment variable has the swingall.jar file in it.

Return to Magercise 4

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.