Need to create address book using javaFX search and delete button
I'm trying to create an address book containing information about the person with the parameters Add
, Edit
, Search
and Delete
. See below.
- Search button: if the user adds some information to the blocks, and instead of clicking
Add
, if he is clickedSearch
, then the information will be displayed accordingly. - "Delete" button: if the user selects a line, then one person specifies, after pressing the delete button he shows what will be removed from the data.
- Am is trying to get values ββfrom the ComboBox. After adding ComboBox to add button. But this is not storing the ComboBOx value in the tablecell.
My problem is that when I add or remove some personal data from the table, it gets deleted but refreshed after I hit the search button why this is happening.
My current code so far Please help.
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<Person> table = new TableView<Person>();
// for Filter
private ObservableList<Person> filteredList = FXCollections.observableArrayList();
private final ObservableList<Person> 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<Person> 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<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param) {
final ListCell<String> cell = new ListCell<String>() {
{
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<Person, String> firstNameCol = new TableColumn<Person, String>(
"First Name");
firstNameCol.setMinWidth(100);
firstNameCol
.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
// firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
firstNameCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> 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<Person, String>("lastName"));
lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
lastNameCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
((Person) t.getTableView().getItems()
.get(t.getTablePosition().getRow()))
.setLastName(t.getNewValue());
}
});
TableColumn genderCol = new TableColumn("Gender");
genderCol.setMinWidth(10);
genderCol.setCellValueFactory(new PropertyValueFactory<Person, String>("gender"));
genderCol.setCellFactory(TextFieldTableCell.forTableColumn());
genderCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
((Person) t.getTableView().getItems()
.get(t.getTablePosition().getRow()))
.setGender(t.getNewValue());
}
});
TableColumn addressCol = new TableColumn("Address");
addressCol.setMinWidth(200);
addressCol
.setCellValueFactory(new PropertyValueFactory<Person, String>("address"));
addressCol.setCellFactory(TextFieldTableCell.forTableColumn());
addressCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> 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<Person, String>("phoneNo"));
phoneNoCol.setCellFactory(TextFieldTableCell.forTableColumn());
phoneNoCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> 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<Person, String>("dateOfBirth"));
dateOfBirthCol.setCellFactory(TextFieldTableCell.forTableColumn());
dateOfBirthCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
((Person) t.getTableView().getItems()
.get(t.getTablePosition().getRow()))
.setDateOfBirth(t.getNewValue());
}
});
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(150);
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
emailCol.setCellFactory(TextFieldTableCell.forTableColumn());
emailCol.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> 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<ActionEvent>() {
@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<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
data.remove(index.get());
table.getSelectionModel().clearSelection();
}
});
final Button searchButton = new Button("Search");
searchButton.setOnAction(new EventHandler<ActionEvent>() {
@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<Person> {
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());
}
}
}
source to share
This creates a button
final Button addButton = new Button("Add");
addButton.setOnAction(new EventHandler<ActionEvent>() {
@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();
}
});
and this line added all the elements to the scene
hb.getChildren().addAll(addFirstName, addLastName, addGender, addAddress, addPhoneNo, addDateOfBirth, addEmail, addButton);
So, create your buttons
final Button deleteButton = new Button("Delete");
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// Delete your date here
}
});
and add it to the list like
hb.getChildren().addAll(addFirstName, addLastName, addGender, addAddress, addPhoneNo, addDateOfBirth, addEmail, addButton, deleteButton, searchButton);
source to share
This is for searchButton: create a method called applyFilter (Person person): which filters the "data" of the table view based on values ββthat are personally present. Call this method on the searchButton action event.
there is another list
private ObservableList<Person> filteredList= FXCollections.observableArrayList();
1) instead of adding "data" to the table, add "filterList" to the Tableview
2) now add all the "data" items to filterList
tableView.setItems(filteredList);
filteredList.addAll(data);
Example:
final Button searchButton = new Button("search");
searchButton.setOnAction(new EventHandler<ActionEvent>() {
@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);
}
});
filtration method
protected void filterPerson( Person filteringPerson ){
// first clear the data in the table.
filteredList.clear();
Iterator<Person> 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 );
}
}
}
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<Person> table = new TableView<Person>();
// for Filter
private ObservableList<Person> filteredList = FXCollections
.observableArrayList();
private final ObservableList<Person> 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<Person> 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<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param) {
final ListCell<String> cell = new ListCell<String>() {
{
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<Person, String> firstNameCol = new TableColumn<Person, String>(
"First Name");
firstNameCol.setMinWidth(100);
firstNameCol
.setCellValueFactory(new PropertyValueFactory<Person, String>(
"firstName"));
// firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
firstNameCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> 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<Person, String>(
"lastName"));
lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
lastNameCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
((Person) t.getTableView().getItems()
.get(t.getTablePosition().getRow()))
.setLastName(t.getNewValue());
}
});
TableColumn genderCol = new TableColumn("Gender");
genderCol.setMinWidth(10);
genderCol.setCellValueFactory(new PropertyValueFactory<Person, String>(
"gender"));
genderCol.setCellFactory(TextFieldTableCell.forTableColumn());
genderCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
((Person) t.getTableView().getItems()
.get(t.getTablePosition().getRow()))
.setGender(t.getNewValue());
}
});
TableColumn addressCol = new TableColumn("Address");
addressCol.setMinWidth(200);
addressCol
.setCellValueFactory(new PropertyValueFactory<Person, String>(
"address"));
addressCol.setCellFactory(TextFieldTableCell.forTableColumn());
addressCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> 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<Person, String>(
"phoneNo"));
phoneNoCol.setCellFactory(TextFieldTableCell.forTableColumn());
phoneNoCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> 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<Person, String>(
"dateOfBirth"));
dateOfBirthCol.setCellFactory(TextFieldTableCell.forTableColumn());
dateOfBirthCol
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
((Person) t.getTableView().getItems()
.get(t.getTablePosition().getRow()))
.setDateOfBirth(t.getNewValue());
}
});
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(150);
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>(
"email"));
emailCol.setCellFactory(TextFieldTableCell.forTableColumn());
emailCol.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> 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<ActionEvent>() {
@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<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
data.remove(index.get());
table.getSelectionModel().clearSelection();
}
});
final Button searchButton = new Button("Search");
searchButton.setOnAction(new EventHandler<ActionEvent>() {
@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<Person> {
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());
}
}
}
source to share