Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use inheritance to create a JavaFX Application based on the attached base class. java.util.EventListener, javafx.event.EventHandler public class GUITemplate extends javafx.application.Application implements javafx.event.EventHandler Create a project,

Use inheritance to create a JavaFX Application based on the attached base class.

java.util.EventListener, javafx.event.EventHandler

public class GUITemplate extends javafx.application.Application implements javafx.event.EventHandler 

Create a project, then download the attached .zip file and extract myStyle.css and GUITemplate.java and import both of them into your project. Also, add the jfxrt.jar library to your project.

Create a class that extends GUITemplate.java

In the constructor for your class, create an ArrayList and add "To F" and "To C" to it, then use that as a parameter to the set() method of the superclass to create a GUITemplate with

Instructions saying: Enter a temperature, choose "to C" or "to F" and click Go

one TextField

The ArrayList for the RadioButton Labels

A reference to an object of your class for the EventHandler class

In your main method, call the static void launch method of the base class, passing it the same args parameter as the one to your main method.

Override the public void handle(ActionEvent arg0) method of the base class to code what the Go button does. It should

Declare a double variable for the contents of the TextField, then, in a try block, assign it the value of getAsDouble(0). Catch any IllegalArgumentException, calling setAnswer(s) with the exception's message as the parameter s, and returning from the handle method.

Use getSelectedRBIndex() to check if the first RadioButton is selected, and, if it is, call setAnswer(s) with s being a String stating the input value as a Celsius temperature and its value in Fahrenheit, and, if it is not, call setAnswer(s) with s being a String stating the input value as a Fahrenheit temperature and its value in Celsius.

GUI template:

import javafx.scene.layout.*;

import javafx.event.*;

/**

* This is a template for a JavaFX Application

* To use it, extend GUITemplate, create an ArrayList of Strings

* for the labels of the radio buttons you need, and call the set()

* method of this class in the constructor. In your main method,

* call the main method of this class with your args as the parameter.

*

*/

public class GUITemplate extends Application implements EventHandler {

/** the instructions */

private Label lbl;

/** the answer label */

private Label answer;

/** the Button to execute your instructions */

private Button btnGo;

/** the Button to exit the Application */

private Button btnQuit;

/** a list of TextField controls */

private ArrayList input;

/** a list of RadioButton controls */

private ArrayList rb;

/** the ToggleGroup for the RadioButtons */

private ToggleGroup tg;

/**

* The set method sets up the GUI. It should be called from

* the constructor of your derived class.

* @param instruct is a String for your instructions label at the top

* @param numText is an int representing the number of input TextFields

* @param rbLabel is an ArrayList of Strings, one for each RadioButton you want

* @param myHandle is the EventHandler class, which should be a reference to an object of the derived class, which should override public void handle(ActionEvent arg0)

*/

public void set(String instruct,int numText, ArrayList rbLabel, EventHandler myHandle){

lbl = new Label(instruct);

input = new ArrayList();

for (int i=0; i

rb = new ArrayList();

tg = new ToggleGroup();

for (String rbs: rbLabel){

RadioButton newButton = new RadioButton(rbs);

newButton.setToggleGroup(tg);

rb.add(newButton);

}

}

/**

* The default constructor sets up a dummy GUI with one TextField and two RadioButtons

*/

public GUITemplate(){

String[] labels = {"First","Second"};

lbl = new Label("instructions");

input = new ArrayList();

for (int i=0; i<1; i++) input.add(new TextField());

rb = new ArrayList();

tg = new ToggleGroup();

for (String rbs: labels){

RadioButton newButton = new RadioButton(rbs);

newButton.setToggleGroup(tg);

rb.add(newButton);

}

}

/**

* getAsDouble will attempt to parse the contents of the TextField

* with the given index as a double.

* @param inx is an int which represents the index of the TextField

* @return The value as a double

* @throws ArrayIndexOutOfBoundsException if inx is out of bounds

* @throws IllegalArgumentException if the TextField is not a number.

*/

public double getAsDouble(int inx){

if (inx<0 || inx>=input.size())

throw new ArrayIndexOutOfBoundsException("getAsDouble illegal index: " + inx);

if (!input.get(inx).getText().matches("^[\\d\\-\\.]+$")){

throw new IllegalArgumentException(input.get(inx).getText() + " is not a number");

}

return Double.parseDouble(input.get(inx).getText());

}

/**

* setAnswer sets the answer Label to the passed String

* @param s is the String to set the answer Label's text to

*/

public void setAnswer(String s){

this.answer.setText(s);

}

/**

* getSelectedRBIndex is used to determing which RadioButton is selected

* @return the selected index, or -1 if none are selected

*/

public int getSelectedRBIndex() {

for (int i=0; i

if (rb.get(i).isSelected()) return i;

return -1;

}

@Override

public void start(Stage stage) throws Exception {

HBox hb1 = new HBox();

HBox hb2 = new HBox();

VBox vb = new VBox();

hb2.setSpacing(5);

btnGo = new Button(" Go ");

btnGo.setOnAction(this);

answer = new Label("answer here");

btnQuit = new Button(" Quit ");

btnQuit.setOnAction(new EventHandler() {

@Override

public void handle(ActionEvent arg0) {

stage.close();

}});

hb1.setSpacing(20);

for (TextField tf: this.input){

hb1.getChildren().add(tf);

}

for (RadioButton b: this.rb)

hb2.getChildren().add(b);

this.rb.get(0).setSelected(true);

vb.getChildren().add(lbl);

vb.getChildren().add(hb1);

vb.getChildren().add(hb2);

vb.getChildren().add(btnGo);

vb.getChildren().add(answer);

vb.getChildren().add(btnQuit);

Scene sc = new Scene(vb,800,400);

stage.setScene(sc);

sc.getStylesheets().add("myStyle.css");

stage.show();

}

public static void main(String[] args){

launch(args);

}

/**

* handle should be overridden in the derived class to set

* what happens when the Go button is clicked.

*/

@Override

public void handle(ActionEvent arg0) {

// TODO Auto-generated method stub

}

}

myStyle.css:

.root{

-fx-font-size:30px;

-fx-background-color:#FFFFDD;

}

.button {

-fx-font-size:20px;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions

Question

How is the NDAA used to shape defense policies indirectly?

Answered: 1 week ago

Question

How fast should bidder managers move into the target?

Answered: 1 week ago