Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.Serializable;
- public class contactInfo implements Serializable {
- private static final long serialVersionUID = 1L;
- private String firstNameField;
- private String LastNameField;
- private String phoneNum1Field;
- private String phoneNum2Field;
- private String dobField;
- private String addressField;
- private String notesField;
- //every variables has getters and setters.
- }
- @FXML
- private ListView<String> listView_Contact;
- private final ObservableList<String> myContactList = FXCollections.observableArrayList();
- public void saveContents() throws IOException, FileNotFoundException {
- String filename = "pipdata.txt";
- File contactListdata = new File(filename);
- FileOutputStream file = new FileOutputStream(contactListdata, true);
- myContact.setFirstNameField(firstNameTBox_Contact.getText());
- myContact.setLastNameField(lastNameTBox_Contact.getText());
- myContact.setPhoneNum1Field(phoneNum1TBox_Contact.getText());
- myContact.setPhoneNum2Field(phoneNum2TBox_Contact.getText());
- myContact.setDobField(birthdayTBox_Contact.getText());
- myContact.setAddressField(homeAddressTBox_Contact.getText());
- myContact.setNotesField(notesTBox_Contact.getText());
- addToListView();
- try {
- ObjectOutputStream out = new ObjectOutputStream(file);
- out.writeObject(myContact);
- out.writeObject(new ArrayList<>(myContactList));
- out.close();
- file.close();
- } catch (FileNotFoundException o) {
- System.out.println("File not Found");
- }
- }
- }
- public void loadContents() throws IOException, ClassNotFoundException {
- try {
- FileInputStream file = new FileInputStream("pipdata.txt");
- ObjectInputStream ob = new ObjectInputStream(file);
- myContact = (contactInfo) ob.readObject();
- myContactList.setAll((ArrayList<String>) ob.readObject());
- ob.close();
- file.close();
- } catch (FileNotFoundException ex) {
- Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public void itemSeleted(){
- listView_Contact.getSelectionModel().selectedItemProperty()
- .addListener(new ChangeListener<String>() {
- @Override
- public void changed(
- ObservableValue<? extends String> observable,
- String oldValue, String newValue) {
- firstNameTBox_Contact.setText(myContact.getFirstNameField());
- lastNameTBox_Contact.setText(myContact.getLastNameField());
- phoneNum1TBox_Contact.setText(myContact.getPhoneNum1Field());
- phoneNum2TBox_Contact.setText(myContact.getPhoneNum2Field());
- birthdayTBox_Contact.setText(myContact.getDobField());
- homeAddressTBox_Contact.setText(myContact.getAddressField());
- notesTBox_Contact.setText(myContact.getNotesField());
- }
- });
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- try {
- listView_Contact.setItems(myContactList);
- loadContents();
- // itemSeleted();
- } catch (IOException | ClassNotFoundException ex) {
- Logger.getLogger(ContactController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Add Comment
Please, Sign In to add comment