• Post Reply 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JCheckBox and File IO **Help**

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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 cannot update to textfile...
no changes have been made...
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) {
}JOptionPane.showMessageDialog(this, "Entry Entered", "Successful", JOptionPane.INFORMATION_MESSAGE);
}
***Here's my add method to textFile***
public void mark() throws IOException {

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, present);

//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();

}
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at http://www.catb.org/~esr/faqs/smart-questions.html#beprecise
And please use code tags to preserve the formatting of your code...
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic