Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Convert Temperature 
 

To convert the temperature, we have created two textField for Fahrenheit value and Celsius value.

 

Convert Temperature

                         

This section illustrates you how to convert the temperature from Fahrenheit to Celsius.

To convert the temperature, we have created two textField for Fahrenheit value and Celsius value. A button is created to perform an action. As the user enters the Fahrenheit value, and click the button, the temperature in Fahrenheit has been converted into Celsius and displayed on the specified textfield.

The method fahrenheitText.getText() get the text value of Fahrenheit temperature and passed the value to the variable selected of String type. 

Following code converts temperature from F to C:

double f = Double.parseDouble(selected);
double c = (f - 32) / 1.8;
celciusText.setText(Double.toString(c));

The method Double.parseDouble(selected) returns a double value represented by the specified String. The method Double.toString(c) returns the string representation of double argument. 

Here is the code of TemperatureConverterInSwing.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TemperatureConverterInSwing implements ActionListener {

JFrame frame;
JPanel panel;
JTextField fahrenheitText;
JLabel celLabel, fahLabel;
JButton changeTemp;
JTextField celciusText;

public TemperatureConverterInSwing() {
  
  frame = new JFrame("Change Celsius to Fahrenheit");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(new Dimension(18080));

  panel = new JPanel(new GridLayout(32));
  panel.setBorder(BorderFactory.createEmptyBorder(60
  60
  20,
  60)
  );
  addItems();
  frame.getRootPane().setDefaultButton(changeTemp);
    frame.getContentPane().add(panel, BorderLayout.CENTER);
  frame.pack();
  frame.setVisible(true);
}
private void addItems() {
  fahLabel = new JLabel("Fahrenheit", SwingConstants.LEFT);
  fahrenheitText = new JTextField(20);
  
  changeTemp = new JButton("Convert");
  celLabel = new JLabel("Celcius", SwingConstants.LEFT);
    celciusText = new JTextField(20);
    
  changeTemp.addActionListener(this);
    panel=new JPanel(new GridLayout(4,1));
  panel.add(fahLabel);
  panel.add(fahrenheitText);
  panel.add(celLabel);
  panel.add(celciusText);
  panel.add(changeTemp);
  
    celLabel.setBorder(BorderFactory.createEmptyBorder(51055));
  fahLabel.setBorder(BorderFactory.createEmptyBorder(51055));
}
public void actionPerformed(ActionEvent event) {
   String selected=fahrenheitText.getText();
     if (selected.equals("")){
    celciusText.setText("");
        System.out.println("enter the fahrenheit value");
      JOptionPane.showMessageDialog(null,"Enter the fahrenheit value");
       }
     else{
        double f = Double.parseDouble(selected);
        double c = (f - 32) / 1.8;
        celciusText.setText(Double.toString(c));
  }
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
TemperatureConverterInSwing converter = new TemperatureConverterInSwing();
}
public static void main(String[] args) {
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      createAndShowGUI();
    }
  });
}
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: c class orm form io method sed format 2d get ip translation return rotation instance transform show for transformation to

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 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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.