Guest User

Untitled

a guest
Apr 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. import java.io.Serializable;
  2. public class contactInfo implements Serializable {
  3. private static final long serialVersionUID = 1L;
  4. private String firstNameField;
  5. private String LastNameField;
  6. private String phoneNum1Field;
  7. private String phoneNum2Field;
  8. private String dobField;
  9. private String addressField;
  10. private String notesField;
  11. //every variables has getters and setters.
  12. }
  13.  
  14. @FXML
  15. private ListView<String> listView_Contact;
  16.  
  17. private final ObservableList<String> myContactList = FXCollections.observableArrayList();
  18.  
  19. public void saveContents() throws IOException, FileNotFoundException {
  20. String filename = "pipdata.txt";
  21. File contactListdata = new File(filename);
  22. FileOutputStream file = new FileOutputStream(contactListdata, true);
  23. myContact.setFirstNameField(firstNameTBox_Contact.getText());
  24. myContact.setLastNameField(lastNameTBox_Contact.getText());
  25. myContact.setPhoneNum1Field(phoneNum1TBox_Contact.getText());
  26. myContact.setPhoneNum2Field(phoneNum2TBox_Contact.getText());
  27. myContact.setDobField(birthdayTBox_Contact.getText());
  28. myContact.setAddressField(homeAddressTBox_Contact.getText());
  29. myContact.setNotesField(notesTBox_Contact.getText());
  30. addToListView();
  31. try {
  32.  
  33. ObjectOutputStream out = new ObjectOutputStream(file);
  34. out.writeObject(myContact);
  35. out.writeObject(new ArrayList<>(myContactList));
  36. out.close();
  37. file.close();
  38. } catch (FileNotFoundException o) {
  39. System.out.println("File not Found");
  40. }
  41. }
  42. }
  43.  
  44. public void loadContents() throws IOException, ClassNotFoundException {
  45. try {
  46. FileInputStream file = new FileInputStream("pipdata.txt");
  47. ObjectInputStream ob = new ObjectInputStream(file);
  48. myContact = (contactInfo) ob.readObject();
  49. myContactList.setAll((ArrayList<String>) ob.readObject());
  50. ob.close();
  51. file.close();
  52. } catch (FileNotFoundException ex) {
  53. Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE, null, ex);
  54.  
  55. }
  56. }
  57.  
  58. public void itemSeleted(){
  59. listView_Contact.getSelectionModel().selectedItemProperty()
  60. .addListener(new ChangeListener<String>() {
  61. @Override
  62. public void changed(
  63.  
  64. ObservableValue<? extends String> observable,
  65. String oldValue, String newValue) {
  66. firstNameTBox_Contact.setText(myContact.getFirstNameField());
  67. lastNameTBox_Contact.setText(myContact.getLastNameField());
  68. phoneNum1TBox_Contact.setText(myContact.getPhoneNum1Field());
  69. phoneNum2TBox_Contact.setText(myContact.getPhoneNum2Field());
  70. birthdayTBox_Contact.setText(myContact.getDobField());
  71. homeAddressTBox_Contact.setText(myContact.getAddressField());
  72. notesTBox_Contact.setText(myContact.getNotesField());
  73. }
  74. });
  75. }
  76.  
  77. @Override
  78. public void initialize(URL url, ResourceBundle rb) {
  79. try {
  80. listView_Contact.setItems(myContactList);
  81. loadContents();
  82. // itemSeleted();
  83. } catch (IOException | ClassNotFoundException ex) {
  84. Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE, null, ex);
  85. }
  86. }
  87. }
Add Comment
Please, Sign In to add comment