Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 { private VBox vbox = new VBox(8.0); // 8 points of gap between controls private ImageView thumbImageView = new ImageView(); // initially empty private Label label = new Label();

// 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 booksListView; @FXML private ImageView coverImageView;

// stores the list of Book Objects private final ObservableList books = FXCollections.observableArrayList();

public void initialize() { // populate the ObservableList books.add(new Book("Android How to Program", "/images/small/androidhtp.jpg", "/images/large/androidhtp.jpg")); books.add(new Book("C How to Program", "/images/small/chtp.jpg", "/images/large/chtp.jpg")); books.add(new Book("C++ How to Program", "/images/small/cpphtp.jpg", "/images/large/cpphtp.jpg")); books.add(new Book("Internet and World Wide Web How to Program", "/images/small/iw3htp.jpg", "/images/large/iw3htp.jpg")); books.add(new Book("Java How to Program", "/images/small/jhtp.jpg", "/images/large/jhtp.jpg")); books.add(new Book("Visual Basic How to Program", "/images/small/vbhtp.jpg", "/images/large/vbhtp.jpg")); books.add(new Book("Visual C# How to Program", "/images/small/vcshtp.jpg", "/images/large/vcshtp.jpg")); booksListView.setItems(books); // bind booksListView to books

// when ListView selection changes, show large cover in ImageView booksListView.getSelectionModel().selectedItemProperty(). addListener( new ChangeListener() { @Override public void changed(ObservableValue extends Book> ov, Book oldValue, Book newValue) { coverImageView.setImage( new Image(newValue.getLargeImage())); } } ); // set custom ListView cell factory booksListView.setCellFactory( new Callback, ListCell>() { @Override public ListCell call(ListView listView) { return new ImageTextCell(); } } ); } }

// 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); } }

image text in transcribed

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 ConList;

// fx:id is set for the list view in the fxml file using scene builder @FXML private ListView ConList;

private final ObservableList condata = FXCollections.observableArrayList();

public void initialize() {

// Attempted to hard coded the data just as a test, dose not work // ObservableList data = FXCollections.observableArrayList( // "Joe", "Bill", "Robert" // ); condata.add(new Contact("Matthew", "Parra", "mparra@email.com", "555-222-2121"));

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

Cover Viewer |-Java Dalr C How to Program DEITEL HOW TO PROGRAM EARLY OBJECTS ELEVENTH EDITION CHow to Program Internet and World Wide Web How to Program ava- Use with Java" SE 8 or Java" SE 9 PAUL DE|TEL | HARVEY DEITEL Java How to Program Cover Viewer |-Java Dalr C How to Program DEITEL HOW TO PROGRAM EARLY OBJECTS ELEVENTH EDITION CHow to Program Internet and World Wide Web How to Program ava- Use with Java" SE 8 or Java" SE 9 PAUL DE|TEL | HARVEY DEITEL Java How to Program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Datalog Engines In Databases

Authors: Bas Ketsman ,Paraschos Koutris

1st Edition

ISBN: 1638280428, 978-1638280422

More Books

Students also viewed these Databases questions

Question

What can you do to prevent unethical behavior?

Answered: 1 week ago