Skip to content

Commit

Permalink
Refactored book to Customers.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosFMeneses committed May 19, 2016
1 parent 70dbe5f commit 2ee3ab8
Showing 1 changed file with 49 additions and 51 deletions.
100 changes: 49 additions & 51 deletions src/tableviewdemo/TableViewDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ public class TableViewDemo extends Application {
private final TableView<Person> table = new TableView<>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
new Person("Ethan", "Williams", "ethan.williams@example.com"),
new Person("Emma", "Jones", "emma.jones@example.com"),
new Person("Michael", "Brown", "michael.brown@example.com"));
new Person("Apple", "(800)692–7753", "apple.com"),
new Person("Google", "(800)877-2981", "google.com"),
new Person("Facebook", "(888)275-2174 ", "facebook.com"));
final HBox hb = new HBox();

public static void main(String[] args) {
Expand All @@ -53,77 +51,77 @@ public static void main(String[] args) {
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setTitle("Table View Demo");
stage.setWidth(450);
stage.setHeight(550);

final Label label = new Label("Address Book");
final Label label = new Label("Customers");
label.setFont(new Font("Arial", 20));

table.setEditable(true);
Callback<TableColumn, TableCell> cellFactory;
cellFactory = (TableColumn p) -> new EditingCell();

TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<>("firstName"));
firstNameCol.setCellFactory(cellFactory);
firstNameCol.setOnEditCommit(
TableColumn companyCol = new TableColumn("Company");
companyCol.setMinWidth(100);
companyCol.setCellValueFactory(
new PropertyValueFactory<>("companyName"));
companyCol.setCellFactory(cellFactory);
companyCol.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());
).setCompanyName(t.getNewValue());
}
}
);


TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));
lastNameCol.setCellFactory(cellFactory);
lastNameCol.setOnEditCommit(
TableColumn phoneCol = new TableColumn("Phone");
phoneCol.setMinWidth(150);
phoneCol.setCellValueFactory(
new PropertyValueFactory<>("phoneNumber"));
phoneCol.setCellFactory(cellFactory);
phoneCol.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());
).setPhoneNumber(t.getNewValue());
}
}
);

TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<>("email"));
emailCol.setCellFactory(cellFactory);
emailCol.setOnEditCommit(
TableColumn websiteCol = new TableColumn("Website");
websiteCol.setMinWidth(150);
websiteCol.setCellValueFactory(
new PropertyValueFactory<>("website"));
websiteCol.setCellFactory(cellFactory);
websiteCol.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());
).setWebsite(t.getNewValue());
}
}
);

table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
table.getColumns().addAll(companyCol, phoneCol, websiteCol);

final TextField addFirstName = new TextField();
addFirstName.setPromptText("First Name");
addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
addFirstName.setMaxWidth(companyCol.getPrefWidth());
final TextField addLastName = new TextField();
addLastName.setMaxWidth(lastNameCol.getPrefWidth());
addLastName.setMaxWidth(phoneCol.getPrefWidth());
addLastName.setPromptText("Last Name");
final TextField addEmail = new TextField();
addEmail.setMaxWidth(emailCol.getPrefWidth());
addEmail.setMaxWidth(websiteCol.getPrefWidth());
addEmail.setPromptText("Email");

final Button addButton = new Button("Add");
Expand Down Expand Up @@ -153,38 +151,38 @@ public void handle(CellEditEvent<Person, String> t) {

public static class Person {

private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private final SimpleStringProperty companyName;
private final SimpleStringProperty phoneNumber;
private final SimpleStringProperty website;

private Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
private Person(String cName, String pNumber, String wSite) {
this.companyName = new SimpleStringProperty(cName);
this.phoneNumber = new SimpleStringProperty(pNumber);
this.website = new SimpleStringProperty(wSite);
}

public String getFirstName() {
return firstName.get();
public String getCompanyName() {
return companyName.get();
}

public void setFirstName(String fName) {
firstName.set(fName);
public void setCompanyName(String cName) {
companyName.set(cName);
}

public String getLastName() {
return lastName.get();
public String getPhoneNumber() {
return phoneNumber.get();
}

public void setLastName(String fName) {
lastName.set(fName);
public void setPhoneNumber(String pNumber) {
phoneNumber.set(pNumber);
}

public String getEmail() {
return email.get();
public String getWebsite() {
return website.get();
}

public void setEmail(String fName) {
email.set(fName);
public void setWebsite(String wSite) {
website.set(wSite);
}
}

Expand Down

0 comments on commit 2ee3ab8

Please sign in to comment.