Saturday, October 22, 2011

create simple application with netbeans : CheckBox


Create a JFrame container
  1. In the Projects window, right-click the your project node and choose New > JFrame Form.
  2. Enter firscheckbox as the class name.
  3. Enter default package as the package.
  4. Click Finish.

Adding ComponentsNow you have JFrame Form.  Then  add three components three JLabels, two JChecBox, and one  JTextField.
  1.  Use  Event : StateChanged . Right click on the jCheckBox and click events >Action >Change >StateChanged
  2. Use isSelected() , if a checkbox is checked then result will be show  in JTextField.

This is complete script

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cek.java
 *
 */

/**
 *
 * @author 1code
 */
public class cek extends javax.swing.JFrame {

    /** Creates new form cek */
    public cek() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Chiller", 1, 14)); // NOI18N
        jLabel1.setText("Example code");

        jLabel2.setText("packet");

        jCheckBox1.setText("packet 1");
        jCheckBox1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jCheckBox1StateChanged(evt);
            }
        });

        jCheckBox2.setText("packet 2");
        jCheckBox2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jCheckBox2StateChanged(evt);
            }
        });
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });

        jLabel3.setText("TOTAL");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox2)
                            .addComponent(jCheckBox1))))
                .addContainerGap(262, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jCheckBox1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox2)
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(132, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    private void jCheckBox1StateChanged(javax.swing.event.ChangeEvent evt) {                                       
        // TODO add your handling code here:
        if(jCheckBox1.isSelected()==true && jCheckBox2.isSelected()==false
                ||jCheckBox1.isSelected()==false && jCheckBox2.isSelected()==true)
        {
            jTextField1.setText("15.000");
        }
        else if(jCheckBox1.isSelected()==true && jCheckBox2.isSelected()==true)

        {
            jTextField1.setText("25.000");
        }
        else
        {
            jTextField1.setText("Rp.0.000");
        }
    }                                      

    private void jCheckBox2StateChanged(javax.swing.event.ChangeEvent evt) {                                       
        // TODO add your handling code here:
        if(jCheckBox1.isSelected()==true && jCheckBox2.isSelected()==false
                ||jCheckBox1.isSelected()==false && jCheckBox2.isSelected()==true)
        {
            jTextField1.setText("15.000");
        }
        else if(jCheckBox1.isSelected()==true && jCheckBox2.isSelected()==true)

        {
            jTextField1.setText("25.000");
        }
        else
        {
            jTextField1.setText("0.000");
        }
    }                                      

    private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new cek().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                  

}

0 comments:

Post a Comment

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes