ErrorDialog_FXMLController.java
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class ErrorDialog_FXMLController implements Initializable
{
@FXML private Button loadFileButton;
@FXML private TextField fileNameField;
@FXML private TextArea displayArea;
@Override
public void initialize(URL url, ResourceBundle rb) {
loadFileButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event) {
loadFileButtonAction();
}
});
}
private void loadFileButtonAction()
{
Alert alert = new Alert(AlertType.NONE);
if(fileNameField.getText().equals(""))
{
alert.setAlertType(AlertType.ERROR);
alert.setTitle("Field missing alert");
alert.setContentText("Please provide a file name!");
alert.show();
}
else
{
displayArea.setWrapText(true);
displayArea.clear();
String fileName = fileNameField.getText().trim();
File file = new File(fileName);
Scanner fileReader;
try
{
fileReader = new Scanner(file);
while(fileReader.hasNext())
{
if(fileReader.hasNextInt())
{
displayArea.appendText(fileReader.nextInt() + " ");
}
else
{
displayArea.appendText(fileReader.next() + " ");
}
}
alert.setAlertType(AlertType.INFORMATION);
alert.setTitle("Loading success alert");
alert.setContentText("Contents of file \"" + fileName + "\" has
been loaded successfully!");
alert.show();
}catch(FileNotFoundException fnfe){
alert.setAlertType(AlertType.ERROR);
alert.setTitle("File not found alert");
alert.setContentText("File not found.");
alert.show();
}
}
}
}
ErrorDialogFXML.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="287.0"
prefWidth="498.0" xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8.0.171"
fx:controller="errordialogfile.ErrorDialog_FXMLController">
<children>
<Button fx:id="loadFileButton" layoutX="14.0" layoutY="23.0"
mnemonicParsing="false" text="Load File" />
<TextField fx:id="fileNameField" layoutX="90.0" layoutY="23.0"
/>
<TextArea fx:id="displayArea" layoutX="15.0" layoutY="66.0"
prefHeight="200.0" prefWidth="468.0" />
</children>
</AnchorPane>
ErrorDialogFile.java (Main class)
mport javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ErrorDialogFile extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root =
FXMLLoader.load(getClass().getResource("ErrorDialog_FXML.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Homework 8 Extra Credit Assignment");
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
**************************************************************** SCREENSHOT ***********************************************************
Add event-handling code for the "Load File” button and its associated TextField so that the user...