Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Expert Solution & Answer
Book Icon
Chapter 12, Problem 12AW

Explanation of Solution

Event handler:

  • The event handler is nothing but an object that responds to the events.
  • A particular method in the event handler is called if an event source is connected to the event handler. The object of the event is passed as the argument to that method.
    • This process is known as event firing.
  • The event handler class should implement the “EventHandler” interface.
    • This interface is in the package “javafx.event”.

Algorithm to register an instance of the class with “myButton” control:

Step 1: Register the instance of the class with “myButton” control. It is done with the help of the statement “setOnAction (new ButtonClickHandler ())”.

Statement to register an instance of the class with “myButton” control:

//Register the event handler

myButton.setOnAction(new ButtonClickHandler ());

Example program:

The statement to register the instance of the class with “myButton” control is highlighted.

//Import required packages

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Pos;

import javafx.geometry.Insets;

import javafx.stage.Stage;

import javafx.scene.Scene;

import javafx.scene.layout.VBox;

import javafx.scene.control.Label;

import javafx.scene.control.Button;

//Declare the main class

public class Example extends Application

{

//Create a label

private Label outputLabel;

//Declare the main method

public static void main(String[] args)

{

// Launch the application...

Blurred answer
Students have asked these similar questions
Let's say the graphical user interface (GUI) of an application has a Label control with the name outputLabel. Create a statement that erases any text that the control could be showing at the time.
In this exercise, you’ll use radio buttons to determine whether the conversion is from Fahrenheit to Celsius or vice versa. You’ll also modify the DOM so the labels change when a radio button is clicked, and the page displays an error message when the user enters invalid data.  2. Note that the JavaScript file has some starting JavaScript code, including the $() function, three helper functions, three event handler functions, and a DOMContentLoaded event handler that attaches the three event handlers. 3. Review how the toCelsius() and toFarhenheit() event handler functions call the toggleDisplay() helper function and pass it strings to display. Also note that the toggleDisplay() helper function and the convertTemp() event handler function are incomplete. 4. Code the toggleDisplay() function so it changes the text in the labels for the text boxes to the values in the parameters that it receives. It should also clear the disabled text box where the computed temperature is displayed. 5.…
When working with a PrintDocument control, what is the difference between thePrint method and the PagePrint event handler?

Chapter 12 Solutions

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

Ch. 12.2 - Prob. 12.11CPCh. 12.2 - What is the purpose of the Application classs...Ch. 12.2 - Prob. 12.13CPCh. 12.2 - Prob. 12.14CPCh. 12.3 - What is the general difference between an HBox...Ch. 12.3 - Prob. 12.16CPCh. 12.3 - Prob. 12.17CPCh. 12.3 - How do you change the alignment of an HBox...Ch. 12.3 - Prob. 12.19CPCh. 12.4 - Prob. 12.20CPCh. 12.4 - Prob. 12.21CPCh. 12.4 - Prob. 12.22CPCh. 12.4 - Prob. 12.23CPCh. 12.4 - Prob. 12.24CPCh. 12.5 - Prob. 12.25CPCh. 12.5 - Prob. 12.26CPCh. 12.5 - Prob. 12.27CPCh. 12.5 - Prob. 12.28CPCh. 12.6 - Prob. 12.29CPCh. 12.6 - Prob. 12.30CPCh. 12.6 - Prob. 12.31CPCh. 12.6 - Prob. 12.32CPCh. 12.7 - Prob. 12.33CPCh. 12.7 - Prob. 12.34CPCh. 12.8 - Prob. 12.35CPCh. 12.9 - Prob. 12.36CPCh. 12.9 - Prob. 12.37CPCh. 12 - Prob. 1MCCh. 12 - This type of control appears as a rectangular...Ch. 12 - Typically, when the user clicks this type of...Ch. 12 - Prob. 4MCCh. 12 - Prob. 5MCCh. 12 - Prob. 6MCCh. 12 - Prob. 7MCCh. 12 - All JavaFX applications must extend the class. a....Ch. 12 - This container arranges its contents in a single,...Ch. 12 - Prob. 10MCCh. 12 - You use this class to actually display an image....Ch. 12 - The EventHandler interface specifies a method...Ch. 12 - Prob. 13MCCh. 12 - Prob. 14MCCh. 12 - Prob. 15TFCh. 12 - Prob. 16TFCh. 12 - Prob. 17TFCh. 12 - Prob. 18TFCh. 12 - Prob. 1FTECh. 12 - Prob. 2FTECh. 12 - Assume hbox is an HBox container: // This code has...Ch. 12 - Prob. 4FTECh. 12 - Prob. 5FTECh. 12 - Prob. 1AWCh. 12 - Prob. 2AWCh. 12 - Prob. 3AWCh. 12 - Prob. 4AWCh. 12 - Prob. 5AWCh. 12 - Prob. 6AWCh. 12 - Prob. 7AWCh. 12 - Prob. 8AWCh. 12 - Prob. 9AWCh. 12 - Prob. 10AWCh. 12 - Assume a JavaFX application has a Button control...Ch. 12 - Prob. 12AWCh. 12 - Prob. 13AWCh. 12 - Assume borderPane is the name of an existing...Ch. 12 - Prob. 1SACh. 12 - What is the purpose of the Application classs...Ch. 12 - What is the purpose of the Application classs...Ch. 12 - What purpose do layout containers serve?Ch. 12 - Prob. 5SACh. 12 - What two classes do you use to display an image?Ch. 12 - Prob. 7SACh. 12 - Prob. 8SACh. 12 - Prob. 9SACh. 12 - Prob. 10SACh. 12 - Prob. 11SACh. 12 - Latin Translator Look at the following list of...Ch. 12 - Name Formatter Create a JavaFX application that...Ch. 12 - Tip, Tax, and Total Create a JavaFX application...Ch. 12 - Property Tax A county collects property taxes on...Ch. 12 - Prob. 5PCCh. 12 - Prob. 6PCCh. 12 - Travel Expenses Create a GUI application that...Ch. 12 - Joes Automotive Joes Automotive performs the...Ch. 12 - Tic-Tac-Toe Simulator Create a JavaFX application...Ch. 12 - Prob. 10PC
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning