Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: program wont renew total

  1. #1
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Exclamation program wont renew total

    ok so heress my code... it looks the way its supposed to (except that it shows 1 zero instead of 2 while total is 0 so any additional help with that is appreciated)... it compiles correctly... but when i click on the checkboxes, the total doesnt change which is really irritating cause once this minor problem is out of the way the code is finished...

    <html>
        <title>The Dinner Menu</title>
        <p align = "center">
        <applet code = "DinnerMenu.class" width = "200" height = "440">
        </applet>
        </p>
    </html>

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class DinnerMenu extends Applet implements ItemListener
    {
        int dollar = 0;
        int cents = 00;
     
        Label total = new Label("Dinner Price is: $" + dollar + (".") + cents);
     
        Label dm = new Label("  Dinner Menu");
        Label instruct = new Label("   Please select one item from");
        Label instruct2 = new Label("  each category. Your total will");
        Label instruct3 = new Label("          be displayed below.");
        Label soupl = new Label("Soups*************************");
        Label entreel = new Label("Entrees***********************");
        Label dessertl = new Label("Desserts*********************");
     
        soup watery = new soup();
        entree meal = new entree();
        dessert sweet = new dessert();
     
        Font ft = new Font("Arial", Font.PLAIN, 14);
        Font bigft = new Font("Arial", Font.PLAIN, 28);
        Font boldft = new Font("Arial", Font.BOLD, 16);
     
     
     
        public void init ()
        {
            setLayout(new GridLayout(0,1));
            setBackground(Color.pink);
     
            dm.setFont(bigft);
            add(dm);
     
            this.setFont(ft);
            add(instruct);
            add(instruct2);
            add(instruct3);
     
            soupl.setFont(boldft);
            add(soupl);
     
            this.add(watery);
            watery.clam.addItemListener(this);
            watery.veg.addItemListener(this);
            watery.broth.addItemListener(this);
     
            entreel.setFont(boldft);
            add(entreel);
     
            this.add(meal);
            meal.chic.addItemListener(this);
            meal.fish.addItemListener(this);
            meal.beef.addItemListener(this);
     
            dessertl.setFont(boldft);
            add(dessertl);
     
            this.add(sweet);
            sweet.van.addItemListener(this);
            sweet.rice.addItemListener(this);
            sweet.cake.addItemListener(this);
     
            total.setFont(boldft);
            add(total);
        }
     
     
     
        public void itemStateChanged (ItemEvent e)
        {
            int dollarw = watery.getchangedollar();
            int centsw = watery.getchangecent();
     
            int dollarm = meal.getchangedollar();
            int centsm = meal.getchangecent();
     
            int dollars = sweet.getchangedollar();
            int centss = sweet.getchangecent();
     
            dollar = (dollarw + dollarm + dollars);
            cents = (centsw + centsm + centss);
     
            invalidate();
            validate();
            /*remove(total);
            total.setFont(boldft);
            add(total);
            repaint();*/
        }
     
        public void paint(Graphics g)
        {
     
        }
    }

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class soup extends Panel implements ItemListener
    {
        CheckboxGroup soups = new CheckboxGroup();
        Checkbox clam = new Checkbox("Clam Chowder",soups,false);
        int s11 = 2;
        int s12 = 79;
        Checkbox veg = new Checkbox("Vegetable Soup",soups,false);
        int s21 = 2;
        int s22 = 99;
        Checkbox broth = new Checkbox("Peppered Chicken Broth",soups,false);
        int s31 = 2;
        int s32 = 49;
     
        Font ft = new Font("Arial", Font.PLAIN, 14);
     
        public soup ()
        {
            this.setVisible(true);
            setLayout(new GridLayout(0,1));
            setBackground(Color.pink);
            setFont(ft);
            add(clam);
            add(veg);
            add(broth);
        }
     
        public int getchangedollar()
        {
            int x=0;
     
            if (clam.getState()==true)
                x=s11;
            else if (veg.getState()==true)
                x=s21;
            else if (broth.getState()==true)
                x=s31;
     
            return x;
        }
     
        public int getchangecent()
        {
            int x=0;
     
            if (clam.getState()==true)
                x=s12;
            else if (veg.getState()==true)
                x=s22;
            else if (broth.getState()==true)
                x=s32;
     
            return x;
        }
     
        public void itemStateChanged(ItemEvent e)
        {
        }
    }

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class entree extends Panel
    {
        CheckboxGroup ent = new CheckboxGroup();
        Checkbox chic = new Checkbox("Chicken",ent,false);
        int e11 = 12;
        int e12 = 79;
        Checkbox fish = new Checkbox("Fish",ent,false);
        int e21 = 10;
        int e22 = 99;
        Checkbox beef = new Checkbox("Beef",ent,false);
        int e31 = 14;
        int e32 = 99;
     
        Font ft = new Font("Arial", Font.PLAIN, 14);
     
        public entree ()
        {
            this.setVisible(true);
            setLayout(new GridLayout(1,0));
            setBackground(Color.pink);
            setFont(ft);
            add(chic);
            add(fish);
            add(beef);
        }
     
        public int getchangedollar()
        {
            int x=0;
     
            if (chic.getState()==true)
                x=e11;
            else if (fish.getState()==true)
                x=e21;
            else if (beef.getState()==true)
                x=e31;
     
            return x;
        }
     
        public int getchangecent()
        {
            int x=0;
     
            if (chic.getState()==true)
                x=e12;
            else if (fish.getState()==true)
                x=e22;
            else if (beef.getState()==true)
                x=e32;
     
            return x;
        }
    }

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class dessert extends Panel
    {
        CheckboxGroup dessert = new CheckboxGroup();
        Checkbox van = new Checkbox("Vanilla Ice Cream",dessert,false);
        int d11 = 2;
        int d12 = 79;
        Checkbox rice = new Checkbox("Rice Pudding",dessert,false);
        int d21 = 2;
        int d22 = 99;
        Checkbox cake = new Checkbox("Cheesecake",dessert,false);
        int d31 = 4;
        int d32 = 29;
     
        Font ft = new Font("Arial", Font.PLAIN, 14);
     
        public dessert ()
        {
            this.setVisible(true);
            setLayout(new BorderLayout());
            setBackground(Color.pink);
            setFont(ft);
            add(van, "North");
            add(rice, "West");
            add(cake, "East");
        }
     
        public int getchangedollar()
        {
            int x=0;
     
            if (van.getState()==true)
                x=d11;
            else if (rice.getState()==true)
                x=d21;
            else if (cake.getState()==true)
                x=d31;
     
            return x;
        }
     
        public int getchangecent()
        {
            int x=0;
     
            if (van.getState()==true)
                x=d12;
            else if (rice.getState()==true)
                x=d22;
            else if (cake.getState()==true)
                x=d32;
     
            return x;
        }
    }

    the total is supposed to change after every time you click a checkbox

    thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: program wont renew total

    I believe you wish to have ItemListener's added to the checkboxes? See How to Write an ItemListener

  3. #3
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: program wont renew total

    ok heres what i dont get...

    i set up the checkboxes as itemlisteners in the calling program as:

    watery.clam.addItemListener(this);

    where watery is the object and clam is the checkbox itself with this referring to the applet itself

    do i have to register them as item listeners in their original class too? if so what would i need to put in that classes itemStateChanged method if anything?

  4. #4
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: program wont renew total

    ok so i added the following code after i added them to the applet in the init() method in the DinnerMenu class:

    watery.clam.addItemListener(this);
    watery.veg.addItemListener(this);
    watery.broth.addItemListener(this);
    ... // and i did the same to all the other checkboxes

    then to the DinnerMenu class's itemStateChanged() method i changed the following

    public void itemStateChanged(ItemEvent e)
    {
    if (e.getItemSelectable==watery.clam||watery.veg||watery.broth)
    {
    //add to variables
    }
    //do same with all checkboxes
    dollar = (dollarw + dollarm + dollars);
    cents = (centsw + centsm + centss);

    and now it gives me an error when i try to compile it: "opperator || cannot be applied to boolean, java.awt.checkbox"

    what does that mean?
    Last edited by that_guy; January 17th, 2011 at 12:54 PM.

  5. #5
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: program wont renew total

    ok so i fixed the error problem

    instead of having the if statements with the || operator i just made multiple if statements:

    if (e.getItemSelectable==watery.clam)
    {
    }
    else if (e.getItemSelectable==watery.veg)
    {
    }
    // and i did this with the rest of the checkboxes

    so now it compiles but i still have the same problem with total not updating.

    so back to my original question... since total is a label, how do i update its contents and display them where total is already every time an ItemEvent happens?

Similar Threads

  1. How to get a System's total CPU usage percentage using a java program
    By userj2ee in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 01:28 AM
  2. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 01:46 PM
  3. Why wont the Button wrap?
    By Taylorleemusic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2010, 12:03 AM
  4. Compliing fine but wont run--please help!
    By Nova in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 12th, 2010, 07:51 PM
  5. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM