0% found this document useful (0 votes)
3 views13 pages

CHECK BOXES14

The document provides a tutorial on creating and managing check boxes in a Java Swing application using NetBeans. It explains how to add check boxes to a panel, change their text, and retrieve selected items when a button is clicked, displaying the results in a text area. Additionally, it includes sample code for implementing the functionality and handling user interactions.

Uploaded by

buushman151
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

CHECK BOXES14

The document provides a tutorial on creating and managing check boxes in a Java Swing application using NetBeans. It explains how to add check boxes to a panel, change their text, and retrieve selected items when a button is clicked, displaying the results in a text area. Additionally, it includes sample code for implementing the functionality and handling user interactions.

Uploaded by

buushman151
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Java Check Boxes

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):

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 1


When you click on Edit Text, the default text will be highlighted:

Type C Sharp over the top of the highlighted text:

Press the enter key on your keyboard to confirm the change. The text will change for your check box:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 2


However, this just changes the text, and not the variable name. The variable name will still
be jCheckBox1, as you can see in the Navigator area to the left:

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.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 3


When you've aligned your new controls, your form should look something like this:

Now for the code.

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:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 4


The string we're building up is called s1. If a check box is selected then we get the text from that check
box. This is then stored in the s1 variable, along with a new line character ( '\n') The last line of code
sets the text for the text area. In between the round brackets of setText, we have the s1 variable, which
is the string we're building up.

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

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 5


/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package option;

import javax.swing.JOptionPane;

/**

* @author WAINAINA

*/

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 6


public class CHECKBOX extends javax.swing.JFrame {

/**

* Creates new form CHECKBOX

*/

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

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jButton1 = new javax.swing.JButton();

jCheckBox1 = new javax.swing.JCheckBox();

jCheckBox2 = new javax.swing.JCheckBox();

jCheckBox3 = new javax.swing.JCheckBox();

jCheckBox4 = new javax.swing.JCheckBox();

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 7


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Tahoma", 3, 24)); // NOI18N

jLabel1.setForeground(new java.awt.Color(255, 102, 102));

jLabel1.setText("PROGRAMMING SKILLS");

jButton1.setFont(new java.awt.Font("Tahoma", 3, 24)); // NOI18N

jButton1.setForeground(new java.awt.Color(255, 102, 102));

jButton1.setText("SELECT SKILL");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

});

jCheckBox1.setText("Java");

jCheckBox2.setText("PHP");

jCheckBox3.setText("C#");

jCheckBox4.setText("Python");

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 8


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(100, 100, 100)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jCheckBox1)

.addComponent(jCheckBox2)

.addComponent(jCheckBox3)

.addComponent(jCheckBox4)

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 316,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 279,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap(203, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 48,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jCheckBox1)

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 9


.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.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)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 64,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(79, 79, 79))

);

pack();

}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

if(jCheckBox1.isSelected())

JOptionPane.showMessageDialog(rootPane, jCheckBox1.getText());

if(jCheckBox2.isSelected())

JOptionPane.showMessageDialog(rootPane, jCheckBox2.getText());

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 10


}

if(jCheckBox3.isSelected())

JOptionPane.showMessageDialog(rootPane, jCheckBox3.getText());

if(jCheckBox4.isSelected())

JOptionPane.showMessageDialog(rootPane, jCheckBox4.getText());

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<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.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 11


javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(CHECKBOX.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(CHECKBOX.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(CHECKBOX.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(CHECKBOX.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new CHECKBOX().setVisible(true);

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 12


});

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JCheckBox jCheckBox1;

private javax.swing.JCheckBox jCheckBox2;

private javax.swing.JCheckBox jCheckBox3;

private javax.swing.JCheckBox jCheckBox4;

private javax.swing.JLabel jLabel1;

// End of variables declaration

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 13

You might also like