Lesson Fifteen

Using AWT Component methods


Now that we can create Frames and applets that use Frames, the other components that we can use in Frames and applets. One popular type of component is the Checkbox. Checkboxes are input boxes with labels. You can type the label as you create the checkbox (i.e. Checkbox boxOne = new Checkbox("Click here to select");), or you can add one later with setLabel() (i.e. boxOne.setLabel("Select this box");. Below is a program that shows how checkboxes work.

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class CarOptions extends Applet implements ItemListener
{
String companyName = new String("North Star Ford");
Font bigFont = new Font("Arial", Font.PLAIN, 24);
Checkbox stereoBox = new Checkbox("AM/FM stereo");
Checkbox seatsBox = new Checkbox("bucket seats");
Checkbox acBox = new Checkbox("air conditioning");
Checkbox roofBox = new Checkbox("sun roof");
int stereoPrice = 200, seatsPrice = 300, acPrice = 600, roofPrice = 1000, totalPrice = 0;
public void init()
{
add(stereoBox);
stereoBox.addItemListener(this);
add(seatsBox);
seatsBox.addItemListener(this);
add(acBox);
acBox.addItemListener(this);
add(roofBox);
roofBox.addItemListener(this);
}
public void paint(Graphics gr)
{
gr.setFont(bigFont);
gr.setColor(Color.blue);
gr.drawString(companyName,10,100);
gr.drawString("Options price estimate",10,150);
gr.setColor(Color.black);
gr.drawString(Integer.toString(totalPrice),280,150);
}
public void itemStateChanged(ItemEvent check)
{
totalPrice = 0;
if(stereoBox.getState())
{
totalPrice += stereoPrice;
}
if(seatsBox.getState())
{
totalPrice += seatsPrice;
}
if(acBox.getState())
{
totalPrice += acPrice;
}
if(roofBox.getState())
{
totalPrice += roofPrice;
}
repaint();
}
}

To see how this program works, click here and try clicking each box on and off to see the cost of the options change as you select different options.

Checkboxes are good for giving the user multiple options. However, in the previous program, you can see that you can select as many as you want. What if you only wanted the user to select one option? This is where the CheckboxGroup (also known as "radio buttons") come into play. Consider the program below:

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class DemoCheckBoxGroup extends Applet implements ItemListener
{
String companyName = new String("Event Handlers Incorporated");
Font bigFont = new Font("Arial", Font.PLAIN, 24);
Checkbox cocktailBox = new Checkbox("Cocktails");
Checkbox dinnerBox = new Checkbox("Dinner");
int cocktailPrice = 300, dinnerPrice = 600, totalPrice = 200;
int beefPrice = 100, fishPrice = 75;
CheckboxGroup dinnerGrp = new CheckboxGroup();
Checkbox chickenBox = new Checkbox("Chicken", false, dinnerGrp);
Checkbox beefBox = new Checkbox("Beef", false, dinnerGrp);
Checkbox fishBox = new Checkbox("Fish", false, dinnerGrp);
public void init()
{
add(cocktailBox);
cocktailBox.addItemListener(this);
add(dinnerBox);
dinnerBox.addItemListener(this);
add(chickenBox);
chickenBox.addItemListener(this);
add(beefBox);
beefBox.addItemListener(this);
add(fishBox);
fishBox.addItemListener(this);
}
public void paint(Graphics gr)
{
gr.setFont(bigFont);
gr.setColor(Color.magenta);
gr.drawString(companyName,10,100);
gr.drawString("Event price estimate",10,150);
gr.setColor(Color.blue);
gr.drawString(Integer.toString(totalPrice),280,150);
}
public void itemStateChanged(ItemEvent check)
{
totalPrice = 200;
if(cocktailBox.getState())
{
totalPrice += cocktailPrice;
}
if(dinnerBox.getState())
{
totalPrice += dinnerPrice;
Checkbox dinnerSelection = dinnerGrp.getSelectedCheckbox();
if(dinnerSelection == beefBox)
totalPrice += beefPrice;
else if(dinnerSelection == fishBox)
totalPrice += fishPrice;
else
chickenBox.setState(true);
}
repaint();
}
}

This program starts your party bill out at a base price of $200.00. If you click on "Cocktails", it adds $300.00 to the cost of the party. If you were to click on "Dinner", it adds $600.00 to the cost and the "Chicken" radio button is selected. To see how the whole program functions, click here. Sometimes radio buttons can be too many and clutter up the viewing area. This is where the Choice object would be an option. Like a "list box", the Choice object displays a list of options that the user can choose from. To allow for these options, the Choice object uses the same "get()" methods (i.e. getSelectedIndex()). With these methods, you can access arrays using variables created by the methods. To see how this is done, let's look at the following program:

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class DemoChoice extends Applet implements ItemListener
{
String companyName = new String("Event Handlers Incorporated");
Font bigFont = new Font("Arial", Font.PLAIN, 24);
int totalPrice = 0;
Choice entertainmentChoice = new Choice();
int[] actPrice = {0,725,325,125};
public void init()
{
add(entertainmentChoice);
entertainmentChoice.addItemListener(this);
entertainmentChoice.add("No entertainment");
entertainmentChoice.add("Rock band");
entertainmentChoice.add("Pianist");
entertainmentChoice.add("Clown");
}
public void paint(Graphics gr)
{
gr.setFont(bigFont);
gr.setColor(Color.magenta);
gr.drawString(companyName,10,100);
gr.drawString("Price for entertainment",10,150);
gr.setColor(Color.blue);
gr.drawString(Integer.toString(totalPrice),280,150);
}
public void itemStateChanged(ItemEvent choice)
{
int actNum = entertainmentChoice.getSelectedIndex();
totalPrice = actPrice[actNum];
repaint();
}
}

Now, instead of a clutter of radio buttons, you have one list. Here's how your program should look. If you'll notice, there's one minor problem, you can only choose one choice at a time. To remedy this, we can create a List. Similar to a Choice object, you can choose from an array. A List, however, displays all options at one time instead of a "drop-down" box. You can also choose multiple options from this List. To see how this is done, let's look at the program below:

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class DemoList extends Applet implements ItemListener
{
String companyName = new String("Event Handlers Incorporated");
Font bigFont = new Font("Arial", Font.PLAIN, 24);
int totalPrice = 0;
List partyFavorList = new List();
int[] favorPrice = {8,10,25,35};
public void init()
{
add(partyFavorList);
partyFavorList.setMultipleMode(true);
partyFavorList.addItemListener(this);
partyFavorList.add("Hats");
partyFavorList.add("Streamers");
partyFavorList.add("Noise makers");
partyFavorList.add("Balloons");
}
public void paint(Graphics gr)
{
gr.setFont(bigFont);
gr.setColor(Color.magenta);
gr.drawString(companyName,10,100);
gr.drawString("Price for entertainment",10,150);
gr.setColor(Color.blue);
gr.drawString(Integer.toString(totalPrice),280,150);
}
public void itemStateChanged(ItemEvent check)
{
int[] favorNums = partyFavorList.getSelectedIndexes();
totalPrice = 0;
for(int x = 0; x < favorNums.length; ++x)
totalPrice += favorPrice[favorNums[x]];
repaint();
}
}

Here's the final product. Notice now that you select as many options as you want from a neat looking list.

Question: Ok, wait a minute! In HTML, I can create a form a lot easier and I can place things where I want. I can even do mouse events with JavaScript. How do you get the same outcome from Java?

Answer: True. You can do all of what you said with greater ease than with Java. But you are here to learn how you can do the SAME thing using Java. To show you how to make things look and work similar, let's look at the layout managers and the event model.


Lesson Fourteen | Home | Lesson Sixteen

If you have a question about any of the lessons, feel free to ask.

1