Home Java Example Java Swing Convert Temperature
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Convert Temperature

Advertisement
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

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 14, 2008

Ask Questions?    Discuss: Convert Temperature   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com