From 8e40d561afa81693633865bb71f23a945673223a Mon Sep 17 00:00:00 2001 From: Jonas Hinterdorfer Date: Wed, 28 May 2025 18:19:51 +0200 Subject: [PATCH] iplemted textview --- .../controller/AddressBookController.java | 46 +++++++++++++------ .../at/ionas999/adressbook/addressbook.fxml | 9 +++- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/main/java/at/ionas999/adressbook/controller/AddressBookController.java b/src/main/java/at/ionas999/adressbook/controller/AddressBookController.java index dca34e5..9700b69 100644 --- a/src/main/java/at/ionas999/adressbook/controller/AddressBookController.java +++ b/src/main/java/at/ionas999/adressbook/controller/AddressBookController.java @@ -1,13 +1,15 @@ package at.ionas999.adressbook.controller; import at.ionas999.adressbook.models.Contact; -import javafx.collections.FXCollections; +import at.ionas999.adressbook.repository.AddressBook; import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; import javafx.event.ActionEvent; import javafx.scene.control.*; import javafx.scene.control.Alert.AlertType; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.SimpleIntegerProperty; import java.util.function.Predicate; @@ -18,19 +20,36 @@ public class AddressBookController { public TextField phoneField; public TextField emailField; public Label labelContactAmount; - public ListView contactView; + public TableView contactTable; + public TableColumn idColumn; + public TableColumn nameColumn; + public TableColumn phoneColumn; + public TableColumn emailColumn; public Button searchButton; - private final ObservableList contacts = FXCollections.observableArrayList(); + private final AddressBook addressBook = AddressBook.getInstance(); + private final ObservableList contacts = addressBook.getContacts(); private final FilteredList filteredContacts = new FilteredList<>(contacts); public void initialize() { - contactView.setItems(filteredContacts); + idColumn.setCellValueFactory(cellData -> + new SimpleIntegerProperty(cellData.getValue().getId()).asObject()); + + nameColumn.setCellValueFactory(cellData -> + new SimpleStringProperty(cellData.getValue().getName())); + + phoneColumn.setCellValueFactory(cellData -> + new SimpleStringProperty(cellData.getValue().getPhone())); + + emailColumn.setCellValueFactory(cellData -> + new SimpleStringProperty(cellData.getValue().getEmail())); + + contactTable.setItems(filteredContacts); contacts.addListener((ListChangeListener) (observable) -> updateContactCount()); updateContactCount(); - contactView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { + contactTable.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { if (newValue != null) { idField.setText(String.valueOf(newValue.getId())); nameField.setText(newValue.getName()); @@ -61,7 +80,7 @@ public void initialize() { String phone = phoneField.getText(); String email = emailField.getText(); Contact newContact = new Contact(name, phone, email); - contacts.add(newContact); + addressBook.addContact(newContact); clearInputFields(); } catch (IllegalArgumentException e) { showAlert("Error", e.getMessage()); @@ -69,22 +88,22 @@ public void initialize() { } public void deleteContactButtonClickEvent(ActionEvent actionEvent) { - Contact selectedContact = contactView.getSelectionModel().getSelectedItem(); + Contact selectedContact = contactTable.getSelectionModel().getSelectedItem(); if (selectedContact != null) { - contacts.remove(selectedContact); + addressBook.removeContact(selectedContact); } else { showAlert("Error", "No contact selected for deletion."); } } public void saveButtonClickEvent(ActionEvent actionEvent) { - Contact selectedContact = contactView.getSelectionModel().getSelectedItem(); + Contact selectedContact = contactTable.getSelectionModel().getSelectedItem(); if (selectedContact != null) { try { selectedContact.setName(nameField.getText()); selectedContact.setPhone(phoneField.getText()); selectedContact.setEmail(emailField.getText()); - refreshListView(); + refreshTableView(); } catch (IllegalArgumentException e) { showAlert("Error", e.getMessage()); } @@ -98,9 +117,8 @@ public void initialize() { filteredContacts.setPredicate(createFilterPredicate(searchText)); } - private void refreshListView() { - contactView.setItems(null); - contactView.setItems(filteredContacts); + private void refreshTableView() { + contactTable.refresh(); } private void clearInputFields() { @@ -116,4 +134,4 @@ public void initialize() { alert.setContentText(message); alert.showAndWait(); } -} \ No newline at end of file +} diff --git a/src/main/resources/at/ionas999/adressbook/addressbook.fxml b/src/main/resources/at/ionas999/adressbook/addressbook.fxml index 3e6dee0..58fabcb 100644 --- a/src/main/resources/at/ionas999/adressbook/addressbook.fxml +++ b/src/main/resources/at/ionas999/adressbook/addressbook.fxml @@ -22,7 +22,14 @@ - + + + + + + + +