views:

74

answers:

2

Program stopped compiling at this point: What is causing this error? (Error is at the bottom of post)

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();
    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)
        {
            if(serviceTerms.isSelected() = false) // line 79
            {
                JOptionPane.showMessageDialog(null,"Please accept the terms of service.");
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Thanks.");
            }
        }
    }

Error:

\Desktop\Java Programming\JFrameWithPanel.java:79: unexpected type required: variable found : value if(serviceTerms.isSelected() = false) ^ 1 error

+7  A: 

You have an (illegal) assignment instead of a comparison. Surely you mean:

if (serviceTerms.isSelected() == false)

Of course the preferential, and more readable, way to write this condition is:

if (!serviceTerms.isSelected()) 
Yuval A
or if(!serviceTerms.isSelected())
chris
I thought 1 = was boolean
Nicholas Gibson
Well, that did it. Question answered. Thanks. Ill mark it as correct.
Nicholas Gibson
No, 1 equal is assignment.
Skilldrick
@Nicholas - nope. In most modern languages = is always an assignment operator, while == is the comparison operator.
Yuval A
Wait ok, I never thought of it liek that *facepalm*, obviously if its 1 equal you can assign something a boolean value. Thanks very much guys you helped alot!
Nicholas Gibson
+4  A: 

The problem is that you are using the assignment operator =. What you should be using is the equality operator == like the following:

if(serviceTerms.isSelected() == false) // line 79
{
    JOptionPane.showMessageDialog(null,"Please accept the terms of service.");
}

Or, to totally bypass this mistake, you should skip comparing to false and use the ! (not) operator like so.

if(!serviceTerms.isSelected()) // line 79
{
    JOptionPane.showMessageDialog(null,"Please accept the terms of service.");
}

I feel this way reads better.

If not, service terms is selected

That reads more like a sentence than:

If service terms is selected equals false

jjnguy