views:

48

answers:

1

Coded for a checkbox that agrees to the Terms of Service and an accept and decline button.

I need help for an exception handling that

if the user does NOT have the checkbox as SELECTED then when they hit accept, an error message occurs telling the user that he has not selected the checkbox.

How would I code an error handling exception using these 2 arguments?

import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
import java.lang.Integer.*;
import java.lang.Object.*;
import java.util.*;
import java.util.Random;
import java.io.*;
import java.text.*;
import java.text.DecimalFormat.*;

public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener
{
    int packageIndex;
    double price;
    double[] prices = {49.99, 39.99, 34.99, 99.99};

    DecimalFormat money = new DecimalFormat("$0.00");
    JLabel priceLabel = new JLabel("Total Price: "+price);
    JButton button = new JButton("Check Price");
    JComboBox packageChoice = new JComboBox();
    Font newFont = new Font("Helvetica", Font.BOLD, 14);
    JPanel pane = new JPanel();
    TextField text = new TextField(5);
    JButton accept = new JButton("Accept");
    JButton decline = new JButton("Decline");
    JCheckBox serviceTerms = new JCheckBox("I Agree to the Terms of Service.", false);
    JTextArea termsOfService = new JTextArea("This is a text area", 5, 10);


    public JFrameWithPanel()
    {
        super("JFrame with Panel");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(packageChoice);
        setContentPane(pane);
        setSize(250,250);
        setVisible(true);

        packageChoice.addItem("A+ Certification");
        packageChoice.addItem("Network+ Certification ");
        packageChoice.addItem("Security+ Certifictation");
        packageChoice.addItem("CIT Full Test Package");

        pane.add(button);
        button.addActionListener(this);

        pane.add(text);
        text.setEditable(false);
        text.setBackground(Color.WHITE);
        text.addActionListener(this);

        pane.add(termsOfService);
        termsOfService.setEditable(false);
        termsOfService.setBackground(Color.lightGray);

        pane.add(serviceTerms);
        serviceTerms.addItemListener(this);

        pane.add(accept);
        accept.addActionListener(this);


        pane.add(decline);
        decline.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        packageIndex = packageChoice.getSelectedIndex();
        price = prices[packageIndex];
        text.setText("$"+price);

        Object source = e.getSource();

        if(source == accept)
        {
            JOptionPane.showMessageDialog(this,"Thank you for choosing our tests. Enjoy!");
        }
        else if(source == decline)
        {
            System.exit(0);
        }
    }

    public void itemStateChanged(ItemEvent e)
    {
        int select = e.getStateChange();

        if(select == ItemEvent.DESELECTED)
        {
            JOptionPane.showMessageDialog(null,"Please agree to the terms of service.");
        }
        else
        {

        }
    }
}
+1  A: 

Why don't you just check for the isSelected to be true?

if (!serviceTerms.isSelected())
    JOptionPane.showMessageDialog(null, "You have not accepted the terms.");
jsmith
so if(serviceTerms.isSelected = false){JOptionPane.showMessageDialog(null,"Must accept.");}
Nicholas Gibson