Question
JavaFX Programming
Develop a temperature converter GUI JavaFX application that consists of two labels and two text fields. Typing a temperature
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer for your question in Java Programming Language using JavaFX

Kindly upvote if you find the answer helpful.

CODE :

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class TemperatureConverter extends Application{

//Required scene variable
Scene Window;
//Required variables
double celsius = 0.00,fahrenheit = 0.00;
  
//Main method()
public static void main(String[] args){
launch(args);
}
//Method to execute on pressing enter
public static double onEnter(ActionEvent e,String s,double input){
//Required variable
double result = 0;
//If the string is "celsius"
if(s.compareToIgnoreCase("celsius") == 0){
//Calculates fahrenheit value of the input
result = (((9*input)/5) + 32);
}   
else if(s.compareToIgnoreCase("fahrenheit") == 0){
//Else if the input is "fahrenheit"
//Calculates celsius of the input
result = (5*(input - 32))/9;
}
//Returns result
return result;
}
  
//Start method
@Override
public void start(Stage primaryStage) throws Exception {
//Set title for the Stage object
primaryStage.setTitle("Temperature Converter");
  
//Required components of the GUI and their styings
Label celsiusLbl = new Label("Celcius ");
celsiusLbl.setPadding(new Insets(0,0,0,10));
celsiusLbl.setStyle("-fx-font-weight: bold");
Label fahrenheitLbl = new Label("Fahrenheit ");
fahrenheitLbl.setStyle("-fx-font-weight: bold");
  
TextField celsiusTxt = new TextField();
TextField fahrenheitTxt = new TextField();

//Binding event for first textbox
celsiusTxt.setOnAction((ActionEvent e)->{
  
//Read text from first text box to string 'input'
String input = celsiusTxt.getText();
//To handle exception
try{
//If input is not null
if(input.compareTo("") !=0)
//Parse input to double
celsius = Double.parseDouble(input);
//Call onEnter method and calculate fahrenheit
fahrenheit = TemperatureConverter.onEnter(e,"celsius",celsius);
//Display fahrenheit in sen=cond textbox
fahrenheitTxt.setText(String.valueOf(fahrenheit));
}
catch(Exception ex){
//If input is not double then exception will be catched here
  
//Create object of alert class
Alert alert = new Alert(AlertType.INFORMATION);
//Display contents in the alert object
alert.setHeaderText(null);
alert.setTitle("Error");
alert.setContentText("Error, input must be numerical");
alert.showAndWait();
}
  
});
  
//Binding event for second textbox
fahrenheitTxt.setOnAction((ActionEvent e)->{
//Read text from second text box to string 'input'
String input = fahrenheitTxt.getText();
//To handle exception   
try{
//If input is not null
if(input.compareTo("") != 0)
//Parse input to double
fahrenheit = Double.parseDouble(input);
//Call onEnter method and calculate celsius
celsius = TemperatureConverter.onEnter(e,"fahrenheit",fahrenheit);
//Display celsius in first textbox
celsiusTxt.setText(String.valueOf(celsius));
}
catch(Exception ex){
//If input is not double then exception will be catched here
  
//Create object of alert class
Alert alert = new Alert(AlertType.INFORMATION);
//Display contents in the alert object
alert.setHeaderText(null);
alert.setTitle("Error");
alert.setContentText("Error, input must be numerical");
alert.showAndWait();
}
  
  
});
  
//GridPane Object to set components
GridPane g = new GridPane();
//Set components in repsective columns
g.addColumn(0,celsiusLbl);
g.addColumn(1,celsiusTxt);
g.addColumn(2,fahrenheitLbl);
g.addColumn(3,fahrenheitTxt);
  
//Set horizontal gap
g.setHgap(10);
  
//Initialize Window
Window = new Scene(g,500,50);
  
//Add Window to stage object
primaryStage.setScene(Window);
primaryStage.show();
  
}
}

SCREENSHOTS :

Please see the screehsots of the code below for the indentations of the code.

1 2 3 4 5 ç import juvafx.application. Application; import javafx.event. ActionEvent; import javafx.geometry.Insets; import j

33 34 35 36 result (5*(input - 32))/9; } // Returns result return result; } 38 39 40 1 42 43 44 45 46 47 48 49 50 51 52 53 54

67 celsius Double.parseDouble(input); 7/Call on Enter method and calculate fahrenheit fahrenheit = TemperatureConverter.onEnt

//Display celsius in first textbox celsiusTxt.setText(String.valueof (celsius)); 98 99 } catch(Exception ex){ // If input is

- 128 129 130 131 132 133 134 135 // Add Window to stage object primaryStage.setScene (Window); primaryStage.show(); ܝܟ ܢܟ

OUTPUT :

Initial Output

х Temperature Converter Celcius Fahrenheit

If given valid value

х Temperature Converter Celcius 100 Fahrenheit

After clicking "Enter" in first text box

Х Temperature Converter Celcius 100 Fahrenheit 212.0

Similary for Fahrenheit

Temperature Converter - Celcius Fahrenheit 320Temperature Converter x Celcius 160.0 Fahrenheit 320

If given wrong input

Temperature Converter Celcius abcd Fahrenheit Error X Error input must be numerical .- OK

Any doubts regarding this can be explained with pleasure :)

Add a comment
Know the answer?
Add Answer to:
JavaFX Programming Develop a temperature converter GUI JavaFX application that consists of two labels and two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Develop a temperature converter GUI JavaFX application that consists of two labels and two text fields....

    Develop a temperature converter GUI JavaFX application that consists of two labels and two text fields. Typing a temperature into the Celsius field and then pressing the "Enter" key causes the equivalent Fahrenheit temperature to appear in the Fahrenheit field and vice versa. Typing a non-numeric value into either field causes the following error dialog to appear: Formulas are simple:Celsius to Fahrenheit° F = 9/5 ( ° C) + 32Fahrenheit to Celsius° C = 5/9 (° F - 32). O®...

  • C# Temp. Converter program. Create a Celsius and Fahrenheit Temperature Converter application. The application must have...

    C# Temp. Converter program. Create a Celsius and Fahrenheit Temperature Converter application. The application must have 2 Labels (each one of them assigned only to Celsius and Fahrenheit), 2 TextBoxes (each one of them assigned only to Celsius and Fahrenheit), only 1 Convert Button, 1 Clear Button, and 1 Exit Button. Also, 1 Error message label which will display the error messages. The application should only allow the user to enter the desired temperature just in one of the TextBoxes...

  • Question: Fahrenheit To Celsius Temperature Converter GUI Assignment Write A GUI To... java Fahrenholt to Celsius...

    Question: Fahrenheit To Celsius Temperature Converter GUI Assignment Write A GUI To... java Fahrenholt to Celsius Temperature Converter GUI Assignment Write a Gul to convert Fahrenheit temperatures to Celsius temperatures and has the following appearance: Com Convert It must include the following foatures • The frame we must say 'Fahrenheit to Celsius Temperature Converter • A border layout will be used for the GUI • The JLabelite of the GUI wil suy Fahrerholt to Celsius Temperature Converter and be in...

  • Programming Exercise 8.3 | Instructions Write a GUI-based program that allows the user to convert temperature...

    Programming Exercise 8.3 | Instructions Write a GUI-based program that allows the user to convert temperature values between degrees Fahrenheit and degrees Celsius. The interface should have labeled entry fields for these two values. breezypythongui.py temperatureconvert... + 1 2 File: temperatureconverter.py 3 Project 8.3 4 Temperature conversion between Fahrenheit and Celsius. 5 Illustrates the use of numeric data fields. 6 • These components should be arranged in a grid where the labels occupy the first row and the corresponding fields...

  • C# Temperature Converter Application: I have most of the application complete I just need only help...

    C# Temperature Converter Application: I have most of the application complete I just need only help with error messages. My instructor wants me to show four error messages inside an error message label (not a message box) and all of the error messages should appear inside error message label. The four error messages should appear when: 1: User enters data inside both Celsius and fahrenheit text boxes: “You should enter data in only one textbox. Press Clear to start over.”...

  • This is python3. Please help me out. Develop the following GUI interface to allow the user...

    This is python3. Please help me out. Develop the following GUI interface to allow the user to specify the number of rows and number of columns of cards to be shown to the user. Show default values 2 and 2 for number of rows and number of columns. The buttons "New Game" and "Turn Over" span two column-cells each. Fig 1. The GUI when the program first starts. When the user clicks the "New Game" button, the program will read...

  • C# I am having trouble this program. So far my temperature converter program is converting temps...

    C# I am having trouble this program. So far my temperature converter program is converting temps from C to F. However, whenever I try to convert F to C, it's keep crashing and displaying an error message, which I can not figure it out and do not know how to fix it. Attached are two screenshots are of message. Do you know what is causing the issue? I would much appreciate your assistance. namespace Temp Conv { public partial class...

  • Temperature Converter Create a temperature conversion program that will convert the following to Fahrenheit: Celsius Kelvin...

    Temperature Converter Create a temperature conversion program that will convert the following to Fahrenheit: Celsius Kelvin Newton Your program should take a character which represents the temperature to convert from and a value to be converted to using the following specification: C - Celsius K - Kelvin N - Newton In addition your program should take in the following character to exit the program: X - eXit the program The numeric input for your program should be of type double....

  • I need to make javafx GUI application called Email that implements a prototype user interface for...

    I need to make javafx GUI application called Email that implements a prototype user interface for composing email message. The application should have labelled text fields for To, cc,bcc ,subject line, one for message body and button lebelled Send. When we click Send button, the program should print contents of all fields to standard output using println() statement. I am attaching photos of Email.java and EmailPane.java, I need to make it as per these classes CylinderSta.. Cylinder java MultiCylind.. ....

  • Use Kilometer Converter application code to write Temperature Converter application to convert degrees Fahrenheit into degrees...

    Use Kilometer Converter application code to write Temperature Converter application to convert degrees Fahrenheit into degrees Celsius ((F - 32)*5/9). It needs to be JavaFX 1 import javafx.application. Application; 2 import javafx.stage. Stage; 3 import javafx.scene. Scene; 4 import javafx.scene.layout.HBox; 5 import javafx.scene.layout. VBox; 6 import javafx.geometry.Pos; 7 import javafx.geometry.Insets; 8 import javafx.scene.control.Label; 9 import javafx.scene.control. TextField; 10 import javafx.scene.control.Button; 11 import javafx.event. EventHandler; 12 import javafx.event. ActionEvent; 13 14 ** 15 * Kilometer Converter application 16 17 18 public...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT