More Tutorials| Bioinformatics| Open Source| Photoshop| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Join and Excel yourself with our Online instructor led training sessions
Training Courses
Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.