13.3. Additional Practice ExercisesΒΆ
Section 13.1
Look at the scene graph diagram provided in section 13.1.
Identify the parent node of the
Button.Explain what would happen visually if the
ImageViewwere removed from theVBox.
Solution
The parent node of the
Buttonis theHBox.If the
ImageViewwere removed, only theHBoxcontaining theTextFieldandButtonwould remain in theVBox. The app would still run, but no image would be displayed.
Explain what it means to extend a JavaFX class to create a custom component. Give one real-world example of a custom UI component you might create.
Solution
Extending a JavaFX class means creating your own class that inherits functionality from an existing control (e.g.,
HBox,Button,Pane) and customizing behavior or appearance. Example: ASearchBarcomponent that includes aTextFieldand a searchButtonbundled together with built-in event handling.Suppose clicking the
Buttondoes not update theImageView. Suggest two debugging strategies.Solution
Print debug messages to check if the event handler is triggered.
Verify the image URL or file path is valid and supported (check for exceptions).
Optionally add logging to ensure the
ImageViewreference is assigned properly.
The user provides a URL in the
TextField, but the program crashes when an unsupported file type is entered.What exception might be thrown?
How would you handle this gracefully?
Solution
Likely a
IllegalArgumentExceptionor a runtime loading error fromnew Image(url).Use a
try-catchblock and provide user feedback through JavaFX, such as aLabelor anAlertdialog.
Propose one improvement to the GUI that would help users. Explain why it improves usability.
Solution
Answers may vary:
Add prompt text in the
TextFieldso users know to enter an image URL.Display error messages if loading fails.
Add a default image to avoid a blank screen on launch.
Section 13.2
Based on section 13.2, answer the following review questions.
Refer to the original and refactored scene graphs shown in the text.
In the refactored scene graph (where the two lower
VBoxsub-graphs were replaced), what is the parent node of the twoImageLoadercomponents?In the original scene graph, what node type is the root of the repeating sub-graph that we want to encapsulate inside
ImageLoader?
Solution
The parent node of the two
ImageLoadercomponents is the rootHBox(the top-levelHBoxthat contains the twoImageLoaderinstances).The root of the repeating sub-graph is a
VBox(it contains anHBoxand anImageViewas children).
Explain, in two or three sentences, why creating an
ImageLoadercustom component is preferable.Solution
Creating an
ImageLoaderreduces redundancy and centralizes behavior and layout. It makes the code easier to maintain, promotes reuse, and hides implementation details so the top-levelImageAppstays small and focused on composition rather than construction.List which instance variables should be moved into the
ImageLoaderclass (name the node types), and state what to do with the static constants originally inImageApp(visibility and placement).Solution
Instance variables to move into
ImageLoader:HBox urlLayer,TextField urlField,Button loadButton, andImageView imgView(or similarly named variables representing those nodes). Static constants fromImageApp(e.g., default image URL, preferred sizes) should be copied intoImageLoader(or placed in a shared package-level class) and madeprotected staticif other classes in the same package need access. This keeps constants accessible while reducing coupling.Section 13.2 suggests moving the
loadImagemethod fromImageAppintoImageLoader. Describe two things you must remember to do after moving the method so that clicking theButtonstill works.Solution
Set the button's event handler inside
ImageLoader(for example,loadButton.setOnAction(e -> loadImage());) so the handler references theImageLoader's fields (urlField,imgView).Ensure
loadImageis either an instance method ofImageLoader(not static) and that it uses the instance variablesurlField.getText()andimgViewdirectly. Also handle exceptions locally (e.g.,try-catch) to provide user feedback.