Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

JCheckBox and File IO *Help*

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
i default a var, present="No"; then i wanna it to change to "Yes" when i checked the checkbox.. and then update to the text file...
but got reading file error...
Hope there's someone out there to help..
Thanx...
***Here's my code for the checkBox...***
public void itemStateChanged(ItemEvent e) {

JCheckBox checkCb = (JCheckBox)e.getSource();

Object item = e.getItem();

if (e.getStateChange() == ItemEvent.SELECTED) {
present="Yes";
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
present="No";
}
}

public void actionPerformed (ActionEvent e) {

if(e.getSource()==doneBtn){
try {
mark();
}
catch (IOException ex) {
}
***Here's the update method for the textFile...***
public void mark() throws IOException {
//System.out.println(""+present);
Object rela = relation.getSelectedItem().toString();
String n = nameTf.getText();
String tel = telTf.getText();
String fam = famNoTf.getText();
String table = tableNoTf.getText();
String attend = present;

//create new object
GuestFile updatedGuest = new GuestFile(rela, n, tel, fam, table, attend);

//retrieve objects from text and store in ArrayList

boolean eof=false;
String inputStr="";
ArrayList gList=new ArrayList();
GuestFile gObject;
File inputFile = new File("GuestsData.txt");

FileInputStream in = new FileInputStream(inputFile);
InputStreamReader reader = new InputStreamReader(in);
BufferedReader fileInput = new BufferedReader(reader);

while (!eof) {
try {
inputStr=fileInput.readLine();
if (inputStr==null)
eof=true;
else {
gObject=new GuestFile(inputStr,";");
gList.add(gObject);
}
} catch (EOFException e) {
eof=true;
} catch (IOException e) {
System.out.println("Error reading from file");
eof=true;
} catch (NoSuchElementException e) {
System.out.println("Error reading record:"+inputStr);
}
}
in.close();

//overwrite object to be updated with the new Object
gList.set(index, updatedGuest);

//store ArrayList back into text

File outputFile = new File("GuestsData.txt");

if (outputFile.exists()) {
outputFile.delete();
}

FileOutputStream out = new FileOutputStream(outputFile);
PrintStream fileOutput = new PrintStream(out);

for (int i=0; i<gList.size(); i++) {
gObject = (GuestFile)gList.get(i);
fileOutput.println(gObject.getRelationship() + ";" + gObject.getName() + ";" + gObject.getTel() + ";" + gObject.getFamNo() + ";" + gObject.getTableNo() + ";" + gObject.getPresent());
}

out.close();

}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Closing this duplicate post. Please respond to the thread in IO and Streams. Please do not cross post questions in multiple forums.
 
    Bookmark Topic Watch Topic
  • New Topic