iplemted textview

This commit is contained in:
Jonas Hinterdorfer 2025-05-28 18:19:51 +02:00
parent 46c0e9d760
commit 8e40d561af
2 changed files with 40 additions and 15 deletions

View File

@ -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<Contact> contactView;
public TableView<Contact> contactTable;
public TableColumn<Contact, Integer> idColumn;
public TableColumn<Contact, String> nameColumn;
public TableColumn<Contact, String> phoneColumn;
public TableColumn<Contact, String> emailColumn;
public Button searchButton;
private final ObservableList<Contact> contacts = FXCollections.observableArrayList();
private final AddressBook addressBook = AddressBook.getInstance();
private final ObservableList<Contact> contacts = addressBook.getContacts();
private final FilteredList<Contact> 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<? super Contact>) (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();
}
}
}

View File

@ -22,7 +22,14 @@
<padding>
<Insets top="10" right="10" bottom="10" left="10"/>
</padding>
<ListView fx:id="contactView" VBox.vgrow="ALWAYS" maxHeight="150"/>
<TableView fx:id="contactTable" VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="idColumn" text="ID" prefWidth="50"/>
<TableColumn fx:id="nameColumn" text="Name" prefWidth="150"/>
<TableColumn fx:id="phoneColumn" text="Phone" prefWidth="100"/>
<TableColumn fx:id="emailColumn" text="Email" prefWidth="200"/>
</columns>
</TableView>
<HBox spacing="10" alignment="CENTER" GridPane.rowIndex="4" GridPane.columnIndex="1">
<Label text="Number Of Contacts:"/>
<Label fx:id="labelContactAmount"/>