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

You’re a given a java GUI file (ShakespeareUI.java). The GUI consists of five bu

ID: 3832753 • Letter: Y

Question

You’re a given a java GUI file (ShakespeareUI.java). The GUI consists of five buttons, text object and a
text area.
You’re also given 5 text files named hamlet.txt, richard.txt, othelleo.txt, lear.txt and macbeth.txt. Eachfile contains one famous quote from a play.
You are tasked with writing the code so that when a button is pressed the quote from that play appearsin the text box and the text object on top changes to the name of the play:

extracts the quotes from the file.

import javafx.application.Application;

import javafx.stage.Stage;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.TextArea;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.Pane;

import javafx.scene.text.Font;

import javafx.scene.text.Text;

public class ShakespeareUI extends Application {

  

  

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

public void start(Stage primaryStage) {

BorderPane pane = new BorderPane();

  

// Top of Pane with Text

Pane paneForText = new Pane();

paneForText.setPadding(new Insets(0,0,5,0));

Text shText = new Text(25, 50,"Shakespeare Quotes");

shText.setFont(Font.font("Arial", 28));

paneForText.getChildren().add(shText);

pane.setTop(paneForText);

  

// Center of Border Pane with TextArea

TextArea taQuote = new TextArea();

taQuote.setPrefColumnCount(30);

taQuote.setPrefRowCount(5);

pane.setCenter(taQuote);

  

// Bottom of Pane with Buttons

HBox paneForButtons = new HBox(20);

Button btLear = new Button("King Lear");

Button btMacBeth = new Button("MacBeth");

Button btHamlet = new Button("Hamlet");

Button btRichard = new Button("Richard III");

Button btOthello = new Button("Othello");

pane.setBottom(paneForButtons);

  

paneForButtons.getChildren().addAll(btLear, btMacBeth, btHamlet, btRichard, btOthello );

paneForButtons.setAlignment(Pos.CENTER);

paneForButtons.setStyle("-fx-border-color: green");

  

// Create a scene and place it in the stage

Scene scene = new Scene(pane, 455, 150);

primaryStage.setTitle("Your First and Last Name Here"); // Set the stage title

primaryStage.setScene(scene); // Place the scene in the stage

primaryStage.show(); // Display the stage

  

  

////// Your code here that handles events when buttons are clicked

  

  

/////////////////////////////////////////////////////

}

/**

* The main method is only needed for the IDE with limited

* JavaFX support. Not needed for running from the command line.

*/

public static void main(String[] args) {

launch(args);

}

}

Please use Eclipse to write this program

Explanation / Answer

Following is the final code with even handlers implemented for the button clicks, seperate class for file operations and other required implementation.

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

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.Charset;

public class ShakespeareUI extends Application {

@Override // Override the start method in the Application class
public void start(Stage primaryStage) {

BorderPane pane = new BorderPane();

// Top of Pane with Text
Pane paneForText = new Pane();
paneForText.setPadding(new Insets(0,0,5,0));
Text shText = new Text(25, 50,"Shakespeare Quotes");
shText.setFont(Font.font("Arial", 28));
paneForText.getChildren().add(shText);
pane.setTop(paneForText);

// Center of Border Pane with TextArea
TextArea taQuote = new TextArea();
taQuote.setPrefColumnCount(30);
taQuote.setPrefRowCount(5);
pane.setCenter(taQuote);

// Bottom of Pane with Buttons
HBox paneForButtons = new HBox(20);
Button btLear = new Button("King Lear");
Button btMacBeth = new Button("MacBeth");
Button btHamlet = new Button("Hamlet");
Button btRichard = new Button("Richard III");
Button btOthello = new Button("Othello");
pane.setBottom(paneForButtons);

paneForButtons.getChildren().addAll(btLear, btMacBeth, btHamlet, btRichard, btOthello );
paneForButtons.setAlignment(Pos.CENTER);
paneForButtons.setStyle("-fx-border-color: green");

// Create a scene and place it in the stage
Scene scene = new Scene(pane, 455, 150);
primaryStage.setTitle("Your First and Last Name Here"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage

// code that handles events when buttons are clicked
btLear.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// read the file contents. File should be in the same directory as the code.
String quoteFromFile = FileHandler.getFileContents("lear.txt");
taQuote.setText(quoteFromFile);
shText.setText(btLear.getText());
}
});

btMacBeth.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// read the file contents. File should be in the same directory as the code.
String quoteFromFile = FileHandler.getFileContents("macbeth.txt");
taQuote.setText(quoteFromFile);
shText.setText(btMacBeth.getText());
}
});

btHamlet.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// read the file contents. File should be in the same directory as the code.
String quoteFromFile = FileHandler.getFileContents("hamlet.txt");
taQuote.setText(quoteFromFile);
shText.setText(btHamlet.getText());
}
});

btRichard.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// read the file contents. File should be in the same directory as the code.
String quoteFromFile = FileHandler.getFileContents("richard.txt");
taQuote.setText(quoteFromFile);
shText.setText(btRichard.getText());
}
});

btOthello.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// read the file contents. File should be in the same directory as the code.
String quoteFromFile = FileHandler.getFileContents("othelleo.txt");
taQuote.setText(quoteFromFile);
shText.setText(btOthello.getText());
}
});
}

/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}

// Class to handle file reading.
class FileHandler {
static String getFileContents(String file) {
try {
return new String(Files.readAllBytes(Paths.get(file)), Charset.defaultCharset());
} catch(IOException e) {
return e.toString();
}
}