This example illustrates you how to convert temperature from Fahrenheit to Celsius.
SWT provides the class ApplicationWindow of package org.eclipse.jface.window
that provides general framework for creating and managing different windows. In
this example we are going to show you how to convert the temperature. Given is
the description of example code..
Here we have used the method addStatusLine() that configures the window to have a status line. The class Composite contains the controls. The method converterComposite.setLayout() sets the layout. By using the class Label and Text, the labels and textBox are created for converting the temperature. A button is created to perform an action by calling the Event class. A variable selected of string type is defined which gets the text of fvalue by using the method fvalue.getText(). This variable is passed to the method Double.parseDouble(selected) which returns a new double value. The method Double.toString() creates a string representation of the double value.
Following code converts the Fahrenheit temperature into the Celsius temperature:
| double f = Double.parseDouble(selected);
double c = (f - 32) / 1.8;
setStatus("Temperature Convert Successfully from F to C.");
The method open() of class Window opens the window. To check whether the open() method should block until the window close, the method setBlockOnOpen(true) is used.
Here is the code of TemperatureConverter.java
Output will be displayed as:
If you click the convert button without providing the Fahrenheit temperature in the given text box, it will displays the error message using setStatus() method.
If you enter the value, the converted temperature will be displayed into the text of Celsius temperature with a success message.
Posted on: October 3, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles