Question
13.4 (Contacts App) Create a Contacts app modeled after the Cover Viewer app (Sections 13.5 13.6). Store the contact information in an ObservableList of Contact
13.4 (Contacts App) Create a Contacts app modeled after the Cover Viewer app (Sections 13.5 13.6). Store the contact information in an ObservableList of Contact objects. A Contact should contain first name, last name, email and phone number properties (you can provide others). When the user selects a contact from the contacts list, its information should display in a Grid of TextFields. As the information is modified (a Contacts data is updated, a new Contact is added or an existing Contact is deleted), the contacts ListView should display the updates. The ListView should display the Contacts last names.
This question is supposed to be covered by the text book solutions for Java How to Program, Early Objects 11th Edition. However the solution provided was from the 10th edition, which dose not match up. I am having a difficult time trying to getting the data to display properly, I have followed the examples as closely as I can. Please help. The answer requires the use of Java FX, my class has been using SceneBuilder by Gluon to create the FXML file associated with the Java code. I will post both the code from the example and my own for your review, thank you to anyone who can help.
CODE EXAMPLE FROM BOOK (images are omitted as it is just an example):
// Book.java public class Book { private String title; // book title private String thumbImage; // source of book cover's thumbnail image private String largeImage; // source of book cover's full-size image
public Book(String title, String thumbImage, String largeImage) { this.title = title; this.thumbImage = thumbImage; this.largeImage = largeImage; } public String getTitle() {return title;}
public void setTitle(String title) {this.title = title;} public String getThumbImage() {return thumbImage;}
public void setThumbImage(String thumbImage) {this.thumbImage = thumbImage;}
public String getLargeImage() {return largeImage;}
public void setLargeImage(String largeImage) {this.largeImage = largeImage;} @Override public String toString() {return getTitle();} }
//ImageTextCell.java // Custom ListView cell factory that displays an Image and text import javafx.geometry.Pos; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.scene.text.TextAlignment;
public class ImageTextCell extends ListCell
// constructor configures VBox, ImageView and Label public ImageTextCell() { vbox.setAlignment(Pos.CENTER); // center VBox contents horizontally
thumbImageView.setPreserveRatio(true); thumbImageView.setFitHeight(100.0); // thumbnail 100 points tall vbox.getChildren().add(thumbImageView); // attach to Vbox
label.setWrapText(true); // wrap if text too wide to fit in label label.setTextAlignment(TextAlignment.CENTER); // center text vbox.getChildren().add(label); // attach to VBox
setPrefWidth(USE_PREF_SIZE); // use preferred size for cell width }
// called to configure each custom ListView cell @Override protected void updateItem(Book item, boolean empty) { // required to ensure that cell displays properly super.updateItem(item, empty);
if (empty || item == null) { setGraphic(null); // don't display anything } else { // set ImageView's thumbnail image thumbImageView.setImage(new Image(item.getThumbImage())); label.setText(item.getTitle()); // configure Label's text setGraphic(vbox); // attach custom layout to ListView cell } } }
// CoverViewerController.java // Controller for Cover Viewer application import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.util.Callback;
public class CoverViewerController { // instance variables for interacting with GUI @FXML private ListView
// stores the list of Book Objects private final ObservableList
public void initialize() { // populate the ObservableList
// when ListView selection changes, show large cover in ImageView booksListView.getSelectionModel().selectedItemProperty(). addListener( new ChangeListener
// CoverViewer.java // Main application class that loads and displays the CoverViewer's GUI. import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage;
public class CoverViewer extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("CoverViewer.fxml")); Scene scene = new Scene(root); stage.setTitle("Cover Viewer"); stage.setScene(scene); stage.show(); }
public static void main(String[] args) { launch(args); } }
MY CODE (I kept it simple just to get it to work, java will compile but no data is displayed):
// Contact.java
public class Contact {
private String fname; private String lname; private String email; private String phone;
public Contact(String fname, String lname, String email, String phone) { setfname(fname); setlname(lname); setemail(email); setphone(phone); }
public String getfname() {return fname;}
public void setfname(String fname) {this.fname = fname;}
public String getlname() {return lname;}
public void setlname(String lname) {this.lname = lname;}
public String getemail() {return email;}
public void setemail(String email) {this.email = email;}
public String getphone() {return phone;}
public void setphone(String email) {this.phone = phone;}
@Override public String toString() {return getlname();} }
//ContactListController.java import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.util.Callback;
public class ContactListController {
// For hard coded data // @FXML private ListView
// fx:id is set for the list view in the fxml file using scene builder @FXML private ListView
private final ObservableList
public void initialize() {
// Attempted to hard coded the data just as a test, dose not work // ObservableList
ConList.setItems(condata); // ConList.setItems(data);
} }
// ContactsViewer.java
import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage;
public class ContactsViewer extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("ContactsViewer.fxml"));
Scene scene = new Scene(root); stage.setTitle("Contacts Viewer"); stage.setScene(scene); stage.show(); }
public static void main(String[] args) { launch(args); } }
// ContactViewer.fxml
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started