Its a beatBox program with 256 checkBoxes and to make a track and run it . With start , stop , loadTrack and saveTrack options. When we click start button it loops over all checkBoxes and make a track with them. Below is code that restore values of checkBoxes , through JFileChooser and FileInput Stream.
void buildAndStartTrack(){
ArrayList checkBoxList = new ArrayList<JCheckBox>();
for ( int i=0; i<256; i++)
{
JCheckBox c = new JCheckBox();
c.setSelected(false);
checkBoxList.add(c);
mainPanel.add(c);
} // end loop
}
//here some more code to maketracks and start sequencer
}
public class MyObjectLoadListener implements ActionListener{
public void actionPerformed(ActionEvent event)
{
JFileChooser fileLoad = new JFileChooser();
fileLoad.showOpenDialog(theFrame);
LoadedFile(fileLoad.getSelectedFile());
}
}
public void LoadedFile(File file)
{
boolean [] checkBoxState = null;
try
{
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream is = new ObjectInputStream(fileIn);
checkBoxState = (boolean[]) is.readObject();
}
catch(Exception ex)
{
ex.printStackTrace();
}
Restoring values of checkBoxes and than it will invoke buildAndStartTrack
//how i did it myself (It has a bug but its working fine)
for (int i=0; i<256; i++)
{
JCheckBox check = new JCheckBox();
if(checkBoxState[i])
{
check.setSelected(true);
checkBoxList.set(i, check);
}
else
{
check.setSelected(false);
checkBoxList.set(i, check);
}
}
sequencer.stop();
buildTrackAndStart();
}
Than i looked at the book and found this code .I am not able to get it even after two days searching over checkBoxs, arraylist topics all over internet and forums.
for (int i=0; i<256; i++)
{
JCheckBox check = (JCheckBox) checkBoxList.get(i);
if(checkBoxState[i])
{
check.setSelected(true); // here doubt
// check is on left handside
// so changing its value shouldn't effect value on right hand side i.e checkBoxList.get(i)
}
else
{
check.setSelected(false);
}
But assignment on left hand side is effecting value on right hand side .
Aucun commentaire:
Enregistrer un commentaire