Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a CheckBox Array 1

Status
Not open for further replies.

mac2k

Technical User
Oct 2, 2000
10
US
Hi.

I need to save an "1" and "0" array to a plain file taking these values from an "CheckBox" array, but i have problems when i wanted to check if a check box is or not "selected" while processing a ItemStateChange listener, the CheckBox checked is done depending on this "1" or "0" array, look the code:

public class XXX extends JFrame{
private JCheckBox op[];
...

public XXX(String[] cad, StringBuffer ops){

op = new JCheckBox[cad.length];

CheckBoxHandler chkChg = new CheckBoxHandler();

for (int ind=0;ind<cad.length;ind++){
op[ind] = new JCheckBox(cad[ind]);
if (ops.charAt(ind)=='1')
op[ind].setSelected(true);
nvoOps.add(op[ind]);
op[ind].addItemListener(chkChg); // is this Ok?
}

...

public class CheckBoxHandler implements ItemListener {
private int no;
private String el;

public void itemStateChanged(ItemEvent e){
no = e.getStateChange();
if (no==1){
el=e.getSource().toString();
JOptionPane.showMessageDialog(null,"Selected " + el);
} else {
el=e.getSource().toString();
JOptionPane.showMessageDialog(null,"Unselected " + el);
}
}
}

But when i tried to change the value of StringBuffer ops i can't catch the op[ind] object as source, can anyone know how can i do this? help will be appreciate.

thanks

Mac2K
 
You should test the return value from getStateChange like this:-
Code:
if ( e.getStateChange() == ItemEvent.SELECTED ) {
 ...

What about the getItem() method of ItemEvent? This should contain the JCheckBox object which is changing state.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top