Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having problems with my java checkbox applet. I got it mostly working but I ended up with a minor bug. When I select soup the salad should be grayed out (and it is), but if I already have selected an item from salad and then click soup, salad becomes grayed out and the item (inside salad) is still selected.

public class Applet_11 extends Applet implements ItemListener
{
Panel
    dinnerPanel,
    soupPanel,
    saladPanel;

Checkbox
    standard, deluxe,
    soup, salad, 
    cream, broth, gumbo,
    tossed, caesar,
    croutons,lite;

//Used to setup all of the boxes and add them to the Applet
public void init()
{
    CheckboxGroup dinnerType = new CheckboxGroup();
    standard = new Checkbox("standard", dinnerType, false);
    standard.addItemListener(this);
    deluxe = new Checkbox("deluxe", dinnerType, true);
    deluxe.addItemListener(this);

    CheckboxGroup soupOrSalad = new CheckboxGroup();
    soup = new Checkbox("Soup", soupOrSalad, false);
    soup.addItemListener(this);
    salad = new Checkbox("Salad", soupOrSalad, false);
    salad.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);

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

    //adding the componets to the Applet
    setLayout(new GridLayout(0, 1));

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

    soupPanel = new Panel();
    add(soupPanel);
    soupPanel.add(soup);
    soupPanel.add(cream);
    soupPanel.add(broth);
    soupPanel.add(gumbo);

    saladPanel = new Panel();
    add(saladPanel);
    saladPanel.add(salad);
    saladPanel.add(tossed);
    saladPanel.add(caesar);
    saladPanel.add(croutons);
    saladPanel.add(lite);
}

//ItemEvents and calls other functions
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());
    }    
}    

//helper function for dinner type
void handleDinnerType(Checkbox selectedType) 
{
    boolean enabled = false;

    if (selectedType == standard) {
        enabled = false;    
    } else if (selectedType == deluxe) {
        enabled = true;
    }

    cream.setEnabled(enabled);
    broth.setEnabled(enabled);
    gumbo.setEnabled(enabled);
    tossed.setEnabled(enabled);
    caesar.setEnabled(enabled);
    croutons.setEnabled(enabled);
    lite.setEnabled(enabled);

}

//helper function for salad and soup type
void handleSoupSaladChoice(Checkbox selectedCourse) 
{
    boolean soupEnabled, saladEnabled;

    if (selectedCourse == soup) {
        soupEnabled = true;
        saladEnabled = false;
    } else {
        soupEnabled = false;
        saladEnabled = true;
    }

    cream.setEnabled(soupEnabled);
    broth.setEnabled(soupEnabled);
    gumbo.setEnabled(soupEnabled);

    tossed.setEnabled(saladEnabled);
    caesar.setEnabled(saladEnabled);
    croutons.setEnabled(saladEnabled);
    lite.setEnabled(saladEnabled);
}
}     
share|improve this question
before I post anything else I would recommend storing everything in an array/arraylist, will probably make your code 1/4 of what is is currently – Jimmt Apr 10 at 19:14
this is for a class assignment, and we havent got up to arrays yet, and i dont want to jump ahead. But I understand it will be easier and shorter. – FJam Apr 10 at 19:24

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.