Sunday, January 24, 2016

CheckBox - Add Item Listener


package checkboxExample1;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JOptionPane;

/**
 *
 * @author zbawk
 */
public class FrameEx1 extends javax.swing.JFrame {

    double subtotal = 655; //Subtotal for second panel
    /**
     * Creates new form FrameEx1
     */
    public FrameEx1() {
        initComponents();
       
        //Step 1: attach the action event to the button
        //using anonymous class style
       
        jbtnComputeCost.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
             
                double subtotal = 655;
                if (jChkMonitor.isSelected()){
                    subtotal += 229;
                }
                if(jChkDVD.isSelected()){
                    subtotal += 175;
                }
                if(jChkHD.isSelected()){
                    subtotal += 99;
                }
               
                double tax = subtotal * 0.098;
                double total = subtotal + tax;
               
                //display
                NumberFormat formatter = NumberFormat.getCurrencyInstance();
                //display to a java messageBox
               
                JOptionPane.showMessageDialog(null, "subtotal: " + formatter.format(subtotal) + "\n" +
                        "tax: " + formatter.format (tax) + "\n" +
                        "total: " + formatter.format(total));
            }
        });
       
        //==========================Second Panel============================
        //
        //have the checkboxes respond to the item event and
        //change the subtotal, tax and total accordingly
        //item even is for components that selectable, comcomponents that have
        //2 states (selected and deselected) such as
        //checkboxes and radiobuttons
        jChkDVD1.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent ie) {
                //The itemStateChanged is invoked when the state
                //of this checkbox changes from selected to
                //deselected or from deselected to selected
               
                //find out if it was selected
                double tax;
                double total;
                if(ie.getStateChange()==ItemEvent.SELECTED){
                    subtotal += 175;
                    tax = subtotal * 0.098;
                    total = subtotal + tax;
                }else {
                    subtotal -= 175;
                    tax = subtotal * 0.098;
                    total = subtotal + tax;
                }
                //display
                NumberFormat formatter = NumberFormat.getCurrencyInstance();
                jTextArea1.setText("subtotal: " + formatter.format(subtotal) + "\n" +
                        "tax: " + formatter.format (tax) + "\n" +
                        "total: " + formatter.format(total));
            }
        });
       
        jChkMonitor1.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent ie) {
                //find out if it was selected
                double tax;
                double total;
                if(ie.getStateChange()==ItemEvent.SELECTED){
                    subtotal += 229;
                    tax = subtotal * 0.098;
                    total = subtotal + tax;
                }else {
                    subtotal -= 229;
                    tax = subtotal * 0.098;
                    total = subtotal + tax;
                }
                //display
                NumberFormat formatter = NumberFormat.getCurrencyInstance();
                jTextArea1.setText("subtotal: " + formatter.format(subtotal) + "\n" +
                        "tax: " + formatter.format (tax) + "\n" +
                        "total: " + formatter.format(total));
            }
        });
       
        jChkHD1.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent ie) {
                //find out if it was selected
                double tax;
                double total;
                if(ie.getStateChange()==ItemEvent.SELECTED){
                    subtotal += 99;
                    tax = subtotal * 0.098;
                    total = subtotal + tax;
                }else {
                    subtotal -= 99;
                    tax = subtotal * 0.098;
                    total = subtotal + tax;
                }
                //display
                NumberFormat formatter = NumberFormat.getCurrencyInstance();
                jTextArea1.setText("subtotal: " + formatter.format(subtotal) + "\n" +
                        "tax: " + formatter.format (tax) + "\n" +
                        "total: " + formatter.format(total));
            }
        });
    }

No comments:

Post a Comment