I'm working on a simple program, but I help with a part of it. When I click the submit button I need the name field to be cleared and for all of the checkbox to be unchecked. I have the name field done, but I need help with unchecking all of the checkboxes.

Thanks for the help.

-jdm

package Gui2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

 class Gui2 extends JFrame implements ActionListener, ItemListener
{
    //declares all the variables for use in my program.
    private JCheckBox [] CheckBox;

    String[] ButtonText = { "Taco $0.99", "Burrito $1.29", "Enchilada $1.39",
                            "Nachos $1.19", "Salad $2.09", "Tea $2.00"};

    private JButton submitButton;

    private JTextField NameField;
    private JTextArea Info;

    private JLabel NameLabel, TopLabel, TextAreaTitle, AmtDue;
    private JPanel TextFieldPanel, CheckBoxPanel, TextAreaPanel, TextAreaPanel2,
            ButtonPanel, NorthBorder, WestBorder, EastBorder, TopPanel, TextLabelPanel,
            GridLeft, GridRight, InnerRightGrid, InnerRightBorder, IRGinnerBorder,
            BlankPanel, GRBGrid, NameFlowPanel, NameFlowPanel2, TextAreaFlowPanel,
            TATFlowPanel;

    private double price = 0;

    private final double taco = 0.99;
    private final double burrito = 1.29;
    private final double enchilada = 1.39;
    private final double nachos = 1.19;
    private final double salad = 2.09;
    private final double tea = 2.00;

    private Container contentPane;

    //The normal main making
    public static void main(String [] args)
    {
        Gui2 frame = new Gui2();
        frame.setVisible(true);
    }//end of main

    public Gui2()
    {
        //detailing the JFrame
        setTitle("Britton's TexMex Cafe Order Form");
        setSize(400,500);
        setLocation(200,200);
        setResizable(false);

        contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //makes the button
        submitButton = new JButton("Submit Order");
        submitButton.addActionListener(this);

        //makes and sets the name for the label
        NameLabel = new JLabel("Type your name in the box below");

        //makes the text field
        NameField = new JTextField();
        NameField.setColumns(21);
        NameField.addActionListener(this);

        //makes the checkbox
        CheckBox = new JCheckBox[ButtonText.length];

        //makes the very top label
        TopLabel = new JLabel("Please Place Your Order for Britton's Cafe");
        Font font = new Font("Arial", Font.BOLD, 17);
        TopLabel.setFont(font);

        //makes the info in the cyan TextArea
        Info = new JTextArea("Britton's Cafe, located in the north-    west corner"
                + " of Tennessee near the       community of Martin, specializes in"
                + "    Mexican food with a TexMex flavor."
                + "                                                       ");

        Info.setBackground(Color.cyan);
        Info.setLineWrap(true);
        Info.setEditable(false);
        Info.setColumns(21);

        //for future use with the listener outputs the price
        AmtDue = new JLabel("Amount Due: $" + price);
        AmtDue.setForeground(Color.red);

        //makes the label above the cyan textarea
        TextAreaTitle = new JLabel("Specializing in TexMex Food");
        TextAreaTitle.setForeground(Color.blue);

        //creats all the panels used in the frame
        //TAT stands for TextAreaTitle
        TATFlowPanel = new JPanel();
        TATFlowPanel.setLayout(new FlowLayout());
        TATFlowPanel.add(TextAreaTitle);

        NorthBorder = new JPanel();
        NorthBorder.setLayout(new BorderLayout());
        NorthBorder.setBackground(Color.orange);

        WestBorder = new JPanel();
        WestBorder.setLayout(new BorderLayout());

        EastBorder = new JPanel();
        EastBorder.setLayout(new BorderLayout());

        GridLeft = new JPanel();
        GridLeft.setLayout(new GridLayout(2,0));

        GridRight = new JPanel();
        GridRight.setLayout(new GridLayout(2,0));

        InnerRightGrid = new JPanel();
        InnerRightGrid.setLayout(new GridLayout(2,0));

        InnerRightBorder = new JPanel();
        InnerRightBorder.setLayout(new BorderLayout());

        //IRG stands for InnerRightGrid
        IRGinnerBorder = new JPanel();
        IRGinnerBorder.setLayout(new BorderLayout());

        TopPanel = new JPanel();
        TopPanel.setLayout(new FlowLayout());
        TopPanel.setBackground(Color.orange);
        TopPanel.add(TopLabel);

        CheckBoxPanel = new JPanel();
        CheckBoxPanel.setLayout(new GridLayout(0,1));

        for (int i = 0; i < CheckBox.length; i++)
	{
		CheckBox[i] = new JCheckBox(ButtonText[i]);
		CheckBoxPanel.add(CheckBox[i]);
                CheckBox[i].addItemListener(this);
        }

        CheckBoxPanel.setBorder(BorderFactory.createTitledBorder("Make Selections" ));

        NameFlowPanel = new JPanel();
        NameFlowPanel.setLayout(new FlowLayout());
        NameFlowPanel.add(NameLabel);

        TextLabelPanel = new JPanel();
        TextLabelPanel.setLayout(new BorderLayout());
        TextLabelPanel.add(NameFlowPanel, BorderLayout.NORTH);

        NameFlowPanel2 = new JPanel();
        NameFlowPanel2.setLayout(new FlowLayout());
        NameFlowPanel2.add(NameField);

        TextFieldPanel = new JPanel();
        TextFieldPanel.setLayout(new BorderLayout());
        TextFieldPanel.add(NameFlowPanel2, BorderLayout.SOUTH);

        ButtonPanel = new JPanel();
        ButtonPanel.setLayout(new BorderLayout());
        ButtonPanel.add(submitButton, BorderLayout.SOUTH);

        TextAreaFlowPanel = new JPanel();
        TextAreaFlowPanel.setLayout(new FlowLayout());
        TextAreaFlowPanel.add(Info);
        TextAreaPanel = new JPanel();
        TextAreaPanel.setLayout(new BorderLayout());
        TextAreaPanel.add(TextAreaFlowPanel);

        TextAreaPanel2 = new JPanel();
        TextAreaPanel2.setLayout(new FlowLayout());
        TextAreaPanel2.setBackground(Color.yellow);
        TextAreaPanel2.add(AmtDue);

        //GRB stands for GridRightBorder
        GRBGrid = new JPanel();
        GRBGrid.setLayout(new GridLayout(2,0));

        BlankPanel = new JPanel();
        BlankPanel.setLayout(new FlowLayout());

        //adding the basic panel to the frame
        contentPane.add(NorthBorder, BorderLayout.NORTH);
        contentPane.add(WestBorder, BorderLayout.WEST);
        contentPane.add(EastBorder, BorderLayout.CENTER);

        //stacking all the panels on each other to look right
        NorthBorder.add(TopPanel, BorderLayout.NORTH);

        WestBorder.add(GridLeft);

        GridLeft.add(CheckBoxPanel);

        GridLeft.add(ButtonPanel);

        EastBorder.add(GridRight);

        GridRight.add(InnerRightBorder);

        InnerRightBorder.add(TextLabelPanel, BorderLayout.NORTH);

        InnerRightBorder.add(InnerRightGrid, BorderLayout.CENTER);

        InnerRightGrid.add(IRGinnerBorder);

        IRGinnerBorder.add(TextFieldPanel, BorderLayout.NORTH);

        IRGinnerBorder.add(TATFlowPanel, BorderLayout.SOUTH);

        InnerRightGrid.add(TextAreaPanel);

        GridRight.add(GRBGrid);

        GRBGrid.add(BlankPanel);

        GRBGrid.add(TextAreaPanel2);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }//end of GUI2

    public void actionPerformed(ActionEvent event)
    {

    //when the Submit Button is clicked the name is in the Namefield appears
    //in the yellow box.

    AmtDue.setText("Order Submitted by " + NameField.getText());
    NameField.setText(null);

    }

    public void itemStateChanged(ItemEvent event)
    {
        JCheckBox source = (JCheckBox) event.getSource();

               if (source == CheckBox[0])
               {
                   if(event.getStateChange() == ItemEvent.SELECTED)
                   price += taco;

                   else
                   price -= taco;

                   AmtDue.setText("Amount Due: $" + price);
 
               }

                else if(source == CheckBox[1])
               {
                   if(event.getStateChange() == ItemEvent.SELECTED)
                    price += burrito;

                   else
                    price -= burrito;

                   AmtDue.setText("Amount Due: $" + price);
               }

                else if(source == CheckBox[2])
               {
                   if(event.getStateChange() == ItemEvent.SELECTED)
                    price += enchilada;

                   else
                    price -= enchilada;

                   AmtDue.setText("Amount Due: $" + price);
               }

                else if(source == CheckBox[3])
               {
                   if(event.getStateChange() == ItemEvent.SELECTED)
                    price += nachos;

                   else
                    price -= nachos;

                   AmtDue.setText("Amount Due: $" + price);
               }

                else if(source == CheckBox[4])
               {
                   if(event.getStateChange() == ItemEvent.SELECTED)
                    price += salad;

                   else
                    price -= salad;

                   AmtDue.setText("Amount Due: $" + price);
               }

                else if(source == CheckBox[5])
               {
                   if(event.getStateChange() == ItemEvent.SELECTED)
                    price += tea;

                   else
                    price -= tea;

                   AmtDue.setText("Amount Due: $" + price);
               }
    }
}

Recommended Answers

All 2 Replies

if(myCheckBox.isSelected()){
myCheckBox.setSelected(false);
}

if(myCheckBox.isSelected()){
myCheckBox.setSelected(false);
}

Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.