Creating Control Buttons with NetBeans IDE - The nucleus of JCheckBox

Started by NAREN, Apr 21, 2008, 01:59 PM

Previous topic - Next topic

NAREN

Creating Control Buttons with NetBeans IDE

Working with JCheckBox

In this section, we shall develop a small application with two check boxes, a button and a text box. The first check box stands for bold and the second for italic. The only difference between the above sample and this example is that we are using check boxes (instead of toggle buttons) and we wanted to apply the style only after clicking on the button.

Currently, I named the project "SampleJavaApplication1" and the form (or JFrame) "Sample04."

When the form (or JFrame) is created with "Sample04," the code behind it (only the constructor) would look something like the following:

  public class Sample04 extends javax.swing.JFrame {
    /** Creates new form Sample04 */
    public Sample04() {
      initComponents();
    }

Make changes to the above code fragment in such a way that it looks similar to the following:

  public class Sample04 extends javax.swing.JFrame {
    /** Creates new form Sample04 */
    public Sample04() {
      initComponents();
      this.setSize(300,200);
    }

The code behind JCheckBox

Let us go through the code created behind JCheckBox. The entire code created by IDE is as follows:

    // Variables declaration - do not modify
    private javax.swing.JButton btnShow;
    private javax.swing.JCheckBox chkBold;
    private javax.swing.JCheckBox chkItalic;
    private javax.swing.JTextField txtMsg;

  private void initComponents() {
      chkBold = new javax.swing.JCheckBox();
      chkItalic = new javax.swing.JCheckBox();
      btnShow = new javax.swing.JButton();
      txtMsg = new javax.swing.JTextField();

      getContentPane().setLayout(null);

  setDefaultCloseOperation(
        javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setTitle("Checkboxes: a demo");
      chkBold.setText("Bold");
      getContentPane().add(chkBold);
      chkBold.setBounds(40, 10, 130, 23);

      chkItalic.setText("Italic");
      getContentPane().add(chkItalic);
      chkItalic.setBounds(40, 30, 130, 23);

      btnShow.setText("Show");
      btnShow.addActionListener(new
          java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent
evt)
  {
          btnShowActionPerformed(evt);
        }
      });

      getContentPane().add(btnShow);
      btnShow.setBounds(40, 60, 130, 23);

      txtMsg.setText("This is some message");
      getContentPane().add(txtMsg);
      txtMsg.setBounds(60, 100, 240, 20);

      pack();
    }

The nucleus of JCheckBox

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

The above code is the heart of our application. According to the code in the previous section, you can understand that when the button is hit, the JRE tries to execute "btnShowActionPerformed." The implementation for that method is as above. According to the above code, the event would simply execute another method, "repaintTextBox."

"repaintTextBox" is my own method, which is defined below:

  private void repaintTextBox() {
    int style=0;
    if (this.chkBold.isSelected()) {
      style += java.awt.Font.BOLD;
    }
    if (this.chkItalic.isSelected()) {
      style += java.awt.Font.ITALIC;
    }
    this.txtMsg.setFont(this.txtMsg.getFont().deriveFont(style));
  }

The above would simply test which check box is switched on (by using "isSelected") and apply the respective font style (bold or italic). This is very similar to what happened when we used the toggle buttons. The only issue is that the changes would not come about unless the "show" button is hit (unlike the previous example, in which they were automatic).
nice games here