Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you review and advise me on how to correct code to create: 1. btOne as a I

ID: 3707962 • Letter: C

Question

Could you review and advise me on how to correct code to create:

1. btOne as a Inner Class Listener
2. btTwo as a Anonymous Listener
3. btThree as a Lambda expression.

Thank you

package Threebuttons;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Threebuttons extends Application {

@Override //*Override the start method in the Application class

public void start(Stage primaryStage) {

    // Hold three buttons in an HBox
    HBox hBox = new HBox();
    hBox.setSpacing(10);
    hBox.setAlignment(Pos.CENTER);   
    Button btOne = new Button("Button 1");
    Button btTwo = new Button("Button 2");
    Button btThree = new Button("Button 3");
    hBox.getChildren().addAll(btOne, btTwo, btThree);

    //*Create and Register the handler
    btOne.setOnAction(new OneHandler());

    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(hBox);
    BorderPane.setAlignment(hBox, Pos. CENTER);

    //*Create a scene and place it in the stage
    Scene scene = new Scene(hBox, 300, 500);
    primaryStage.setTitle("SnippetWk11"); //*Set title
    primaryStage.setScene(scene); //*Place the scene in the stage
    primaryStage.show(); //*Display the stage
}

      //*Button One - Inner Class Listener
    class OneListener implements EventHandler<ActionEvent> {
        @Override
        public void handle(ActionEvent e) {
        System.out.println("Hello World");
        }

    //*Button Two - Anonymous Listener
    btTwo.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
        System.out.println("Hello World");
    }  
});

    //*Button Three - Lambda Expression p598 lines23-25
    btThree.setOnAction((ActionEvent e) -> {
        System.out.println("Hello World");
    });
    }

    //*keep main method launch application
    public static void main(String[] args) {
        Application.launch(args);
}
}

Explanation / Answer

//Changes are in bold letters

package Threebuttons;

import javafx.application.Application;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Threebuttons extends Application
{
  
@Override //*Override the start method in the Application class
public void start(Stage primaryStage)
{
// Hold three buttons in an HBox
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);   
  
//Instantiate three buttons
//Note :Modified button objects to correct names
Button btnOne = new Button("Button 1");
Button btnTwo = new Button("Button 2");
Button btnThree = new Button("Button 3");

  
hBox.getChildren().addAll(btOne, btnTwo, btnThree);
//*Create and Register the handler
btnOne.setOnAction(new OneListener());
  
  
  
//*Button Two - Anonymous Listener
//Set btnTwo is same as object btnTwo of Button class
btnTwo.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("Hello World");
}
});
  
  
  
//*Button Three - Lambda Expression
//Set btnThree is same as object btnThree of Button class
btnThree.setOnAction((ActionEvent e) -> {
System.out.println("Hello World");
});
  
BorderPane borderPane = new BorderPane();
borderPane.setCenter(hBox);
BorderPane.setAlignment(hBox, Pos. CENTER);
  
//*Create a scene and place it in the stage
Scene scene = new Scene(hBox, 300, 500);
primaryStage.setTitle("SnippetWk11"); //*Set title
primaryStage.setScene(scene); //*Place the scene in the stage
primaryStage.show(); //*Display the stage
  
}

//*Button One - Inner Class Listener
class OneListener implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent e)
{
System.out.println("Hello World");
}
}
  
//*keep main method launch application
public static void main(String[] args)
{
Application.launch(args);
}
}