title: Have to make Address Book with javaFX Search And Delete Button
body: <p>I am trying to make an address book containing some Person info With <code>Add</code>, <code>Edit</code>, <code>Search</code> &amp; <code>Delete</code> options. See below.</p>
  
  <ol>
  <li>Search Button: If user add some info in blocks then instead of
  pressing <code>Add</code> if its press <code>Search</code> then info will display accordingly</li>
  <li>Delete Button: If user selects some row i.e one person's details then
  after pressing delete button it show what will be deleted from data.</li>
  <li>Am Trying To Get Values From ComboBox After i add ComboBox in add button            But its not Storing ComboBOx Value in tablecell.</li>
  </ol>
  
  <p>My Problem Is When I add or Delete Some PersonData from table then it will delete but updating on after i press search button why its happens.</p>
  
  <p>My Current Code Till Yet
  Please help.</p>
  
  <pre><code>import java.util.Iterator;
  import javafx.application.Application;
  import javafx.beans.property.IntegerProperty;
  import javafx.beans.property.SimpleIntegerProperty;
  import javafx.beans.property.SimpleStringProperty;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.geometry.Insets;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.control.ComboBox;
  import javafx.scene.control.Label;
  import javafx.scene.control.ListCell;
  import javafx.scene.control.ListView;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableColumn.CellEditEvent;
  import javafx.scene.control.TableView;
  import javafx.scene.control.TextField;
  import javafx.scene.control.cell.PropertyValueFactory;
  import javafx.scene.control.cell.TextFieldTableCell;
  import javafx.scene.layout.HBox;
  import javafx.scene.layout.VBox;
  import javafx.scene.paint.Color;
  import javafx.scene.text.Font;
  import javafx.stage.Stage;
  import javafx.util.Callback;
  
  public class MyAddressBook extends Application {
  
  // for delete selection
  private IntegerProperty index = new SimpleIntegerProperty();
  
  private TableView&lt;Person&gt; table = new TableView&lt;Person&gt;();
  // for Filter
  private ObservableList&lt;Person&gt; filteredList = FXCollections.observableArrayList();
  
  private final ObservableList&lt;Person&gt; data = FXCollections.observableArrayList
      (new Person("Jacob", "Smith", "Male","12 HouseNo 35Street Newyork", " 1234341", "12-01-1867","jacob.smith@example.com"),
       new Person("Isabella","Johnson", "Female", "6 HouseNo 10Street California"," 9876521", "12-01-1885", "isabella.johnson@example.com"),
       new Person("Ethan", "Williams", "Male","3 Villa House 3Street LosAngels", " 1122331","01-10-1871", "ethan.williams@example.com"));
  
  final HBox hb = new HBox();
  
  public static void main(String[] args) {
  launch(args);
  }
  
  public final double getIndex() {
      return index.get();
  }
  
  public final void setIndex(Integer value) {
      index.set(value);
  }
  
  public IntegerProperty indexProperty() {
      return index;
  }
  
  public void filterPerson(Person filteringPerson) {
      // first clear the data in the table.
      filteredList.clear();
      Iterator&lt;Person&gt; completeData = data.iterator();
      while (completeData.hasNext()) {
          Person person = (Person) completeData.next();
          // if the first name matches searching person name, then add it to
          if (person.getFirstName().contains(filteringPerson.getFirstName())) {
              this.filteredList.add(person);
          }
      }
  }
  
  @Override
  public void start(Stage stage) {
      Scene scene = new Scene(new Group());
      stage.setTitle("Address Book");
      stage.setWidth(900);
      stage.setHeight(550);
  
      final Label label = new Label("Address Book");
      label.setFont(new Font("Arial", 20));
  
      ComboBox genderComboBox = new ComboBox();
      genderComboBox.getItems().addAll("Male", "Female");
      genderComboBox.setValue("Male");
      genderComboBox
              .setCellFactory(new Callback&lt;ListView&lt;String&gt;, ListCell&lt;String&gt;&gt;() {
                  @Override
                  public ListCell&lt;String&gt; call(ListView&lt;String&gt; param) {
                      final ListCell&lt;String&gt; cell = new ListCell&lt;String&gt;() {
                          {
                              super.setPrefWidth(100);
                          }
  
                          @Override
                          public void updateItem(String item, boolean empty) {
                              super.updateItem(item, empty);
                              if (item != null) {
                                  setText(item);
                                  if (item.contains("Male")) {
                                      setTextFill(Color.RED);
                                  } else if (item.contains("Female")) {
                                      setTextFill(Color.GREEN);
                                  } else {
                                      setTextFill(Color.BLACK);
                                  }
                              } else {
                                  setText(null);
                              }
                          }
                      };
                      return cell;
                  }
              });
  
      table.setEditable(true);
      TableColumn&lt;Person, String&gt; firstNameCol = new TableColumn&lt;Person, String&gt;(
              "First Name");
      firstNameCol.setMinWidth(100);
      firstNameCol
              .setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;("firstName"));
      // firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
      firstNameCol
              .setOnEditCommit(new EventHandler&lt;CellEditEvent&lt;Person, String&gt;&gt;() {
                  @Override
                  public void handle(CellEditEvent&lt;Person, String&gt; t) {
                      ((Person) t.getTableView().getItems()
                              .get(t.getTablePosition().getRow()))
                              .setFirstName(t.getNewValue());
                  }
              });
  
      TableColumn lastNameCol = new TableColumn("Last Name");
      lastNameCol.setMinWidth(100);
      lastNameCol
              .setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;("lastName"));
      lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
      lastNameCol
              .setOnEditCommit(new EventHandler&lt;CellEditEvent&lt;Person, String&gt;&gt;() {
                  @Override
                  public void handle(CellEditEvent&lt;Person, String&gt; t) {
                      ((Person) t.getTableView().getItems()
                              .get(t.getTablePosition().getRow()))
                              .setLastName(t.getNewValue());
                  }
              });
  
      TableColumn genderCol = new TableColumn("Gender");
      genderCol.setMinWidth(10);
      genderCol.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;("gender"));
      genderCol.setCellFactory(TextFieldTableCell.forTableColumn());
      genderCol
              .setOnEditCommit(new EventHandler&lt;CellEditEvent&lt;Person, String&gt;&gt;() {
                  @Override
                  public void handle(CellEditEvent&lt;Person, String&gt; t) {
                      ((Person) t.getTableView().getItems()
                              .get(t.getTablePosition().getRow()))
                              .setGender(t.getNewValue());
                  }
              });
  
      TableColumn addressCol = new TableColumn("Address");
      addressCol.setMinWidth(200);
      addressCol
              .setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;("address"));
      addressCol.setCellFactory(TextFieldTableCell.forTableColumn());
      addressCol
              .setOnEditCommit(new EventHandler&lt;CellEditEvent&lt;Person, String&gt;&gt;() {
                  @Override
                  public void handle(CellEditEvent&lt;Person, String&gt; t) {
                      ((Person) t.getTableView().getItems()
                              .get(t.getTablePosition().getRow()))
                              .setAddress(t.getNewValue());
                  }
              });
  
      TableColumn phoneNoCol = new TableColumn("Phone No");
      phoneNoCol.setMinWidth(100);
      phoneNoCol
              .setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;("phoneNo"));
      phoneNoCol.setCellFactory(TextFieldTableCell.forTableColumn());
      phoneNoCol
              .setOnEditCommit(new EventHandler&lt;CellEditEvent&lt;Person, String&gt;&gt;() {
                  @Override
                  public void handle(CellEditEvent&lt;Person, String&gt; t) {
                      ((Person) t.getTableView().getItems()
                              .get(t.getTablePosition().getRow()))
                              .setPhoneNo(t.getNewValue());
                  }
              });
  
      TableColumn dateOfBirthCol = new TableColumn("Date Of Birth");
      dateOfBirthCol.setMinWidth(100);
      dateOfBirthCol
              .setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;("dateOfBirth"));
      dateOfBirthCol.setCellFactory(TextFieldTableCell.forTableColumn());
      dateOfBirthCol
              .setOnEditCommit(new EventHandler&lt;CellEditEvent&lt;Person, String&gt;&gt;() {
                  @Override
                  public void handle(CellEditEvent&lt;Person, String&gt; t) {
                      ((Person) t.getTableView().getItems()
                              .get(t.getTablePosition().getRow()))
                              .setDateOfBirth(t.getNewValue());
                  }
              });
  
      TableColumn emailCol = new TableColumn("Email");
      emailCol.setMinWidth(150);
      emailCol.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;("email"));
      emailCol.setCellFactory(TextFieldTableCell.forTableColumn());
      emailCol.setOnEditCommit(new EventHandler&lt;CellEditEvent&lt;Person, String&gt;&gt;() {
          @Override
          public void handle(CellEditEvent&lt;Person, String&gt; t) {
              ((Person) t.getTableView().getItems()
                      .get(t.getTablePosition().getRow())).setEmail(t
                      .getNewValue());
          }
      });
  
      table.setItems(filteredList);
      filteredList.addAll(data);
      table.getColumns().addAll(firstNameCol, lastNameCol, genderCol,
              addressCol, phoneNoCol, dateOfBirthCol, emailCol);
  
      final TextField addFirstName = new TextField();
      addFirstName.setPromptText("First Name");
      addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
      final TextField addLastName = new TextField();
      addLastName.setMaxWidth(lastNameCol.getPrefWidth());
      addLastName.setPromptText("Last Name");
  
      final TextField addGender = new TextField();
      addGender.setMaxWidth(genderCol.getPrefWidth());
      addGender.setPromptText("Gender");
  
      final TextField addAddress = new TextField();
      addAddress.setMaxWidth(addressCol.getPrefWidth());
      addAddress.setPromptText("Address");
      final TextField addPhoneNo = new TextField();
      addPhoneNo.setMaxWidth(phoneNoCol.getPrefWidth());
      addPhoneNo.setPromptText("PhoneNo");
      final TextField addDateOfBirth = new TextField();
      addDateOfBirth.setMaxWidth(dateOfBirthCol.getPrefWidth());
      addDateOfBirth.setPromptText("DateOfBirth");
      final TextField addEmail = new TextField();
      addEmail.setMaxWidth(emailCol.getPrefWidth());
      addEmail.setPromptText("Email");
  
      final Button addButton = new Button("Add");
      addButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
          @Override
          public void handle(ActionEvent e) {
              data.add(new Person(addFirstName.getText(), addLastName.getText(), addAddress.getText(), addPhoneNo.getText(),
                      addDateOfBirth.getText(), addEmail.getText(), addGender.getText()));
  
              addFirstName.clear();
              addLastName.clear();
              addAddress.clear();
              addPhoneNo.clear();
              addDateOfBirth.clear();
              addEmail.clear();
              addGender.clear();
          }
      });
  
      table.getSelectionModel().selectedItemProperty()
              .addListener(new ChangeListener() {
                  @Override
                  public void changed(ObservableValue observable,
                          Object oldvalue, Object newValue) {
                      Person selectedPerson = (Person) newValue;
                      setIndex(data.indexOf(newValue));
                      System.out.println("OK");
                  }
              });
  
      final Button delButton = new Button("Delete");
      delButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
          @Override
          public void handle(ActionEvent e) {
              data.remove(index.get());
              table.getSelectionModel().clearSelection();
          }
      });
  
      final Button searchButton = new Button("Search");
      searchButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
          @Override
          public void handle(ActionEvent e) {
              Person person = new Person(addFirstName.getText(), addLastName.getText(), addAddress.getText(), addPhoneNo.getText(),
                      addDateOfBirth.getText(), addEmail.getText(), addGender.getText());
              addFirstName.clear();
              addLastName.clear();
              addAddress.clear();
              addPhoneNo.clear();
              addDateOfBirth.clear();
              addEmail.clear();
              addGender.clear();
              filterPerson(person);
          }
      });
  
      hb.getChildren().addAll(addFirstName, addLastName, genderComboBox,addAddress, addPhoneNo, addDateOfBirth, addEmail, addButton,
              delButton, searchButton);
      hb.setSpacing(3);
  
      final VBox vbox = new VBox();
      vbox.setSpacing(5);
      vbox.setPadding(new Insets(10, 0, 0, 10));
      vbox.getChildren().addAll(label, table, hb);
  
      ((Group) scene.getRoot()).getChildren().addAll(vbox);
  
      stage.setScene(scene);
      stage.show();
  }
  
  public static class Person implements Comparable&lt;Person&gt; {
  
      private final SimpleStringProperty firstName;
      private final SimpleStringProperty lastName;
      private final SimpleStringProperty gender;
      private final SimpleStringProperty address;
      private final SimpleStringProperty phoneNo;
      private final SimpleStringProperty dateOfBirth;
      private final SimpleStringProperty email;
  
      private Person(String fName, String lName, String gen, String address, String phNo, String dob, String email) {
          this.firstName = new SimpleStringProperty(fName);
          this.lastName = new SimpleStringProperty(lName);
          this.gender = new SimpleStringProperty(gen);
          this.address = new SimpleStringProperty(address);
          this.phoneNo = new SimpleStringProperty(phNo);
          this.dateOfBirth = new SimpleStringProperty(dob);
          this.email = new SimpleStringProperty(email);
      }
  
      public String getFirstName() {
          return firstName.get();
      }
  
      public void setFirstName(String fName) {
          firstName.set(fName);
      }
  
      public String getLastName() {
          return lastName.get();
      }
  
      public void setLastName(String fName) {
          lastName.set(fName);
      }
  
      public void setGender(String gen) {
          gender.set(gen);
      }
  
      public String getGender() {
          return gender.get();
      }
  
      public String getAddress() {
          return address.get();
      }
  
      public void setAddress(String add) {
          address.set(add);
      }
  
      public String getPhoneNo() {
          return phoneNo.get();
      }
  
      public void setPhoneNo(String phNo) {
          phoneNo.set(phNo);
      }
  
      public String getDateOfBirth() {
          return dateOfBirth.get();
      }
  
      public void setDateOfBirth(String dob) {
          dateOfBirth.set(dob);
      }
  
      public String getEmail() {
          return email.get();
      }
  
      public void setEmail(String fName) {
          email.set(fName);
      }
  
      @Override
      public int compareTo(Person o) {
          return firstName.get().compareToIgnoreCase(o.getFirstName());
      }
  }
  }
  </code></pre>