Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Checbox help!!!

Thread view: 
abc4616 - 27 Sep 2006 04:31 GMT
I am trying to write a program that calculates the price when the checkboxes
are selected and the calculate button is pressed.
Now I am not sure how to code the part where you click the box and the price
of that item appears. Any suggestions?

Here is what I have now:

import java.awt.*; //Abstract Window Toolkit (AWT) for creating
                    //painting graphics, interfaces and images
import java.applet.*; //gives the classes for an applet to create and
                    //communicate in applet context
import java.awt.event.*; //gives classes and interface for different
                    //types of events from the AWT
import java.text.DecimalFormat; //this class formats decimal numbers

public class PetsApplet extends Applet implements ActionListener
{
    //labels declared
    Label welcomeLabel = new Label ("Welcome to the Pet Clinic");
    Label price = new Label("The total price is $0");
    Label current = new Label("The current price is $0");
    Label serLabel = new Label("Select the service:");

    //string text declared
    String currentString = new String();
    String priceString = new String();

    //checkboxes identified
    Checkbox officeBox = new Checkbox("Office Visits $10");
    Checkbox vacBox = new Checkbox("Vaccination $25");
    Checkbox xrayBox = new Checkbox("X-Rays $40");
    Checkbox hosBox = new Checkbox("Hospitalization $100");
    Checkbox denBox = new Checkbox("Dentistry $50");
    Checkbox labBox = new Checkbox("Laboratory Work $30");
    Checkbox heartBox = new Checkbox("Heartworm Prevention $35");
    Checkbox boardBox = new Checkbox("Boarding $5");
    Checkbox preBox = new Checkbox("Prescription $55");
    Button calcButton = new Button ("Calculate");
    Label outputLabel = new Label("Click the calculate button for total price");

    //price variables of the services declared along with the totalPrice
    int totalPrice = 0;
    int officeBoxPrice = 10, vacBoxPrice = 25, xrayBoxPrice = 40, hosBoxPrice =
100;
    int denBoxPrice = 50, labBoxPrice = 30, heartBoxPrice = 35, boardBoxPrice =
5, preBoxPrice = 55;

    public void init()
    {
        //adds all the components to the applet interface
        setBackground (Color.blue);
        add(welcomeLabel);
        add(serLabel);
        add(officeBox);
        add(vacBox);
        add(xrayBox);
        add(hosBox);
        add(denBox);
        add(labBox);
        add(heartBox);
        add(boardBox);
        add(preBox);
        add(current);
        add(calcButton);
        calcButton.addActionListener(this);
        add(outputLabel);
        add(price);
    }

    public void actionPerformed(ActionEvent e)
    {
            //the current price statement is identified
            if (officeBox.getState())

            currentString = ("The current price is $"+ Integer.toString
(officeBoxPrice));

            if (vacBox.getState())

            currentString = ("The current price is $"+ Integer.toString (vacBoxPrice))
;

            if (xrayBox.getState())

            currentString = ("The current price is $"+ Integer.toString (xrayBoxPrice))
;

            if (hosBox.getState())

            currentString = ("The current price is $"+ Integer.toString (hosBoxPrice))
;

            if (denBox.getState())

            currentString = ("The current price is $"+ Integer.toString (denBoxPrice))
;

            if (labBox.getState())

            currentString = ("The current price is $"+ Integer.toString (labBoxPrice))
;

            if (heartBox.getState())

            currentString = ("The current price is $"+ Integer.toString (heartBoxPrice)
);

            if (boardBox.getState())

            currentString = ("The current price is $"+ Integer.toString (boardBoxPrice)
);

            if (preBox.getState())

            currentString = ("The current price is $"+ Integer.toString (preBoxPrice))
;

            //the text is set equal to currentString
            current.setText (currentString);

            //total price is calculated
            totalPrice = 0;

                if (officeBox.getState())
                totalPrice += officeBoxPrice;

                if (vacBox.getState())
                totalPrice += vacBoxPrice;

                if (xrayBox.getState())
                totalPrice += xrayBoxPrice;

                if (hosBox.getState())
                totalPrice += hosBoxPrice;

                if (denBox.getState())
                totalPrice += denBoxPrice;

                if (labBox.getState())
                totalPrice += labBoxPrice;

                if (heartBox.getState())
                totalPrice += heartBoxPrice;

                if (boardBox.getState())
                totalPrice += boardBoxPrice;

                if (preBox.getState())
                totalPrice += preBoxPrice;

                //output statement for total price is identified
                priceString = ("The total price is $" + Integer.toString (totalPrice));
                price.setText (priceString);

                //statements to clear the fields after the calculate button is clicked
                officeBox.setState(false);
                vacBox.setState(false);
                xrayBox.setState(false);
                hosBox.setState(false);
                denBox.setState(false);
                labBox.setState(false);
                heartBox.setState(false);
                boardBox.setState(false);
                preBox.setState(false);

    }
}
Andrew Thompson - 27 Sep 2006 06:26 GMT
> I am trying to write a program that calculates the price when the checkboxes
> are selected and the calculate button is pressed.

The applet* worked as advertised for me.

When I selected some of the checkboxes, and clicked the button,
the total was displayed (and the checkboxes cleared).

> Now I am not sure how to code the part where you click the box and the price
> of that item appears.

What is supposed to happen?

As a few asides.
- * applets are a terrible place to start GUI devlopment, they are
much harder to debug and deploy than applications.  If the applet
is a requirement of the assignment, please impress that upon the
instructor.  If the instructor show any resistance to replacing
appletswith applications, ask them to post the reasons here,
so we can discuss them further.
- Please use smaller code indents than 8 chars when posting to
usenet, and try to keep total line width to less than 64 chars wide,
as some new readers will have wrapped by that stage.

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.