How can i get a counter to tell me how many checkboxes have been selected?

package arabiclanguagelearning;

import java.awt.BorderLayout;

import java.awt.Frame;

import javax.swing.JDialog;

import javax.swing.JPanel;

import java.awt.Rectangle;

import javax.swing.JTextArea;

import com.borland.jbcl.layout.VerticalFlowLayout;

import javax.swing.JLabel;

import javax.swing.*;

import java.awt.Color;

import java.awt.Font;

import java.awt.Toolkit;

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.CheckboxGroup;

import java.awt.event.ItemListener;

import java.awt.event.ItemEvent;

import java.awt.event.KeyEvent;

/**

*

Title: Arabic Language Learning

*

*

Description:

*

*

Copyright: Copyright (c) 2007

*

*

Company:

*

* @author iPortal

* @version 1.0

*/

publicclass ReadingTestextends JDialogimplements ItemListener{

//ImageIcon myImage = new ImageIcon("image.jpg");

JPanel panel1 =new JPanel();

JLabel jLabel1 =new JLabel();

JLabel jLabel3 =new JLabel();

JButton jButton1 =new JButton(new ImageIcon ("image.jpg"));

ButtonGroup question1 =new ButtonGroup();

CheckboxGroup question2 =new CheckboxGroup();

JTextArea question1Text =new JTextArea();

JRadioButton q1A =new JRadioButton();

JRadioButton q1B =new JRadioButton();

JRadioButton q1C =new JRadioButton();

JTextArea question2Text =new JTextArea();

JTextField question2TextArabic =new JTextField();

JCheckBox jCheckBox1 =new JCheckBox();

JCheckBox jCheckBox2 =new JCheckBox();

JCheckBox jCheckBox3 =new JCheckBox();

JCheckBox jCheckBox4 =new JCheckBox();

JLabel jLabel2 =new JLabel();

StringBuffer choices;

//will keep a count on how many checkboxes selected for question 2

int count;

JLabel jLabel4 =new JLabel();

JLabel jLabel5 =new JLabel();

JLabel jLabel6 =new JLabel();

JButton Submit =new JButton();

JLabel jLabel7 =new JLabel();

public ReadingTest(Frame owner, String title,boolean modal){

super(owner, title, modal);

try{

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

jbInit();

pack();

}catch (Exception exception){

exception.printStackTrace();

}

}

public ReadingTest(){

this(new Frame(),"ReadingTest",false);

}

privatevoid jbInit()throws Exception{

panel1.setLayout(null);

this.getContentPane().setLayout(null);

jLabel1.setFont(new java.awt.Font("Tahoma", Font.BOLD, 24));

jLabel1.setHorizontalAlignment(SwingConstants.CENTER);

jLabel1.setIcon(null);

jLabel1.setText("Welcome to the reading tutorial");

jLabel1.setBounds(new Rectangle(49, 4, 509, 67));

jLabel3.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 14));

jLabel3.setToolTipText("");

jLabel3.setHorizontalAlignment(SwingConstants.CENTER);

jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel3.setText(

"Please answer the questions below after reading the text");

jLabel3.setVerticalTextPosition(SwingConstants.TOP);

jLabel3.setBounds(new Rectangle(48, 83, 515, 60));

jButton1.setBounds(new Rectangle(509, 63, 98, 44));

jButton1.setText("Hide me!");

jButton1.addActionListener(new ReadingTest_jButton1_actionAdapter(this));

question1Text.setBackground(Color.lightGray);

question1Text.setFont(new java.awt.Font("Tahoma", Font.BOLD, 14));

question1Text.setEditable(false);

question1Text.setText("What is the first letter of the arabic alphabet?");

question1Text.setBounds(new Rectangle(64, 158, 319, 19));

q1A.setFont(new java.awt.Font("Tahoma", Font.BOLD, 16));

q1A.setText("?);

q1A.setBounds(new Rectangle(60, 189, 93, 23));

q1B.setFont(new java.awt.Font("Tahoma", Font.BOLD, 16));

q1B.setText("?);

q1B.setBounds(new Rectangle(60, 214, 93, 23));

q1C.setFont(new java.awt.Font("Tahoma", Font.BOLD, 16));

q1C.setText("?);

q1C.setBounds(new Rectangle(60, 241, 93, 23));

question2Text.setBackground(Color.lightGray);

question2Text.setFont(new java.awt.Font("Tahoma", Font.BOLD, 14));

question2Text.setEditable(false);

question2Text.setText(

"Choose the two correct words which finish the sentence.");

question2Text.setBounds(new Rectangle(64, 281, 402, 19));

question2TextArabic.setFont(new java.awt.Font("Tahoma", Font.BOLD, 16));

question2TextArabic.setText("卿 轻巅?轻谘软? 窃逖 蓓 轻谇徙?);

question2TextArabic.setBounds(new Rectangle(63, 311, 404, 30));

this.setForeground(Color.white);

jCheckBox1.setFont(new java.awt.Font("Tahoma", Font.BOLD, 13));

jCheckBox1.setText("峭响");

jCheckBox1.setBounds(new Rectangle(63, 356, 81, 23));

jCheckBox1.addActionListener(new ReadingTest_jCheckBox1_actionAdapter(this));

jCheckBox1.addItemListener(this);

jCheckBox2.setFont(new java.awt.Font("Tahoma", Font.BOLD, 13));

jCheckBox2.setText("撬淝");

jCheckBox2.setBounds(new Rectangle(162, 356, 81, 23));

jCheckBox2.addItemListener(this);

jCheckBox3.setFont(new java.awt.Font("Tahoma", Font.BOLD, 13));

jCheckBox3.setText("轻刳倾");

jCheckBox3.setBounds(new Rectangle(260, 356, 81, 23));

jCheckBox3.addItemListener(this);

jCheckBox4.setFont(new java.awt.Font("Tahoma", Font.BOLD, 13));

jCheckBox4.setText("轻巅?);

jCheckBox4.setBounds(new Rectangle(359, 355, 81, 23));

jCheckBox4.addItemListener(this);

jLabel2.setText("jLabel2");

jLabel2.setBounds(new Rectangle(63, 402, 234, 32));

jLabel4.setText("jLabel4");

jLabel4.setBounds(new Rectangle(63, 433, 254, 28));

jLabel5.setText("jLabel5");

jLabel5.setBounds(new Rectangle(63, 465, 194, 37));

jLabel6.setText("jLabel6");

jLabel6.setBounds(new Rectangle(63, 500, 258, 32));

Submit.setBounds(new Rectangle(507, 575, 115, 52));

Submit.setText("Submit");

Submit.addActionListener(new ReadingTest_Submit_actionAdapter(this));

jLabel7.setText("jLabel7");

jLabel7.setBounds(new Rectangle(472, 480, 95, 34));

question1.add(q1A);

question1.add(q1B);

question1.add(q1C);

this.getContentPane().add(panel1,null);

this.getContentPane().add(Submit);

panel1.add(jLabel1);

panel1.add(jLabel3);

panel1.add(q1A);

panel1.add(q1B);

panel1.add(q1C);

panel1.add(question1Text);

panel1.add(question2Text);

panel1.add(question2TextArabic);

panel1.add(jCheckBox1);

panel1.add(jCheckBox2);

panel1.add(jCheckBox3);

panel1.add(jCheckBox4);

panel1.add(jButton1);

panel1.add(jLabel6);

panel1.add(jLabel5);

panel1.add(jLabel4);

panel1.add(jLabel2);

panel1.add(jLabel7);

panel1.setBounds(new Rectangle(10, 10, 623, 549));

}

publicvoid jButton1_actionPerformed(ActionEvent e){

this.setVisible(false);

}

publicvoid itemStateChanged(ItemEvent e){

int index = 0;

char c ='-';

Object source = e.getItemSelectable();

//count variable works fine in this part

if (source == jCheckBox1){

index = 0;

c ='c';

//assign new string variable the value of text for the checkbox

String textSelected = jCheckBox1.getText();

jLabel2.setText("You checked"+ textSelected);

count = count + 1;

jLabel7.setText(Integer.toString(count));

}elseif (source == jCheckBox2){

index = 1;

c ='g';

String textSelected = jCheckBox2.getText();

jLabel4.setText("You checked"+ textSelected);

count = count + 1;

jLabel7.setText(Integer.toString(count));

}elseif (source == jCheckBox3){

index = 2;

c ='h';

String textSelected = jCheckBox3.getText();

jLabel5.setText("You checked"+ textSelected);

count = count + 1;

jLabel7.setText(Integer.toString(count));

}elseif (source == jCheckBox4){

index = 3;

c ='t';

String textSelected = jCheckBox4.getText();

jLabel6.setText("You checked"+ textSelected);

count = count + 1;

jLabel7.setText(Integer.toString(count));

}

//Now that we know which button was pushed, find out

//whether it was selected or deselected.

//count variable does not work in this section.

if (e.getStateChange() == ItemEvent.DESELECTED){

c ='-';

Object deselectedItem = e.getItemSelectable();

if (deselectedItem==jCheckBox1)

{

jLabel2.setText("You deselected checkbox 1");

count = count - 1;

jLabel7.setText(Integer.toString(count));

}

elseif (deselectedItem==jCheckBox2)

{

jLabel4.setText("You deselected checkbox 2");

count = count - 1;

jLabel7.setText(Integer.toString(count));

}

elseif (deselectedItem==jCheckBox3)

{

jLabel5.setText("You deselected checkbox 3");

count = count - 1;

jLabel7.setText(Integer.toString(count));

}

elseif (deselectedItem==jCheckBox4)

{

jLabel6.setText("You deselected checkbox 4");

count = count - 1;

jLabel7.setText(Integer.toString(count));

}

}

[16163 byte] By [iPortala] at [2007-11-27 2:35:21]
# 1

you can call your_checkbox.isSelected() to know whether it's selected for all your check boxes !

suppose you have an jcheckbox array : JCheckBox[] myCheckBoxArray = ...;

int counter = 0;

for(int i=0; i<myCheckBoxArray.legth; i++){

if(myCheckBoxArray.isSelected())

counter++;

}

and that's all>

java_2006a at 2007-7-12 2:53:37 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks very much. Keep things simple is the motto then!
iPortala at 2007-7-12 2:53:37 > top of Java-index,Java Essentials,New To Java...
# 3
and what about the dukes ? ;)
java_2006a at 2007-7-12 2:53:37 > top of Java-index,Java Essentials,New To Java...
# 4
i gave you some! greedy!
iPortala at 2007-7-12 2:53:37 > top of Java-index,Java Essentials,New To Java...
# 5
> and what about the dukes ? ;)hehehehe <3
DishSoapMonkeya at 2007-7-12 2:53:37 > top of Java-index,Java Essentials,New To Java...