Your task is to create a JavaFX application that uses a Cascading Style Sheet, F
ID: 3705570 • Letter: Y
Question
Your task is to create a JavaFX application that uses a Cascading Style Sheet, FXML source code, and an FXML Controller file. Your software should be a simple I-P-O application with multiple inputs. An I-P-O program gets input form the user, performs calculations or transforms the input somehow, then outputs the result back to the user. You have a great deal of flexibility with the topic or subject of your I-P-O application. See page 52 in Chapter 2 for some examples of I-P-O software. The only criteria is that your application should have multiple inputs.You could create software that is useful for one of your other courses, or in everyday life. For example, you could create a Body Mass Index calculator, an application that calculates a moment of inertia for physics class, or an application that asks the user for the length and width of a lawn, then tells the user how much grass seed is needed for the lawn. Exactly what your program does is up to you, but the look and feel of your software should fit with the topic.
Explanation / Answer
GrassSeedForLawnCalculator.java
package com.chegg.question3;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class GrassSeedForLawnCalculator extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("grassseed_lawn_form.fxml"));
primaryStage.setTitle("Grass Seed For My Lawn Calculator");
primaryStage.setScene(new Scene(root, 800, 500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
GrassSeedForLawnFormController.java
package com.chegg.question3;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Window;
public class GrassSeedForLawnFormController {
@FXML
private TextField lengthOfLawnField;
@FXML
private TextField WidthOfLawnField;
@FXML
private Button submitButton;
@FXML
protected void handleSubmitButtonAction(ActionEvent event) {
Window owner = submitButton.getScene().getWindow();
if(lengthOfLawnField.getText().isEmpty()) {
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Form Error!",
"Please enter the length of your lawn");
return;
}
if(WidthOfLawnField.getText().isEmpty()) {
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Form Error!",
"Please enter the width of your lawn");
return;
}
AlertHelper.showAlert(Alert.AlertType.CONFIRMATION, owner, "Lawn Details Registration Successful!",
"Based on your lawn details, " + (Integer.parseInt(lengthOfLawnField.getText())) * (Integer.parseInt(WidthOfLawnField.getText()))/2 + " seeds would completely cover your lawn. Note: Formula used : (length*width)/2");
}
}
grassseed_lawn_form.fxml
<?import javafx.scene.layout.GridPane?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.Button?>
<GridPane fx:controller="com.chegg.question3.GrassSeedForLawnFormController"
xmlns:fx="http://javafx.com/fxml" alignment="center"
hgap="10" vgap="10">
<padding><Insets top="40" right="40" bottom="40" left="40"/></padding>
<columnConstraints>
<ColumnConstraints minWidth="100" prefWidth="100"
maxWidth="Infinity" halignment="RIGHT">
</ColumnConstraints>
<ColumnConstraints minWidth="200" prefWidth="200"
maxWidth="Infinity" hgrow="ALWAYS">
</ColumnConstraints>
</columnConstraints>
<!-- Add Header Label -->
<Label text="Grass Seed for Lawn Calculator Form (FXML)" GridPane.columnIndex="0"
GridPane.rowIndex="0" GridPane.columnSpan="2"
GridPane.rowSpan="1" GridPane.halignment="CENTER" >
<font>
<Font name="Arial" size="24" ></Font>
</font>
<GridPane.margin>
<Insets top="20" right="0" bottom="20" left="0"></Insets>
</GridPane.margin>
</Label>
<!-- Add Length Label -->
<Label text="Length of the Lawn : " GridPane.columnIndex="0"
GridPane.rowIndex="1" >
</Label>
<!-- Add Length Text Field -->
<TextField fx:id="lengthOfLawnField" prefHeight="40"
GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<!-- Add Width Label -->
<Label text="Width of the Lawn : " GridPane.columnIndex="0"
GridPane.rowIndex="2" >
</Label>
<!-- Add Width Text Field -->
<TextField fx:id="WidthOfLawnField" prefHeight="40"
GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<!-- Add Submit Button -->
<Button fx:id="submitButton" text="Submit"
prefWidth="100" prefHeight="40" defaultButton="true"
GridPane.columnIndex="0" GridPane.rowIndex="4"
GridPane.columnSpan="2" GridPane.rowSpan="1"
GridPane.halignment="CENTER"
>
<GridPane.margin>
<Insets top="20" right="0" bottom="20" left="0"></Insets>
</GridPane.margin>
</Button>
</GridPane>
AlertHelper.java
package com.chegg.question3;
import javafx.scene.control.Alert;
import javafx.stage.Window;
public class AlertHelper {
public static void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.initOwner(owner);
alert.show();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.