CHECK BOXES14
CHECK BOXES14
A check box is a way to allow your users to select and deselect items. They can be a little fiddly, though,
so it's a good idea to add them to a panel. That way, you can move them all at once just by moving
the panel.
So add a panel to your form, which can be found under Swing Containers in the NetBeans palette.
Now locate the check box control. Drag a check box onto your panel.
The text jCheckBox1 is the default text. You can change this either in the properties window, or by
right-clicking the check box. From the menu that appears, select Edit Text (we've chopped a few
menu items off the list, in the image below):
Press the enter key on your keyboard to confirm the change. The text will change for your check box:
Leave it on the default variable name. But just be aware that changing the text of a control does not
change its variable name.
Now that you have added one check box to your panel, add three more. Change the text of the three
to: Java, PHP, and Visual Basic. Your check boxes will then look like this:
What we'll do is to get the items that a user has checked. We'll do this when a button is clicked. To
display the items, we'll use the Text Area control, rather than a text field.
So add a button to your form. Change the variable name to btnCheckBoxes. Change the text on the
button to Selected Items.
Locate the Text Area control in the NetBeans palette, and drag one onto your form. Change the
variable name to taOne.
Java checkboxes have a property called isSelected. We can use this in a series of IF Statements to see
if each box is selected or not. If they are, we can build up a string, adding the text from each check
box.
Double click your new button to create a code stub. Add the following code:
When you've finished typing the code, run your programme. Select a few check boxes and then click
the button. You should find that the items you checked appear in the text area:
EXAMPLE
*/
package option;
import javax.swing.JOptionPane;
/**
* @author WAINAINA
*/
/**
*/
public CHECKBOX() {
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
*/
@SuppressWarnings("unchecked")
jLabel1.setText("PROGRAMMING SKILLS");
jButton1.setText("SELECT SKILL");
jButton1.addActionListener(new java.awt.event.ActionListener() {
jButton1ActionPerformed(evt);
});
jCheckBox1.setText("Java");
jCheckBox2.setText("PHP");
jCheckBox3.setText("C#");
jCheckBox4.setText("Python");
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jCheckBox1)
.addComponent(jCheckBox2)
.addComponent(jCheckBox3)
.addComponent(jCheckBox4)
.addContainerGap(203, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jCheckBox1)
.addComponent(jCheckBox2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jCheckBox3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jCheckBox4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27,
Short.MAX_VALUE)
);
pack();
}// </editor-fold>
if(jCheckBox1.isSelected())
JOptionPane.showMessageDialog(rootPane, jCheckBox1.getText());
if(jCheckBox2.isSelected())
JOptionPane.showMessageDialog(rootPane, jCheckBox2.getText());
if(jCheckBox3.isSelected())
JOptionPane.showMessageDialog(rootPane, jCheckBox3.getText());
if(jCheckBox4.isSelected())
JOptionPane.showMessageDialog(rootPane, jCheckBox4.getText());
/**
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*/
try {
if ("Nimbus".equals(info.getName())) {
break;
java.util.logging.Logger.getLogger(CHECKBOX.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
java.util.logging.Logger.getLogger(CHECKBOX.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
java.util.logging.Logger.getLogger(CHECKBOX.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
java.util.logging.Logger.getLogger(CHECKBOX.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new CHECKBOX().setVisible(true);