Temperature Converter in SWT

This example illustrates you how to convert temperature from Fahrenheit to Celsius.

Temperature Converter in SWT

Temperature Converter in SWT

     

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;
cvalue.setText(Double.toString(c));
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

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.jface.window.ApplicationWindow;

public class TemperatureConverter extends ApplicationWindow {
Label flabel;
Label clabel;
Text fvalue;
Text cvalue;
Composite converterComposite;
  
public TemperatureConverter() {
  super(null);
  addStatusLine();
  }
public Control createContents(Composite parent) {
  getShell().setText("Temperature Converter");
  converterComposite= new Composite(parent, SWT.NULL);
  converterComposite.setLayout(new GridLayout(4false));
  
  flabel = new Label(converterComposite, SWT.NULL);
  flabel.setText("Fahrenheit: ");
  fvalue = new Text(converterComposite, SWT.SINGLE | SWT.BORDER);

  clabel = new Label(converterComposite, SWT.NULL);
  clabel.setText("Celsius: ");
  cvalue = new Text(converterComposite, SWT.SINGLE | SWT.BORDER);
 
 Button button = new Button(converterComposite, SWT.PUSH);
  button.setText("covert");
  button.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event event) {
  
  String selected=fvalue.getText();
 if (selected==""){
 cvalue.setText("");
 setStatus("Enter the value.");
 }
 else{
  double f = Double.parseDouble(selected);
  double c = (f - 32) / 1.8;
  cvalue.setText(Double.toString(c));
  setStatus("Temperature Convert Successfully from F to C.");
  }
  }
  });
  return converterComposite;
  }
  public static void main(String[] args) {
  TemperatureConverter converter = new TemperatureConverter();
  converter.setBlockOnOpen(true);
  converter.open(); 
  }  
}

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.

Download Source Code