8 Replies - 1724 Views - Last Post: 08 December 2010 - 10:31 PM Rate Topic: -----

#1 mchin131   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Checkbox Running Total problem?

Posted 08 December 2010 - 05:46 PM

I have an assignment where this applet is supposed to simulate a menu with involving checkboxes and checkboxgroups with calories listed along each item. My problem is that I don't know how to do a running total with every item on the menu with different values.

This is what i have:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class CheckboxApplet extends Applet implements ItemListener, ActionListener {
    public void init() {
        setLayout(new GridLayout(1, 0));

        CheckboxGroup dinnerType = new CheckboxGroup();
        standard = new Checkbox("standard", dinnerType, false);
        standard.addItemListener(this);
        deluxe = new Checkbox("deluxe", dinnerType, true);
        deluxe.addItemListener(this);

        CheckboxGroup appetizers = new CheckboxGroup();
        crepe = new Checkbox("crepe", appetizers, false);
        crepe.addItemListener(this);
        quiche = new Checkbox("quiche", appetizers, false);
        quiche.addItemListener(this);
        dumpling = new Checkbox("dumpling", appetizers, false);
        dumpling.addItemListener(this);

        CheckboxGroup soupOrSalad = new CheckboxGroup();

        soup = new Checkbox("Soup", soupOrSalad, false);
        soup.addItemListener(this);

        CheckboxGroup soups = new CheckboxGroup();
        cream = new Checkbox("cream", soups, false);
        cream.addItemListener(this);
        broth = new Checkbox("broth", soups, false);
        broth.addItemListener(this);
        gumbo = new Checkbox("gumbo", soups, false);
        gumbo.addItemListener(this);

        salad = new Checkbox("Salad", soupOrSalad, false);
        salad.addItemListener(this);

        CheckboxGroup salads = new CheckboxGroup();
        tossed = new Checkbox("tossed", salads, false);
        tossed.addItemListener(this);
        caesar = new Checkbox("caesar", salads, false);
        caesar.addItemListener(this);
        croutons = new Checkbox("croutons");
        croutons.addItemListener(this);
        lite = new Checkbox("lite dressing");
        lite.addItemListener(this);

        CheckboxGroup entrees = new CheckboxGroup();
        chicken = new Checkbox("chicken", entrees, false);
        chicken.addItemListener(this);
        beef = new Checkbox("beef", entrees, false);
        beef.addItemListener(this);
        lamb = new Checkbox("lamb", entrees, false);
        lamb.addItemListener(this);
        fish = new Checkbox("fish", entrees, false);
        fish.addItemListener(this);

        CheckboxGroup deserts = new CheckboxGroup();
        pie = new Checkbox("pie", deserts, false);
        pie.addItemListener(this);
        fruit = new Checkbox("fruit", deserts, false);
        fruit.addItemListener(this);
        sherbet = new Checkbox("sherbet", deserts, false);
        sherbet.addItemListener(this);

        setLayout(new GridLayout(0, 1));

        Panel p = new Panel();
        add(p);
        p.add(standard);
        p.add(deluxe);

        appetizerPanel = new Panel();
        add(appetizerPanel);
	crepeLabel = new Label ("400");
	quicheLabel = new Label ("300");
	dumplingLabel = new Label ("250");
        appetizerPanel.add(new Label("Appetizer"));
        appetizerPanel.add(crepe);
        appetizerPanel.add(crepeLabel);
        appetizerPanel.add(quiche);
        appetizerPanel.add(quicheLabel);
        appetizerPanel.add(dumpling);
        appetizerPanel.add(dumplingLabel);

        soupPanel = new Panel();
        add(soupPanel);
        creamLabel = new Label ("300");
	brothLabel = new Label ("250");
	gumboLabel = new Label ("300");
        soupPanel.add(soup);
        soupPanel.add(cream);
        soupPanel.add(creamLabel);
        soupPanel.add(broth);
        soupPanel.add(brothLabel);
        soupPanel.add(gumbo);
        soupPanel.add(gumboLabel);

        saladPanel = new Panel();
        add(saladPanel);
	tossedLabel = new Label ("150");
	caesarLabel = new Label ("250");
	croutonsLabel = new Label ("100");
	liteLabel = new Label ("150");
        saladPanel.add(salad);
        saladPanel.add(tossed);
        saladPanel.add(tossedLabel);
        saladPanel.add(caesar);
        saladPanel.add(caesarLabel);
        saladPanel.add(croutons);
        saladPanel.add(croutonsLabel);
        saladPanel.add(lite);
        saladPanel.add(liteLabel);

        entreePanel = new Panel();
        add(entreePanel);
	chickenLabel = new Label ("700");
	beefLabel = new Label ("800");
	lambLabel = new Label ("750");
	fishLabel = new Label ("600");
        entreePanel.add(chicken);
        entreePanel.add(chickenLabel);
        entreePanel.add(beef);
        entreePanel.add(beefLabel);
        entreePanel.add(lamb);
        entreePanel.add(lambLabel);
        entreePanel.add(fish);
        entreePanel.add(fishLabel);

        desertPanel = new Panel();
	desertLabel = new Label("Dessert");
	pieLabel = new Label ("250");
	fruitLabel = new Label ("150");
	sherbetLabel = new Label ("300");
	desertLabel.setForeground(Color.BLACK);
        add(desertPanel);
	desertPanel.add(desertLabel);
        desertPanel.add(pie);
        desertPanel.add(pieLabel);
        desertPanel.add(fruit);
        desertPanel.add(fruitLabel);
        desertPanel.add(sherbet);
        desertPanel.add(sherbetLabel);

	calc = new Button("Calculate");
	calc.addActionListener(this);
	add(calc);

	calcLabel = new Label ("0");
	add(calcLabel);
	}

    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == standard || e.getSource() == deluxe)
            handleDinnerType((Checkbox)e.getSource());
        else if (e.getSource() == soup || e.getSource() == salad)
            handleSoupSaladChoice((Checkbox)e.getSource());
	}

	int crepeVal = Integer.parseInt(crepeLabel.getText());
	int quicheVal = Integer.parseInt(quicheLabel.getText());
	int dumplingVal = Integer.parseInt(dumplingLabel.getText());
	int tossedVal = Integer.parseInt(tossedLabel.getText());
	int ceasarVal = Integer.parseInt(caesarLabel.getText());
	int croutonsVal = Integer.parseInt(croutonsLabel.getText());
	int liteVal = Integer.parseInt(liteLabel.getText());
	int chickenVal = Integer.parseInt(chickenLabel.getText());
	int beefVal = Integer.parseInt(beefLabel.getText());
	int lambVal = Integer.parseInt(lambLabel.getText());
	int fishVal = Integer.parseInt(fishLabel.getText());
	int brothVal = Integer.parseInt(brothLabel.getText());
	int gumboVal = Integer.parseInt(gumboLabel.getText());
	int pieVal = Integer.parseInt(pieLabel.getText());
	int fruitVal = Integer.parseInt(fruitLabel.getText());
	int sherbetVal = Integer.parseInt(sherbetLabel.getText());

/*
	if (e.getSource() == chicken)
	  chickenVal += total;
*/

    void handleDinnerType(Checkbox selectedType) {
        boolean enabled;
        if (selectedType == standard) {
            enabled = false;
	desertLabel.setForeground(Color.GRAY);
	}
        else
            enabled = true;
        pie.setEnabled(enabled);
        fruit.setEnabled(enabled);
        sherbet.setEnabled(enabled);
        cream.setEnabled(enabled);
        broth.setEnabled(enabled);
        gumbo.setEnabled(enabled);
        tossed.setEnabled(enabled);
        caesar.setEnabled(enabled);
        croutons.setEnabled(enabled);
        lite.setEnabled(enabled);
	desertLabel.setForeground(Color.BLACK);
    }

    void handleSoupSaladChoice(Checkbox selectedCourse) {
        boolean soupEnabled, saladEnabled;
        if (selectedCourse == soup) {
            soupEnabled = true;
            saladEnabled = false;
	soup.setForeground(Color.BLACK);
	salad.setForeground(Color.GRAY);
        }
        else {
            soupEnabled = false;
            saladEnabled = true;
	soup.setForeground(Color.GRAY);
	salad.setForeground(Color.BLACK);
        }
        cream.setEnabled(soupEnabled);
        broth.setEnabled(soupEnabled);
        gumbo.setEnabled(soupEnabled);

        tossed.setEnabled(saladEnabled);
        caesar.setEnabled(saladEnabled);
        croutons.setEnabled(saladEnabled);
        lite.setEnabled(saladEnabled);
    }


     public void actionPerformed(ActionEvent e) { 

	if (e.getSource() == calc) {
		calcLabel.setText("" + total);
	}
    }

    Panel
        appetizerPanel, soupPanel, saladPanel, entreePanel, desertPanel;

    Checkbox
        standard, deluxe,
        soup, salad,
        crepe, quiche, dumpling,
        cream, broth, gumbo,
        tossed, caesar,
        croutons, lite,
        chicken, beef, lamb, fish,
        pie, fruit, sherbet;

   Label
	crepeLabel, quicheLabel, dumplingLabel,
	creamLabel, brothLabel, gumboLabel,
	tossedLabel, caesarLabel,
	croutonsLabel, liteLabel,
	chickenLabel, beefLabel, lambLabel, fishLabel,
	pieLabel, fruitLabel, sherbetLabel,
	desertLabel, calcLabel;
   Button calc;
   int total;
}



The part I commented is my attempt, but I really don't have a clue what I am doing. I could really use some help!

Is This A Good Question/Topic? 0
  • +

Replies To: Checkbox Running Total problem?

#2 mchin131   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Checkbox Running Total problem?

Posted 08 December 2010 - 06:09 PM

I was messing around with the commented part, and I came up with this:

	if (e.getStateChange() == ItemEvent.SELECTED)
	    total += 500;
	else total -= 500;



but I don't know how to do it for specified selected items from the menu.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Checkbox Running Total problem?

Posted 08 December 2010 - 06:56 PM

You do not need to add an actinLitener to checkBox
Yust when the calc button, the only one to have an actionListener, to scan your checkBox to see if they are selected or not an act accordingly
Was This Post Helpful? 0
  • +
  • -

#4 mchin131   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Checkbox Running Total problem?

Posted 08 December 2010 - 07:59 PM

I'm not sure what the command is to see if they are selected or not. Also, isn't the actionListener the way to call for button Events to work? That's why I have mine up for.
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Checkbox Running Total problem?

Posted 08 December 2010 - 08:08 PM

View Postmchin131, on 08 December 2010 - 08:59 PM, said:

I'm not sure what the command is to see if they are selected or not. Also, isn't the actionListener the way to call for button Events to work? That's why I have mine up for.

if(crepe.isSelected()) will be true if the crepe checkbox is selected

You need an actionListener in checkBox or radioButton if they have something to perform when clicked. You can have a Calculator that can display the number in binary, decimal and hexadecimal with 3 radioButton to select to display in which base. If the display is in decimal and the user click the hexadecimal button, you have to react immediatly and change the display and put it hex.

This is not your case unless.... to want a JLabel at the bottom that display the total and updates it as soon as one of the button is selected or deselected. In that case, first: you do not need the calc button anymore and second: you will have to scan all your checkBox anyhow to compute your total because you wouldn't know if the user just select cream, or deselected cream, or selected cream wihich deselected broth.
Was This Post Helpful? 0
  • +
  • -

#6 mchin131   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Checkbox Running Total problem?

Posted 08 December 2010 - 08:41 PM

Thank you for the info. Also, I get an error when i use if(crepe.isSelected()). It says method .isSelected() cannot be found.
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Checkbox Running Total problem?

Posted 08 December 2010 - 08:53 PM

oups....
you are using CheckBox not JCheckBox... you are 10 years late

OK the equivalent of JCheckBox.isSelected() is CheckBox.getState()

you should seriously think using Swing and JApplet, JButton, JCheckBox, no serious development 100% in AWT since 10 years but... it is still working
Was This Post Helpful? 0
  • +
  • -

#8 mchin131   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Checkbox Running Total problem?

Posted 08 December 2010 - 09:26 PM

Thank you... and sorry. The Java class I'm taking teaches us to use these methods...

I have a problem with my running total. If I want to change up the slections, the total just keeps adding on. Is there a way to reset every time I have the button pressed?
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Checkbox Running Total problem?

Posted 08 December 2010 - 10:31 PM

View Postmchin131, on 08 December 2010 - 10:26 PM, said:

Thank you... and sorry. The Java class I'm taking teaches us to use these methods...

I have a problem with my running total. If I want to change up the slections, the total just keeps adding on. Is there a way to reset every time I have the button pressed?

This is exactly what I said in my post #5
second: you will have to scan all your checkBox anyhow to compute your total because you wouldn't know if the user just select cream, or deselected cream, or selected cream wich deselected broth.

Everytime your actionPerformed will be called because a checkbox changed
you will have to reset your total to 0
and scan ALL your buttons to add the amount corresponding to what they cost
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1